This repository was archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathindex.js
175 lines (153 loc) · 4.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* eslint prefer-arrow-callback: 0 */
'use strict';
const path = require('path');
const globby = require('globby');
const pkgConf = require('pkg-conf');
const ESLint = require('eslint').ESLint;
const eslint = require('./lib/eslint');
const workspace = require('./lib/workspace');
const toWritableGlobal = require('./lib/toWritableGlobal');
const DEFAULT_IGNORES = [
'**/node_modules/**',
'bower_components/**',
'coverage/**',
'{tmp,temp}/**',
'**/*.min.js',
'**/bundle.js',
'fixture.js',
'{test/,}fixture{s,}/**',
];
exports.lintText = function lintText(str, options) {
const cwd = path.resolve(options.cwd || process.cwd());
const filePath = options.filename;
const absolutePath = path.resolve(cwd, filePath);
const pkgOpts = pkgConf.sync('marlint', { cwd });
const isTypescript = filePath.endsWith('.ts') || filePath.endsWith('.tsx');
const defaultOpts = eslint.generateOpts(
{ ...pkgOpts, typescript: isTypescript },
options
);
const workspacePaths = workspace.getPaths({ cwd });
if (workspacePaths.length > 0) {
const workspacePath = workspacePaths.find((workspacePath) =>
absolutePath.includes(workspacePath)
);
if (!workspacePath) {
const engine = new ESLint(defaultOpts.eslint);
return engine.lintText(str, { filePath });
}
const workspaceOpts = pkgConf.sync('marlint', { cwd: workspacePath });
const mergedOpts = {
eslint: {
...defaultOpts.eslint,
overrideConfig: {
rules: { ...defaultOpts.eslint.rules, ...workspaceOpts.rules },
globals: Object.assign(
{},
toWritableGlobal(defaultOpts.eslint.globals),
toWritableGlobal(workspaceOpts.globals)
),
},
},
};
const engine = new ESLint(mergedOpts.eslint);
return engine.lintText(str, { filePath });
}
const engine = new ESLint(defaultOpts.eslint);
return engine.lintText(str, { filePath });
};
const DEFAULT_GLOB = '**/*.{js,jsx,ts,tsx}';
exports.lintFiles = function lintFiles(patterns, runtimeOpts) {
const pkgOpts = pkgConf.sync('marlint', { cwd: process.cwd() });
const workspacePaths = workspace.getPaths({ cwd: process.cwd() });
const ignore = DEFAULT_IGNORES.concat(pkgOpts.ignores || []);
const verbose = runtimeOpts.verbose;
let glob;
if (patterns.length === 0) {
glob = DEFAULT_GLOB;
} else {
glob = patterns.map((p) => {
if (p.includes('.') || p.includes('*')) {
return p;
} else {
return p.endsWith('/') ? p + DEFAULT_GLOB : p + '/' + DEFAULT_GLOB;
}
})[0];
}
if (verbose) {
console.log(`Finding files to lint...`);
}
return globby(glob, { ignore }).then((paths) => {
// separate between js and ts files because they use different parser
// and default rules
const pathsByExt = {
ts: [],
js: [],
};
paths.forEach((path) => {
const isTypescript = path.endsWith('.ts') || path.endsWith('.tsx');
if (isTypescript) {
pathsByExt.ts.push(path);
} else {
pathsByExt.js.push(path);
}
});
if (verbose) {
if (pathsByExt.ts.length > 0) {
console.log(
`Found ${pathsByExt.ts.length} TS files and ${pathsByExt.js.length} JS files`
);
} else {
console.log(`${paths.length} JS files found`);
}
}
const jsOptions = eslint.generateOpts(pkgOpts, runtimeOpts);
const tsOptions = eslint.generateOpts(
{ ...pkgOpts, typescript: true },
runtimeOpts
);
// if it's a workspace, allow override root config via workspace package.json
if (workspacePaths.length !== 0) {
if (verbose) {
console.log(
`Workspace (yarn/lerna) detected, found ${workspacePaths.length} packages`
);
}
const jsConfigs = workspace.groupPathsByPackage(
pathsByExt.js,
workspacePaths,
jsOptions
);
const tsConfigs = workspace.groupPathsByPackage(
pathsByExt.ts,
workspacePaths,
tsOptions
);
const configs = jsConfigs.concat(tsConfigs);
return eslint.run(configs, runtimeOpts);
}
// if ts file exists, run them in parallel with JS
if (pathsByExt.ts.length > 0) {
return eslint.run(
[
{
id: '__extensions_js__',
paths: pathsByExt.js,
options: jsOptions,
},
{
id: '__extensions_js__',
paths: pathsByExt.ts,
options: tsOptions,
},
],
runtimeOpts
);
}
// no ts file, pass original paths
return eslint.run(
[{ id: '__shared__', paths, options: jsOptions }],
runtimeOpts
);
});
};