forked from traveloka-archive/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
111 lines (92 loc) · 3.45 KB
/
index.js
File metadata and controls
111 lines (92 loc) · 3.45 KB
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
/* eslint prefer-arrow-callback: 0 */
'use strict';
const path = require('path');
const globby = require('globby');
const pkgConf = require('pkg-conf');
const CLIEngine = require('eslint').CLIEngine;
const eslint = require('./lib/eslint');
const workspace = require('./lib/workspace');
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 CLIEngine(defaultOpts.eslint);
return engine.executeOnText(str, filePath);
}
const workspaceOpts = pkgConf.sync('marlint', { cwd: workspacePath })
const mergedOpts = {
eslint: {
...defaultOpts.eslint,
rules: { ...defaultOpts.eslint.rules, ...workspaceOpts.rules },
globals: defaultOpts.eslint.globals.concat(workspaceOpts.globals || []),
}
};
const engine = new CLIEngine(mergedOpts.eslint);
return engine.executeOnText(str, filePath);
}
const engine = new CLIEngine(defaultOpts.eslint);
return engine.executeOnText(str, filePath);
};
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 || []);
let glob = patterns;
if (patterns.length === 0) {
glob = '**/*.{js,jsx,ts,tsx}';
}
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);
}
});
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) {
return Promise.all([
workspace.runESLint(pathsByExt.js, workspacePaths, jsOptions),
workspace.runESLint(pathsByExt.ts, workspacePaths, tsOptions),
]).then(eslint.mergeReports);
}
// if ts file exists, run them in parallel with JS
if (pathsByExt.ts.length > 0) {
return Promise.all([
eslint.run(pathsByExt.ts, tsOptions),
eslint.run(pathsByExt.js, jsOptions),
]).then(eslint.mergeReports);
}
// no ts file, pass original paths
return eslint.run(paths, jsOptions);
});
};
exports.getFormatter = CLIEngine.getFormatter;
exports.getErrorResults = CLIEngine.getErrorResults;
exports.outputFixes = CLIEngine.outputFixes;