forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruleTester.js
More file actions
129 lines (99 loc) · 3.52 KB
/
Copy pathruleTester.js
File metadata and controls
129 lines (99 loc) · 3.52 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'use strict';
const ESLintRuleTester = require('eslint').RuleTester;
const semver = require('semver');
const eslintPkg = require('eslint/package.json');
// `item` can be a config passed to the constructor, or a test case object/string
function convertToFlat(item, plugins) {
if (typeof item === 'string') {
return item;
}
if (typeof item !== 'object' || item === null) {
throw new TypeError('Invalid value for "item" option. Expected an object or a string.');
}
const newItem = Object.assign({}, item, { languageOptions: {} });
if (newItem.parserOptions) {
newItem.languageOptions.parserOptions = newItem.parserOptions;
if (newItem.parserOptions.ecmaVersion) {
newItem.languageOptions.ecmaVersion = newItem.parserOptions.ecmaVersion;
}
if (newItem.parserOptions.sourceType) {
newItem.languageOptions.sourceType = newItem.parserOptions.sourceType;
}
delete newItem.parserOptions;
}
if (newItem.parser) {
newItem.languageOptions.parser = require(newItem.parser); // eslint-disable-line global-require, import/no-dynamic-require
delete newItem.parser;
}
if (newItem.globals) {
newItem.languageOptions.globals = newItem.globals;
delete newItem.globals;
}
if (plugins) {
newItem.plugins = plugins;
}
return newItem;
}
const eslintMajor = semver.major(eslintPkg.version);
function stripTypeOnEslint10(test) {
if (eslintMajor < 10 || !test || typeof test !== 'object' || !Array.isArray(test.errors)) {
return test;
}
return Object.assign({}, test, {
errors: test.errors.map((err) => {
if (!err || typeof err !== 'object' || !('type' in err)) {
return err;
}
const next = Object.assign({}, err);
delete next.type;
return next;
}),
});
}
let RuleTester = ESLintRuleTester;
if (semver.major(eslintPkg.version) >= 9) {
const PLUGINS = Symbol('eslint-plugin-react plugins');
const RULE_DEFINER = Symbol.for('react.RuleTester.RuleDefiner');
RuleTester = class extends ESLintRuleTester {
constructor(config) {
if ((typeof config !== 'object' && typeof config !== 'undefined') || config === null) {
throw new TypeError('Invalid value for "config" option. Expected an object or undefined.');
}
const newConfig = convertToFlat(config || {});
if (!newConfig.languageOptions.ecmaVersion) {
newConfig.languageOptions.ecmaVersion = 5; // old default
}
if (!newConfig.languageOptions.sourceType) {
newConfig.languageOptions.sourceType = 'script'; // old default
}
super(newConfig);
this[RULE_DEFINER] = {
defineRule: (ruleId, rule) => {
if (!this[PLUGINS]) {
this[PLUGINS] = {};
}
const ruleIdSplit = ruleId.split('/');
if (ruleIdSplit.length !== 2) {
throw new Error('ruleId should be in the format: plugin-name/rule-name');
}
const pluginName = ruleIdSplit[0];
const ruleName = ruleIdSplit[1];
if (!this[PLUGINS][pluginName]) {
this[PLUGINS][pluginName] = { rules: {} };
}
this[PLUGINS][pluginName].rules[ruleName] = rule;
},
};
}
run(ruleName, rule, tests) {
const newTests = {
valid: tests.valid.map((test) => convertToFlat(test, this[PLUGINS])),
invalid: tests.invalid
.map(stripTypeOnEslint10)
.map((test) => convertToFlat(test, this[PLUGINS])),
};
super.run(ruleName, rule, newTests);
}
};
}
module.exports = RuleTester;