|
| 1 | +/** |
| 2 | + * @fileoverview Helpers for tests. |
| 3 | + |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | +const { version } = require('eslint/package.json') |
| 7 | +const { RuleTester } = require('eslint') |
| 8 | +const globals = require('globals') |
| 9 | + |
| 10 | +const majorVersion = Number.parseInt(version.split('.')[0], 10) |
| 11 | + |
| 12 | +function convertConfig(config) { |
| 13 | + if (config instanceof Object === false) { |
| 14 | + return config |
| 15 | + } |
| 16 | + |
| 17 | + if (config.languageOptions == null) { |
| 18 | + config.languageOptions = {} |
| 19 | + } |
| 20 | + |
| 21 | + if (config.parserOptions) { |
| 22 | + Object.assign(config.languageOptions, config.parserOptions) |
| 23 | + delete config.parserOptions |
| 24 | + } |
| 25 | + |
| 26 | + if (typeof config.parser === 'string') { |
| 27 | + config.languageOptions.parser = require(config.parser) |
| 28 | + delete config.parser |
| 29 | + } |
| 30 | + |
| 31 | + if (config.globals instanceof Object) { |
| 32 | + config.languageOptions.globals = config.globals |
| 33 | + delete config.globals |
| 34 | + } |
| 35 | + |
| 36 | + if (config.env instanceof Object) { |
| 37 | + if (config.languageOptions.globals == null) { |
| 38 | + config.languageOptions.globals = {} |
| 39 | + } |
| 40 | + |
| 41 | + for (const key in config.env) { |
| 42 | + Object.assign(config.languageOptions.globals, globals[key]) |
| 43 | + } |
| 44 | + |
| 45 | + delete config.env |
| 46 | + } |
| 47 | + |
| 48 | + delete config.parserOptions |
| 49 | + delete config.parser |
| 50 | + |
| 51 | + return config |
| 52 | +} |
| 53 | + |
| 54 | +exports.RuleTester = function (config = {}) { |
| 55 | + if (majorVersion <= 8) { |
| 56 | + return new RuleTester(config) |
| 57 | + } |
| 58 | + |
| 59 | + const ruleTester = new RuleTester(convertConfig(config)) |
| 60 | + const $run = ruleTester.run.bind(ruleTester) |
| 61 | + ruleTester.run = function (name, rule, tests) { |
| 62 | + tests.valid = tests.valid.map(convertConfig) |
| 63 | + tests.invalid = tests.invalid.map(convertConfig) |
| 64 | + |
| 65 | + $run(name, rule, tests) |
| 66 | + } |
| 67 | + return ruleTester |
| 68 | +} |
0 commit comments