-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
index.js
103 lines (90 loc) · 3.31 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
import { ESLint, RuleTester } from 'eslint';
import _ from 'lodash';
import semver from 'semver'
import config from '../../src';
import ruleNames from './ruleNames.json';
const ruleTester = new RuleTester();
(process.env.npm_config_rule ? process.env.npm_config_rule.split(',') : ruleNames).forEach(async (ruleName) => {
if(semver.gte(ESLint.version, '8.0.0') && ruleName === 'check-examples') {
// This rule cannot yet be supported for ESLint 8;
// The possibility for ESLint 8 support is being tracked at https://github.com/eslint/eslint/issues/14745
return;
}
const rule = config.rules[ruleName];
const parserOptions = {
ecmaVersion: 6,
};
const assertions = (await import(`./assertions/${_.camelCase(ruleName)}`)).default;
if (!_.has(rule, 'meta.schema')) {
const testHasOptions = (item) => {
return item.options;
};
if (
assertions.invalid.some(testHasOptions) ||
assertions.valid.some(testHasOptions)
) {
throw new TypeError(
`Presence of testing options suggests that rule ${ruleName} should ` +
'include a schema.',
);
}
}
let count = 0;
assertions.invalid = assertions.invalid.map((assertion) => {
Reflect.deleteProperty(assertion, 'ignoreReadme');
assertion.parserOptions = _.defaultsDeep(assertion.parserOptions, parserOptions);
assertion.errors.forEach((error) => {
if (!('line' in error)) {
count++;
}
});
return assertion;
});
if (count) {
// Make an exception for now for `require-param` as it helps to find the
// many lines were it is missing to know which tests to check without
// adding false (or failing) expectations now
if (ruleName === 'require-param') {
// eslint-disable-next-line no-console -- CLI
console.log(
`Rule, \`${ruleName}\`, missing line numbers in errors: ${count}`,
);
} else {
// eslint-disable-next-line no-console -- CLI
console.error(`Rule, \`${ruleName}\`, missing line numbers in errors: ${count}`);
return;
}
}
assertions.valid = assertions.valid.map((assertion) => {
Reflect.deleteProperty(assertion, 'ignoreReadme');
if (assertion.errors) {
throw new Error(`Valid assertions for rule ${ruleName} should not have an \`errors\` array.`);
}
if (assertion.output) {
throw new Error(`Valid assertions for rule ${ruleName} should not have an \`output\` property.`);
}
assertion.parserOptions = _.defaultsDeep(assertion.parserOptions, parserOptions);
return assertion;
});
if (process.env.npm_config_invalid) {
const indexes = process.env.npm_config_invalid.split(',');
assertions.invalid = assertions.invalid.filter((_assertion, idx) => {
return indexes.includes(String(idx)) ||
indexes.includes(String(idx - assertions.invalid.length));
});
if (!process.env.npm_config_valid) {
assertions.valid = [];
}
}
if (process.env.npm_config_valid) {
const indexes = process.env.npm_config_valid.split(',');
assertions.valid = assertions.valid.filter((_assertion, idx) => {
return indexes.includes(String(idx)) ||
indexes.includes(String(idx - assertions.valid.length));
});
if (!process.env.npm_config_invalid) {
assertions.invalid = [];
}
}
ruleTester.run(ruleName, rule, assertions);
});