-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathutils.js
347 lines (318 loc) · 13.3 KB
/
utils.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
'use strict';
const util = require('util');
const lodash = require('lodash');
const espree = require('espree');
const escope = require('escope');
const estraverse = require('estraverse');
const assert = require('chai').assert;
const utils = require('../../lib/utils');
describe('utils', () => {
describe('getRuleInfo', () => {
describe('the file does not have a valid rule', () => {
[
'',
'module.exports;',
'module.exports = foo;',
'module.boop = function() {};',
'exports = function() {};',
'module.exports = function* () {};',
'module.exports = async function () {};',
'module.exports = {};',
'module.exports = { meta: {} }',
'module.exports = { create: {} }',
'module.exports = { create: foo }',
'module.exports = { create: function* foo() {} }',
'module.exports = { create: async function foo() {} }',
].forEach(noRuleCase => {
it(`returns null for ${noRuleCase}`, () => {
const ast = espree.parse(noRuleCase, { ecmaVersion: 8 });
assert.isNull(utils.getRuleInfo(ast), 'Expected no rule to be found');
});
});
});
describe('the file has a valid rule', () => {
const CASES = {
'module.exports = { create: function foo() {} };': {
create: { type: 'FunctionExpression', id: { name: 'foo' } }, // (This property will actually contain the AST node.)
meta: null,
isNewStyle: true,
},
'module.exports = { create: () => { } };': {
create: { type: 'ArrowFunctionExpression' },
meta: null,
isNewStyle: true,
},
'module.exports = { create() {}, meta: { } };': {
create: { type: 'FunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
'module.exports.create = function foo() {}; module.exports.meta = {}': {
create: { type: 'FunctionExpression', id: { name: 'foo' } },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
'exports.create = function foo() {}; exports.meta = {};': {
create: { type: 'FunctionExpression', id: { name: 'foo' } },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
'module.exports = { create: () => { } }; exports.create = function foo() {}; exports.meta = {};': {
create: { type: 'ArrowFunctionExpression' },
meta: null,
isNewStyle: true,
},
'exports.meta = {}; module.exports = { create: () => { } };': {
create: { type: 'ArrowFunctionExpression' },
meta: null,
isNewStyle: true,
},
'module.exports = { create: () => { } }; module.exports.meta = {};': {
create: { type: 'ArrowFunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
'module.exports = { meta: {} }; module.exports.create = () => { };': {
create: { type: 'ArrowFunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
'module.exports = { "meta": {} }; module.exports.create = () => { };': {
create: { type: 'ArrowFunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
'module.exports = { create: () => { } }; exports.meta = {};': {
create: { type: 'ArrowFunctionExpression' },
meta: null,
isNewStyle: true,
},
'module.exports = function foo() {}': {
create: { type: 'FunctionExpression', id: { name: 'foo' } },
meta: null,
isNewStyle: false,
},
'module.exports = () => {}': {
create: { type: 'ArrowFunctionExpression' },
meta: null,
isNewStyle: false,
},
'exports.meta = {}; module.exports = () => {}': {
create: { type: 'ArrowFunctionExpression' },
meta: null,
isNewStyle: false,
},
'module.exports = () => {}; module.exports.meta = {};': {
create: { type: 'ArrowFunctionExpression' },
meta: null,
isNewStyle: false,
},
};
Object.keys(CASES).forEach(ruleSource => {
it(ruleSource, () => {
const ast = espree.parse(ruleSource, { ecmaVersion: 6 });
const ruleInfo = utils.getRuleInfo(ast);
assert(
lodash.isMatch(ruleInfo, CASES[ruleSource]),
`Expected \n${util.inspect(ruleInfo)}\nto match\n${util.inspect(CASES[ruleSource])}`
);
});
});
});
});
describe('getContextIdentifiers', () => {
const CASES = {
'module.exports = context => { context; context; context; }' (ast) {
return [
ast.body[0].expression.right.body.body[0].expression,
ast.body[0].expression.right.body.body[1].expression,
ast.body[0].expression.right.body.body[2].expression,
];
},
'module.exports = { meta: {}, create(context, foo = context) {} }' (ast) {
return [ast.body[0].expression.right.properties[1].value.params[1].right];
},
'module.exports = { meta: {}, create(notContext) { notContext; notContext; notContext; } }' (ast) {
return [
ast.body[0].expression.right.properties[1].value.body.body[0].expression,
ast.body[0].expression.right.properties[1].value.body.body[1].expression,
ast.body[0].expression.right.properties[1].value.body.body[2].expression,
];
},
};
Object.keys(CASES).forEach(ruleSource => {
it(ruleSource, () => {
const ast = espree.parse(ruleSource, { ecmaVersion: 6 });
const scope = escope.analyze(ast, { ignoreEval: true, ecmaVersion: 6, sourceType: 'script', nodejsScope: true });
const identifiers = utils.getContextIdentifiers(scope, ast);
assert(identifiers instanceof Set, 'getContextIdentifiers should return a Set');
Array.from(identifiers).forEach((identifier, index) => {
assert.strictEqual(identifier, CASES[ruleSource](ast)[index]);
});
});
});
});
describe('getKeyName', () => {
const CASES = {
'({ foo: 1 })': 'foo',
'({ "foo": 1 })': 'foo',
'({ ["foo"]: 1 })': 'foo',
'({ [`foo`]: 1 })': 'foo',
'({ foo() {} })': 'foo',
'({ "foo"() {} })': 'foo',
'({ ["foo"]() {} })': 'foo',
'({ [`foo`]() {} })': 'foo',
'({ 5: 1 })': '5',
'({ 0x123: 1 })': '291',
'({ [foo]: 1 })': null,
'({ [tag`foo`]: 1 })': null,
'({ ["foo" + "bar"]: 1 })': null,
};
Object.keys(CASES).forEach(objectSource => {
it(objectSource, () => {
const ast = espree.parse(objectSource, { ecmaVersion: 6 });
assert.strictEqual(utils.getKeyName(ast.body[0].expression.properties[0]), CASES[objectSource]);
});
});
});
describe('getTestInfo', () => {
describe('the file does not have valid tests', () => {
[
'',
'module.exports = context => context.report(foo);',
'new (require("eslint").NotRuleTester).run(foo, bar, { valid: [] })',
'new NotRuleTester().run(foo, bar, { valid: [] })',
'new RuleTester()',
'const foo = new RuleTester; bar.run(foo, bar, { valid: [] })',
'new RuleTester().run()',
'new RuleTester().run(foo)',
'new RuleTester().run(foo, bar)',
'new RuleTester().run(foo, bar, notAnObject)',
].forEach(noTestsCase => {
it(`returns no tests for ${noTestsCase}`, () => {
const ast = espree.parse(noTestsCase, { ecmaVersion: 8 });
const scope = escope.analyze(ast, { ignoreEval: true, ecmaVersion: 6, sourceType: 'script', nodejsScope: true });
assert.deepEqual(utils.getTestInfo(scope, ast), [], 'Expected no tests to be found');
});
});
});
describe('the file has valid tests', () => {
const CASES = {
'new RuleTester().run(bar, baz, { valid: [foo], invalid: [bar, baz] })': { valid: 1, invalid: 2 },
'var foo = new RuleTester(); foo.run(bar, baz, { valid: [foo], invalid: [bar] })': { valid: 1, invalid: 1 },
'var foo = new (require("eslint")).RuleTester; foo.run(bar, baz, { valid: [], invalid: [] })': { valid: 0, invalid: 0 },
'var foo = new bar.RuleTester; foo.run(bar, baz, { valid: [], invalid: [bar, baz] })': { valid: 0, invalid: 2 },
};
Object.keys(CASES).forEach(testSource => {
it(testSource, () => {
const ast = espree.parse(testSource, { ecmaVersion: 6 });
const scope = escope.analyze(ast, { ignoreEval: true, ecmaVersion: 6, sourceType: 'script', nodejsScope: true });
const testInfo = utils.getTestInfo(scope, ast);
assert.strictEqual(testInfo.length, 1, 'Expected to find one test run');
assert.strictEqual(
testInfo[0].valid.length,
CASES[testSource].valid,
`Expected ${CASES[testSource].valid} valid cases but got ${testInfo[0].valid.length}`
);
assert.strictEqual(
testInfo[0].invalid.length,
CASES[testSource].invalid,
`Expected ${CASES[testSource].invalid} invalid cases but got ${testInfo[0].invalid.length}`
);
});
});
});
describe('the file has multiple test runs', () => {
const CASES = {
[`
new RuleTester().run(foo, bar, { valid: [foo], invalid: [] });
new RuleTester().run(foo, bar, { valid: [], invalid: [foo, bar] });
`]: [{ valid: 1, invalid: 0 }, { valid: 0, invalid: 2 }],
[`
var foo = new RuleTester;
var bar = new RuleTester;
foo.run(foo, bar, { valid: [foo, bar, baz], invalid: [foo] });
bar.run(foo, bar, { valid: [], invalid: [foo, bar] });
`]: [{ valid: 3, invalid: 1 }, { valid: 0, invalid: 2 }],
[`
var foo = new RuleTester, bar = new RuleTester;
foo.run(foo, bar, { valid: [foo, bar, baz], invalid: [foo] });
bar.run(foo, bar, { valid: [], invalid: [foo, bar] });
`]: [{ valid: 3, invalid: 1 }, { valid: 0, invalid: 2 }],
};
Object.keys(CASES).forEach(testSource => {
it(testSource, () => {
const ast = espree.parse(testSource, { ecmaVersion: 6 });
const scope = escope.analyze(ast, { ignoreEval: true, ecmaVersion: 6, sourceType: 'script', nodejsScope: true });
const testInfo = utils.getTestInfo(scope, ast);
assert.strictEqual(
testInfo.length,
CASES[testSource].length,
`Expected to find ${CASES[testSource].length} test runs but got ${testInfo.length}`
);
CASES[testSource].forEach((testRun, index) => {
assert.strictEqual(
testRun.valid,
testInfo[index].valid.length,
`On run ${index + 1}, expected ${testRun.valid} valid cases but got ${testInfo[index].valid.length}`
);
assert.strictEqual(
testRun.invalid,
testInfo[index].invalid.length,
`On run ${index + 1}, expected ${testRun.invalid} valid cases but got ${testInfo[index].invalid.length}`
);
});
});
});
});
});
describe('getReportInfo', () => {
const CASES = new Map([
[[], () => null],
[['foo', 'bar'], () => null],
[['foo', '"bar"', 'baz', 'qux', 'boop'], args => ({ node: args[0], message: args[1], data: args[2], fix: args[3] })],
[['foo', '`bar`', 'baz', 'qux', 'boop'], args => ({ node: args[0], message: args[1], data: args[2], fix: args[3] })],
[
['foo', '{ bar: 1 }', 'baz', 'qux', 'boop'],
args => ({ node: args[0], loc: args[1], message: args[2], data: args[3], fix: args[4] }),
],
[['foo', 'bar', 'baz'], () => null],
[
['{ node, message }'],
() => ({
node: { type: 'Identifier', name: 'node', start: 17, end: 21 },
message: { type: 'Identifier', name: 'message', start: 23, end: 30 },
}),
],
]);
for (const args of CASES.keys()) {
it(args.join(', '), () => {
const parsedArgs = espree.parse(
`context.report(${args.join(', ')})`,
{ ecmaVersion: 6, loc: false, range: false }
).body[0].expression.arguments;
const reportInfo = utils.getReportInfo(parsedArgs);
assert.deepEqual(reportInfo, CASES.get(args)(parsedArgs));
});
}
});
describe('getSourceCodeIdentifiers', () => {
const CASES = {
'module.exports = context => { const sourceCode = context.getSourceCode(); sourceCode; foo; }': 2,
'module.exports = context => { const x = 1, sc = context.getSourceCode(); sc; sc; sc; sourceCode; }': 4,
'module.exports = context => { const sourceCode = context.getNotSourceCode(); }': 0,
};
Object.keys(CASES).forEach(testSource => {
it(testSource, () => {
const ast = espree.parse(testSource, { ecmaVersion: 6 });
const scope = escope.analyze(ast, { ignoreEval: true, ecmaVersion: 6, sourceType: 'script', nodejsScope: true });
estraverse.traverse(ast, {
enter (node, parent) {
node.parent = parent;
},
});
assert.strictEqual(utils.getSourceCodeIdentifiers(scope, ast).size, CASES[testSource]);
});
});
});
});