|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const ESLintRuleTester = require('eslint').RuleTester; |
| 4 | +const semver = require('semver'); |
| 5 | +const eslintPkg = require('eslint/package.json'); |
| 6 | + |
| 7 | +// `item` can be a config passed to the constructor, or a test case object/string |
| 8 | +function convertToFlat(item, plugins) { |
| 9 | + if (typeof item === 'string') { |
| 10 | + return item; |
| 11 | + } |
| 12 | + |
| 13 | + if (typeof item !== 'object' || item === null) { |
| 14 | + throw new TypeError('Invalid value for "item" option. Expected an object or a string.'); |
| 15 | + } |
| 16 | + |
| 17 | + const newItem = Object.assign({}, item, { languageOptions: {} }); |
| 18 | + |
| 19 | + if (newItem.parserOptions) { |
| 20 | + newItem.languageOptions.parserOptions = newItem.parserOptions; |
| 21 | + |
| 22 | + if (newItem.parserOptions.ecmaVersion) { |
| 23 | + newItem.languageOptions.ecmaVersion = newItem.parserOptions.ecmaVersion; |
| 24 | + } |
| 25 | + |
| 26 | + if (newItem.parserOptions.sourceType) { |
| 27 | + newItem.languageOptions.sourceType = newItem.parserOptions.sourceType; |
| 28 | + } |
| 29 | + |
| 30 | + delete newItem.parserOptions; |
| 31 | + } |
| 32 | + |
| 33 | + if (newItem.parser) { |
| 34 | + newItem.languageOptions.parser = require(newItem.parser); // eslint-disable-line global-require, import/no-dynamic-require |
| 35 | + delete newItem.parser; |
| 36 | + } |
| 37 | + |
| 38 | + if (newItem.globals) { |
| 39 | + newItem.languageOptions.globals = newItem.globals; |
| 40 | + delete newItem.globals; |
| 41 | + } |
| 42 | + |
| 43 | + if (plugins) { |
| 44 | + newItem.plugins = plugins; |
| 45 | + } |
| 46 | + |
| 47 | + return newItem; |
| 48 | +} |
| 49 | + |
| 50 | +let RuleTester = ESLintRuleTester; |
| 51 | + |
| 52 | +if (semver.major(eslintPkg.version) >= 9) { |
| 53 | + const PLUGINS = Symbol('eslint-plugin-react plugins'); |
| 54 | + const RULE_DEFINER = Symbol.for('react.RuleTester.RuleDefiner'); |
| 55 | + |
| 56 | + RuleTester = class extends ESLintRuleTester { |
| 57 | + constructor(config) { |
| 58 | + if ((typeof config !== 'object' && typeof config !== 'undefined') || config === null) { |
| 59 | + throw new TypeError('Invalid value for "config" option. Expected an object or undefined.'); |
| 60 | + } |
| 61 | + |
| 62 | + const newConfig = convertToFlat(config || {}); |
| 63 | + |
| 64 | + if (!newConfig.languageOptions.ecmaVersion) { |
| 65 | + newConfig.languageOptions.ecmaVersion = 5; // old default |
| 66 | + } |
| 67 | + |
| 68 | + if (!newConfig.languageOptions.sourceType) { |
| 69 | + newConfig.languageOptions.sourceType = 'script'; // old default |
| 70 | + } |
| 71 | + |
| 72 | + super(newConfig); |
| 73 | + |
| 74 | + this[RULE_DEFINER] = { |
| 75 | + defineRule: (ruleId, rule) => { |
| 76 | + if (!this[PLUGINS]) { |
| 77 | + this[PLUGINS] = {}; |
| 78 | + } |
| 79 | + |
| 80 | + const ruleIdSplit = ruleId.split('/'); |
| 81 | + |
| 82 | + if (ruleIdSplit.length !== 2) { |
| 83 | + throw new Error('ruleId should be in the format: plugin-name/rule-name'); |
| 84 | + } |
| 85 | + |
| 86 | + const pluginName = ruleIdSplit[0]; |
| 87 | + const ruleName = ruleIdSplit[1]; |
| 88 | + |
| 89 | + if (!this[PLUGINS][pluginName]) { |
| 90 | + this[PLUGINS][pluginName] = { rules: {} }; |
| 91 | + } |
| 92 | + |
| 93 | + this[PLUGINS][pluginName].rules[ruleName] = rule; |
| 94 | + }, |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + run(ruleName, rule, tests) { |
| 99 | + const newTests = { |
| 100 | + valid: tests.valid.map((test) => convertToFlat(test, this[PLUGINS])), |
| 101 | + invalid: tests.invalid.map((test) => convertToFlat(test, this[PLUGINS])), |
| 102 | + }; |
| 103 | + |
| 104 | + super.run(ruleName, rule, newTests); |
| 105 | + } |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +module.exports = RuleTester; |
0 commit comments