|
4 | 4 | import path from 'path';
|
5 | 5 |
|
6 | 6 | import { expect } from 'chai';
|
7 |
| -import { CLIEngine } from 'eslint'; |
| 7 | +import { CLIEngine, ESLint } from 'eslint'; |
8 | 8 | import eslintPkg from 'eslint/package.json';
|
9 | 9 | import semver from 'semver';
|
10 | 10 | import * as importPlugin from '../../src/index';
|
11 | 11 |
|
12 | 12 | describe('CLI regression tests', function () {
|
13 | 13 | describe('issue #210', function () {
|
| 14 | + let eslint; |
14 | 15 | let cli;
|
15 | 16 | before(function () {
|
16 |
| - cli = new CLIEngine({ |
17 |
| - useEslintrc: false, |
18 |
| - configFile: './tests/files/issue210.config.js', |
19 |
| - rulePaths: ['./src/rules'], |
20 |
| - rules: { |
21 |
| - 'named': 2, |
22 |
| - }, |
23 |
| - }); |
24 |
| - cli.addPlugin('eslint-plugin-import', importPlugin); |
| 17 | + if (ESLint) { |
| 18 | + eslint = new ESLint({ |
| 19 | + useEslintrc: false, |
| 20 | + overrideConfigFile: './tests/files/issue210.config.js', |
| 21 | + rulePaths: ['./src/rules'], |
| 22 | + overrideConfig: { |
| 23 | + rules: { |
| 24 | + 'named': 2, |
| 25 | + }, |
| 26 | + }, |
| 27 | + plugins: { 'eslint-plugin-import': importPlugin }, |
| 28 | + }); |
| 29 | + } else { |
| 30 | + cli = new CLIEngine({ |
| 31 | + useEslintrc: false, |
| 32 | + configFile: './tests/files/issue210.config.js', |
| 33 | + rulePaths: ['./src/rules'], |
| 34 | + rules: { |
| 35 | + 'named': 2, |
| 36 | + }, |
| 37 | + }); |
| 38 | + cli.addPlugin('eslint-plugin-import', importPlugin); |
| 39 | + } |
25 | 40 | });
|
26 | 41 | it("doesn't throw an error on gratuitous, erroneous self-reference", function () {
|
27 |
| - expect(() => cli.executeOnFiles(['./tests/files/issue210.js'])).not.to.throw(); |
| 42 | + if (eslint) { |
| 43 | + return eslint.lintFiles(['./tests/files/issue210.js']) |
| 44 | + .catch(() => expect.fail()); |
| 45 | + } else { |
| 46 | + expect(() => cli.executeOnFiles(['./tests/files/issue210.js'])).not.to.throw(); |
| 47 | + } |
28 | 48 | });
|
29 | 49 | });
|
30 | 50 |
|
31 | 51 | describe('issue #1645', function () {
|
| 52 | + let eslint; |
32 | 53 | let cli;
|
33 | 54 | beforeEach(function () {
|
34 | 55 | if (semver.satisfies(eslintPkg.version, '< 6')) {
|
35 | 56 | this.skip();
|
36 | 57 | } else {
|
37 |
| - cli = new CLIEngine({ |
38 |
| - useEslintrc: false, |
39 |
| - configFile: './tests/files/just-json-files/.eslintrc.json', |
40 |
| - rulePaths: ['./src/rules'], |
41 |
| - ignore: false, |
42 |
| - }); |
43 |
| - cli.addPlugin('eslint-plugin-import', importPlugin); |
| 58 | + if (ESLint) { |
| 59 | + eslint = new ESLint({ |
| 60 | + useEslintrc: false, |
| 61 | + overrideConfigFile: './tests/files/just-json-files/.eslintrc.json', |
| 62 | + rulePaths: ['./src/rules'], |
| 63 | + ignore: false, |
| 64 | + plugins: { 'eslint-plugin-import': importPlugin }, |
| 65 | + }); |
| 66 | + } else { |
| 67 | + cli = new CLIEngine({ |
| 68 | + useEslintrc: false, |
| 69 | + configFile: './tests/files/just-json-files/.eslintrc.json', |
| 70 | + rulePaths: ['./src/rules'], |
| 71 | + ignore: false, |
| 72 | + }); |
| 73 | + cli.addPlugin('eslint-plugin-import', importPlugin); |
| 74 | + } |
44 | 75 | }
|
45 | 76 | });
|
46 | 77 |
|
47 | 78 | it('throws an error on invalid JSON', () => {
|
48 | 79 | const invalidJSON = './tests/files/just-json-files/invalid.json';
|
49 |
| - const results = cli.executeOnFiles([invalidJSON]); |
50 |
| - expect(results).to.eql({ |
51 |
| - results: [ |
52 |
| - { |
53 |
| - filePath: path.resolve(invalidJSON), |
54 |
| - messages: [ |
| 80 | + if (eslint) { |
| 81 | + return eslint.lintFiles([invalidJSON]).then(results => { |
| 82 | + expect(results).to.eql( |
| 83 | + [ |
55 | 84 | {
|
56 |
| - column: 2, |
57 |
| - endColumn: 3, |
58 |
| - endLine: 1, |
59 |
| - line: 1, |
60 |
| - message: 'Expected a JSON object, array or literal.', |
61 |
| - nodeType: results.results[0].messages[0].nodeType, // we don't care about this one |
62 |
| - ruleId: 'json/*', |
63 |
| - severity: 2, |
64 |
| - source: results.results[0].messages[0].source, // NewLine-characters might differ depending on git-settings |
| 85 | + filePath: path.resolve(invalidJSON), |
| 86 | + messages: [ |
| 87 | + { |
| 88 | + column: 2, |
| 89 | + endColumn: 3, |
| 90 | + endLine: 1, |
| 91 | + line: 1, |
| 92 | + message: 'Expected a JSON object, array or literal.', |
| 93 | + nodeType: results[0].messages[0].nodeType, // we don't care about this one |
| 94 | + ruleId: 'json/*', |
| 95 | + severity: 2, |
| 96 | + source: results[0].messages[0].source, // NewLine-characters might differ depending on git-settings |
| 97 | + }, |
| 98 | + ], |
| 99 | + errorCount: 1, |
| 100 | + ...(semver.satisfies(eslintPkg.version, '>= 7.32 || ^8.0.0-0') && { |
| 101 | + fatalErrorCount: 0, |
| 102 | + }), |
| 103 | + warningCount: 0, |
| 104 | + fixableErrorCount: 0, |
| 105 | + fixableWarningCount: 0, |
| 106 | + source: results[0].source, // NewLine-characters might differ depending on git-settings |
| 107 | + usedDeprecatedRules: results[0].usedDeprecatedRules, // we don't care about this one |
65 | 108 | },
|
66 | 109 | ],
|
67 |
| - errorCount: 1, |
68 |
| - ...(semver.satisfies(eslintPkg.version, '>= 7.32') && { |
69 |
| - fatalErrorCount: 0, |
70 |
| - }), |
71 |
| - warningCount: 0, |
72 |
| - fixableErrorCount: 0, |
73 |
| - fixableWarningCount: 0, |
74 |
| - source: results.results[0].source, // NewLine-characters might differ depending on git-settings |
75 |
| - }, |
76 |
| - ], |
77 |
| - ...(semver.satisfies(eslintPkg.version, '>= 7.32') && { |
78 |
| - fatalErrorCount: 0, |
79 |
| - }), |
80 |
| - errorCount: 1, |
81 |
| - warningCount: 0, |
82 |
| - fixableErrorCount: 0, |
83 |
| - fixableWarningCount: 0, |
84 |
| - usedDeprecatedRules: results.usedDeprecatedRules, // we don't care about this one |
85 |
| - }); |
| 110 | + ); |
| 111 | + }); |
| 112 | + } else { |
| 113 | + const results = cli.executeOnFiles([invalidJSON]); |
| 114 | + expect(results).to.eql({ |
| 115 | + results: [ |
| 116 | + { |
| 117 | + filePath: path.resolve(invalidJSON), |
| 118 | + messages: [ |
| 119 | + { |
| 120 | + column: 2, |
| 121 | + endColumn: 3, |
| 122 | + endLine: 1, |
| 123 | + line: 1, |
| 124 | + message: 'Expected a JSON object, array or literal.', |
| 125 | + nodeType: results.results[0].messages[0].nodeType, // we don't care about this one |
| 126 | + ruleId: 'json/*', |
| 127 | + severity: 2, |
| 128 | + source: results.results[0].messages[0].source, // NewLine-characters might differ depending on git-settings |
| 129 | + }, |
| 130 | + ], |
| 131 | + errorCount: 1, |
| 132 | + warningCount: 0, |
| 133 | + fixableErrorCount: 0, |
| 134 | + fixableWarningCount: 0, |
| 135 | + source: results.results[0].source, // NewLine-characters might differ depending on git-settings |
| 136 | + }, |
| 137 | + ], |
| 138 | + errorCount: 1, |
| 139 | + warningCount: 0, |
| 140 | + fixableErrorCount: 0, |
| 141 | + fixableWarningCount: 0, |
| 142 | + usedDeprecatedRules: results.usedDeprecatedRules, // we don't care about this one |
| 143 | + }); |
| 144 | + } |
86 | 145 | });
|
87 | 146 | });
|
88 | 147 | });
|
0 commit comments