-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.js
132 lines (120 loc) · 3.49 KB
/
util.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
const fs = require('fs');
const { FileEnumerator } = require('eslint/lib/cli-engine/file-enumerator');
const {
CascadingConfigArrayFactory,
} = require('eslint/lib/cli-engine/cascading-config-array-factory');
function indexOfFirstStatement(text) {
let i = 0;
let slashStarComment = false;
while (i < text.length) {
let s = text.substr(i);
if (slashStarComment) {
const endComment = s.match(/^.*?\*\//s);
if (endComment) {
slashStarComment = false;
i += endComment[0].length;
} else {
return -1;
}
} else {
const ws = s.match(/^\s+/);
if (ws) {
i += ws[0].length;
s = text.substr(i);
}
const multilineComment = s.match(/^\/\*/);
if (multilineComment) {
slashStarComment = true;
i += multilineComment[0].length;
} else {
const lineComment = s.match(/^\/\/.*/);
if (lineComment) {
i += lineComment[0].length;
} else {
// No comments, no whitespace.
return i;
}
}
}
}
return -1;
}
exports.indexOfFirstStatement = indexOfFirstStatement;
function partitionFiles(files, dir = '') {
const jessieFiles = [];
const nonJessieFiles = [];
for (const f of files) {
if (!f.endsWith('.js')) {
nonJessieFiles.push(f);
// eslint-disable-next-line no-continue
continue;
}
// maybe a Jessie file
let isJessie = false;
// TODO: Maybe lazily read the file.
const rawText = fs.readFileSync(`${dir}${f}`, 'utf-8');
const text = rawText.startsWith('#!') ? `// ${rawText}` : rawText;
// Find the 'use jessie' token.
const pos = indexOfFirstStatement(text);
if (pos >= 0) {
for (const jessieToken of ['"use jessie";', "'use jessie';"]) {
if (text.substr(pos, jessieToken.length) === jessieToken) {
isJessie = true;
break;
}
}
}
if (isJessie) {
jessieFiles.push(f);
} else {
nonJessieFiles.push(f);
}
}
return [jessieFiles, nonJessieFiles];
}
exports.partitionFiles = partitionFiles;
function partitionFromGlobs(globs, cwd = undefined) {
const ccaf = new CascadingConfigArrayFactory({ cwd });
const configArrayFactory = {
getConfigArrayForFile(filePath) {
try {
process.env.DISABLE_JESSIE_OVERRIDE = 'true';
return ccaf.getConfigArrayForFile(filePath);
} finally {
delete process.env.DISABLE_JESSIE_OVERRIDE;
}
},
};
const enumerator = new FileEnumerator({ configArrayFactory, cwd });
const files = [];
const dir = cwd ? `${cwd}/` : '';
for (const { filePath } of enumerator.iterateFiles(globs)) {
if (filePath.startsWith(dir)) {
files.push(filePath.substr(dir.length));
} else {
files.push(filePath);
}
}
// console.log('have files', files);
const [jessieFiles, nonJessieFiles] = partitionFiles(files, dir);
return [jessieFiles, nonJessieFiles];
}
exports.partitionFromGlobs = partitionFromGlobs;
function jessieOverrides(globs, srcdir, additionalJessieOverrides = {}) {
const overrides = [];
if (process.env.DISABLE_JESSIE_OVERRIDE) {
// Guard against infinite recursion.
return overrides;
}
const [jessieFiles] = partitionFromGlobs(globs, srcdir);
if (jessieFiles.length) {
// We have some Jessie files to apply against.
overrides.push({
...additionalJessieOverrides,
files: jessieFiles,
extends: ['jessie'],
});
}
return overrides;
}
exports.jessieOverrides = jessieOverrides;