|
1 |
| -"use strict"; |
| 1 | +'use strict'; |
2 | 2 |
|
3 |
| -const path = require("path"); |
4 |
| -const fs = require("fs"); |
5 |
| -const diff = require("cli-diff").default; |
| 3 | +const path = require('path'); |
| 4 | +const fs = require('fs'); |
| 5 | +const diff = require('cli-diff').default; |
6 | 6 |
|
7 |
| -const { Linter, CLIEngine } = require("eslint"); |
8 |
| -const { rules } = require("../lib"); |
| 7 | +const { Linter, CLIEngine } = require('eslint'); |
| 8 | +const { rules } = require('../lib'); |
9 | 9 |
|
10 | 10 | const project = path.dirname(__dirname);
|
11 |
| -const tests = path.join(project, "tests", "rules"); |
| 11 | +const tests = path.join(project, 'tests', 'rules'); |
12 | 12 |
|
13 |
| -const format = new CLIEngine().getFormatter("codeframe"); |
14 |
| -const fakeName = "source.ts"; |
| 13 | +const format = new CLIEngine().getFormatter('codeframe'); |
| 14 | +const fakeName = 'source.ts'; |
15 | 15 |
|
16 | 16 | const linter = new Linter();
|
17 | 17 |
|
18 | 18 | linter.defineRules(rules);
|
19 | 19 |
|
20 | 20 | const testedRules = fs
|
21 |
| - .readdirSync(tests) |
22 |
| - .filter(name => !name.endsWith(".snap.ts")) |
23 |
| - .map(name => name.replace(".src.ts", "")); |
| 21 | + .readdirSync(tests) |
| 22 | + .filter(name => !name.endsWith('.snap.ts')) |
| 23 | + .map(name => name.replace('.src.ts', '')); |
24 | 24 |
|
25 | 25 | for (const rule of testedRules) {
|
26 |
| - console.log(`testing ${rule}...`); |
27 |
| - |
28 |
| - const snapPath = path.join(tests, `${rule}.snap.ts`); |
29 |
| - |
30 |
| - const source = fs.readFileSync(path.join(tests, `${rule}.src.ts`), "utf8"); |
31 |
| - const messages = linter.verify( |
32 |
| - source, |
33 |
| - { |
34 |
| - parser: path.join(project, "parser.js"), |
35 |
| - plugins: [require("../lib/index")], |
36 |
| - useEslintrc: false, |
37 |
| - rules: { [rule]: "error" }, |
38 |
| - }, |
39 |
| - fakeName |
40 |
| - ); |
41 |
| - |
42 |
| - try { |
43 |
| - // eslint-disable-next-line node/no-extraneous-require |
44 |
| - require("chalk").enabled = false; |
45 |
| - } catch (e) { |
46 |
| - /* */ |
| 26 | + console.log(`testing ${rule}...`); |
| 27 | + |
| 28 | + const snapPath = path.join(tests, `${rule}.snap.ts`); |
| 29 | + |
| 30 | + const source = fs.readFileSync(path.join(tests, `${rule}.src.ts`), 'utf8'); |
| 31 | + const messages = linter.verify( |
| 32 | + source, |
| 33 | + { |
| 34 | + parser: path.join(project, 'parser.js'), |
| 35 | + plugins: [require('../lib/index')], |
| 36 | + useEslintrc: false, |
| 37 | + rules: { [rule]: 'error' } |
| 38 | + }, |
| 39 | + fakeName |
| 40 | + ); |
| 41 | + |
| 42 | + try { |
| 43 | + // eslint-disable-next-line node/no-extraneous-require |
| 44 | + require('chalk').enabled = false; |
| 45 | + } catch (e) { |
| 46 | + /* */ |
| 47 | + } |
| 48 | + |
| 49 | + const received = format([ |
| 50 | + { |
| 51 | + filePath: fakeName, |
| 52 | + messages, |
| 53 | + errorCount: messages.length, |
| 54 | + warningCount: 0, |
| 55 | + fixableErrorCount: 0, |
| 56 | + fixableWarningCount: 0, |
| 57 | + source |
47 | 58 | }
|
48 |
| - |
49 |
| - const received = format([ |
50 |
| - { |
51 |
| - filePath: fakeName, |
52 |
| - messages, |
53 |
| - errorCount: messages.length, |
54 |
| - warningCount: 0, |
55 |
| - fixableErrorCount: 0, |
56 |
| - fixableWarningCount: 0, |
57 |
| - source, |
58 |
| - }, |
59 |
| - ]) |
60 |
| - .replace( |
61 |
| - new RegExp( |
62 |
| - String.raw`^error: (.+?) \(${rule}\) at ${fakeName.replace( |
63 |
| - ".", |
64 |
| - "\\." |
65 |
| - )}:(\d+:\d+):$`, |
66 |
| - "gm" |
67 |
| - ), |
68 |
| - "// [$2] $1" |
69 |
| - ) |
70 |
| - .replace(/^([ >]) (?:\d+|\s+) \| /gm, "$1") |
71 |
| - .replace(/\n\n\n\d+ errors found\./, "\n"); |
72 |
| - |
73 |
| - try { |
74 |
| - // eslint-disable-next-line node/no-extraneous-require |
75 |
| - require("chalk").enabled = true; |
76 |
| - } catch (e) { |
77 |
| - /* */ |
| 59 | + ]) |
| 60 | + .replace( |
| 61 | + new RegExp( |
| 62 | + String.raw`^error: (.+?) \(${rule}\) at ${fakeName.replace( |
| 63 | + '.', |
| 64 | + '\\.' |
| 65 | + )}:(\d+:\d+):$`, |
| 66 | + 'gm' |
| 67 | + ), |
| 68 | + '// [$2] $1' |
| 69 | + ) |
| 70 | + .replace(/^([ >]) (?:\d+|\s+) \| /gm, '$1') |
| 71 | + .replace(/\n\n\n\d+ errors found\./, '\n'); |
| 72 | + |
| 73 | + try { |
| 74 | + // eslint-disable-next-line node/no-extraneous-require |
| 75 | + require('chalk').enabled = true; |
| 76 | + } catch (e) { |
| 77 | + /* */ |
| 78 | + } |
| 79 | + |
| 80 | + if (fs.existsSync(snapPath)) { |
| 81 | + const expected = fs.readFileSync(snapPath, 'utf8'); |
| 82 | + |
| 83 | + const changes = diff(expected, received); |
| 84 | + |
| 85 | + if (changes) { |
| 86 | + console.error(`${rule}: snapshots differ:`); |
| 87 | + console.log(changes); |
| 88 | + process.exit(1); |
| 89 | + } else { |
| 90 | + console.log(`${rule}: valid \u{2713}`); |
78 | 91 | }
|
79 |
| - |
80 |
| - if (fs.existsSync(snapPath)) { |
81 |
| - const expected = fs.readFileSync(snapPath, "utf8"); |
82 |
| - |
83 |
| - const changes = diff(expected, received); |
84 |
| - |
85 |
| - if (changes) { |
86 |
| - console.error(`${rule}: snapshots differ:`); |
87 |
| - console.log(changes); |
88 |
| - process.exit(1); |
89 |
| - } else { |
90 |
| - console.log(`${rule}: valid \u{2713}`); |
91 |
| - } |
| 92 | + } else { |
| 93 | + if (process.env.CI) { |
| 94 | + console.error(`${rule}: missing snapshot, not creating one in CI`); |
| 95 | + process.exit(1); |
92 | 96 | } else {
|
93 |
| - if (process.env.CI) { |
94 |
| - console.error(`${rule}: missing snapshot, not creating one in CI`); |
95 |
| - process.exit(1); |
96 |
| - } else { |
97 |
| - fs.writeFileSync(snapPath, received); |
98 |
| - console.log(`${rule}: created new snapshot \u{2713}`); |
99 |
| - } |
| 97 | + fs.writeFileSync(snapPath, received); |
| 98 | + console.log(`${rule}: created new snapshot \u{2713}`); |
100 | 99 | }
|
| 100 | + } |
101 | 101 | }
|
0 commit comments