|
1 |
| -import { TSESLint, ESLintUtils } from '@typescript-eslint/experimental-utils'; |
2 |
| -import { clearCaches } from '@typescript-eslint/parser'; |
| 1 | +import { ESLintUtils } from '@typescript-eslint/experimental-utils'; |
3 | 2 | import * as path from 'path';
|
4 | 3 |
|
5 |
| -const parser = '@typescript-eslint/parser'; |
6 |
| - |
7 |
| -type RuleTesterConfig = Omit<TSESLint.RuleTesterConfig, 'parser'> & { |
8 |
| - parser: typeof parser; |
9 |
| -}; |
10 |
| -class RuleTester extends TSESLint.RuleTester { |
11 |
| - // as of eslint 6 you have to provide an absolute path to the parser |
12 |
| - // but that's not as clean to type, this saves us trying to manually enforce |
13 |
| - // that contributors require.resolve everything |
14 |
| - constructor(private readonly options: RuleTesterConfig) { |
15 |
| - super({ |
16 |
| - ...options, |
17 |
| - parser: require.resolve(options.parser), |
18 |
| - }); |
19 |
| - } |
20 |
| - private getFilename(options?: TSESLint.ParserOptions): string { |
21 |
| - if (options) { |
22 |
| - const filename = `file.ts${ |
23 |
| - options.ecmaFeatures && options.ecmaFeatures.jsx ? 'x' : '' |
24 |
| - }`; |
25 |
| - if (options.project) { |
26 |
| - return path.join(getFixturesRootDir(), filename); |
27 |
| - } |
28 |
| - |
29 |
| - return filename; |
30 |
| - } else if (this.options.parserOptions) { |
31 |
| - return this.getFilename(this.options.parserOptions); |
32 |
| - } |
33 |
| - |
34 |
| - return 'file.ts'; |
35 |
| - } |
36 |
| - |
37 |
| - // as of eslint 6 you have to provide an absolute path to the parser |
38 |
| - // If you don't do that at the test level, the test will fail somewhat cryptically... |
39 |
| - // This is a lot more explicit |
40 |
| - run<TMessageIds extends string, TOptions extends Readonly<unknown[]>>( |
41 |
| - name: string, |
42 |
| - rule: TSESLint.RuleModule<TMessageIds, TOptions>, |
43 |
| - tests: TSESLint.RunTests<TMessageIds, TOptions>, |
44 |
| - ): void { |
45 |
| - const errorMessage = `Do not set the parser at the test level unless you want to use a parser other than ${parser}`; |
46 |
| - |
47 |
| - // standardize the valid tests as objects |
48 |
| - tests.valid = tests.valid.map(test => { |
49 |
| - if (typeof test === 'string') { |
50 |
| - return { |
51 |
| - code: test, |
52 |
| - }; |
53 |
| - } |
54 |
| - return test; |
55 |
| - }); |
56 |
| - |
57 |
| - tests.valid.forEach(test => { |
58 |
| - if (typeof test !== 'string') { |
59 |
| - if (test.parser === parser) { |
60 |
| - throw new Error(errorMessage); |
61 |
| - } |
62 |
| - if (!test.filename) { |
63 |
| - test.filename = this.getFilename(test.parserOptions); |
64 |
| - } |
65 |
| - } |
66 |
| - }); |
67 |
| - tests.invalid.forEach(test => { |
68 |
| - if (test.parser === parser) { |
69 |
| - throw new Error(errorMessage); |
70 |
| - } |
71 |
| - if (!test.filename) { |
72 |
| - test.filename = this.getFilename(test.parserOptions); |
73 |
| - } |
74 |
| - }); |
75 |
| - |
76 |
| - super.run(name, rule, tests); |
77 |
| - } |
78 |
| -} |
79 |
| - |
80 | 4 | function getFixturesRootDir(): string {
|
81 |
| - return path.join(process.cwd(), 'tests/fixtures/'); |
| 5 | + return path.join(__dirname, 'fixtures'); |
82 | 6 | }
|
83 | 7 |
|
84 |
| -const { batchedSingleLineTests } = ESLintUtils; |
85 |
| - |
86 |
| -// make sure that the parser doesn't hold onto file handles between tests |
87 |
| -// on linux (i.e. our CI env), there can be very a limited number of watch handles available |
88 |
| -afterAll(() => { |
89 |
| - clearCaches(); |
90 |
| -}); |
91 |
| - |
92 |
| -/** |
93 |
| - * Simple no-op tag to mark code samples as "should not format with prettier" |
94 |
| - * for the internal/plugin-test-formatting lint rule |
95 |
| - */ |
96 |
| -function noFormat(strings: TemplateStringsArray, ...keys: string[]): string { |
97 |
| - const lastIndex = strings.length - 1; |
98 |
| - return ( |
99 |
| - strings.slice(0, lastIndex).reduce((p, s, i) => p + s + keys[i], '') + |
100 |
| - strings[lastIndex] |
101 |
| - ); |
102 |
| -} |
| 8 | +const { batchedSingleLineTests, RuleTester, noFormat } = ESLintUtils; |
103 | 9 |
|
104 | 10 | export { batchedSingleLineTests, getFixturesRootDir, noFormat, RuleTester };
|
0 commit comments