|
| 1 | +import { RuleTester as TSESLintRuleTester } from '@typescript-eslint/rule-tester' |
| 2 | +import type { TSESLint } from '@typescript-eslint/utils' |
| 3 | + |
| 4 | +import { |
| 5 | + createRuleTestCaseFunctions, |
| 6 | + getNonDefaultParsers, |
| 7 | + parsers, |
| 8 | + testFilePath, |
| 9 | +} from '../utils.js' |
| 10 | + |
| 11 | +import { cjsRequire as require } from 'eslint-plugin-import-x' |
| 12 | +import rule from 'eslint-plugin-import-x/rules/prefer-namespace-import' |
| 13 | + |
| 14 | +const ruleTester = new TSESLintRuleTester() |
| 15 | + |
| 16 | +const { tValid, tInvalid } = createRuleTestCaseFunctions<typeof rule>() |
| 17 | + |
| 18 | +const options = [{ patterns: ['/^@scope/', '/^prefix-/', 'specific'] }] as const |
| 19 | + |
| 20 | +ruleTester.run('prefer-namespace-import', rule, { |
| 21 | + valid: [ |
| 22 | + tValid({ |
| 23 | + code: `import * as Name from '@scope/name';`, |
| 24 | + }), |
| 25 | + tValid({ |
| 26 | + code: `import * as Name from 'prefix-name';`, |
| 27 | + }), |
| 28 | + tValid({ |
| 29 | + code: `import * as Name from 'specific';`, |
| 30 | + }), |
| 31 | + tValid({ |
| 32 | + code: ` |
| 33 | + import * as Name1 from '@scope/name'; |
| 34 | + import * as Name2 from 'prefix-name'; |
| 35 | + import * as Name2 from 'specific'; |
| 36 | + `, |
| 37 | + }), |
| 38 | + tValid({ |
| 39 | + code: `import Name from 'other-name';`, |
| 40 | + options, |
| 41 | + }), |
| 42 | + ], |
| 43 | + invalid: [ |
| 44 | + tInvalid({ |
| 45 | + code: ` |
| 46 | +import Name1 from '@scope/name'; |
| 47 | +import Name2 from 'prefix-name'; |
| 48 | +import Name3 from 'prefix-name' with { type: 'json' }; |
| 49 | +import Name4, { name4 } from 'prefix-name' with { type: 'json' }; |
| 50 | +import Name5 from 'specific'; |
| 51 | +import Name6 from 'other-name'; |
| 52 | + `, |
| 53 | + errors: [ |
| 54 | + { |
| 55 | + messageId: 'preferNamespaceImport', |
| 56 | + data: { source: '@scope/name', specifier: 'Name1' }, |
| 57 | + }, |
| 58 | + { |
| 59 | + messageId: 'preferNamespaceImport', |
| 60 | + data: { source: 'prefix-name', specifier: 'Name2' }, |
| 61 | + }, |
| 62 | + { |
| 63 | + messageId: 'preferNamespaceImport', |
| 64 | + data: { source: 'prefix-name', specifier: 'Name3' }, |
| 65 | + }, |
| 66 | + { |
| 67 | + messageId: 'preferNamespaceImport', |
| 68 | + data: { source: 'prefix-name', specifier: 'Name4' }, |
| 69 | + }, |
| 70 | + { |
| 71 | + messageId: 'preferNamespaceImport', |
| 72 | + data: { source: 'specific', specifier: 'Name5' }, |
| 73 | + }, |
| 74 | + ], |
| 75 | + options, |
| 76 | + output: ` |
| 77 | +import * as Name1 from '@scope/name'; |
| 78 | +import * as Name2 from 'prefix-name'; |
| 79 | +import * as Name3 from 'prefix-name' with { type: 'json' }; |
| 80 | +import * as Name4 from 'prefix-name' with { type: 'json' }; |
| 81 | +import { name4 } from 'prefix-name' with { type: 'json' }; |
| 82 | +import * as Name5 from 'specific'; |
| 83 | +import Name6 from 'other-name'; |
| 84 | + `, |
| 85 | + }), |
| 86 | + ], |
| 87 | +}) |
| 88 | + |
| 89 | +describe('TypeScript', () => { |
| 90 | + for (const parser of getNonDefaultParsers()) { |
| 91 | + const parserConfig = { |
| 92 | + languageOptions: { |
| 93 | + ...(parser === parsers.BABEL && { |
| 94 | + parser: require<TSESLint.Parser.LooseParserModule>(parsers.BABEL), |
| 95 | + }), |
| 96 | + }, |
| 97 | + filename: testFilePath('foo.ts'), |
| 98 | + } |
| 99 | + |
| 100 | + ruleTester.run('prefer-namespace-import', rule, { |
| 101 | + valid: [ |
| 102 | + tValid({ |
| 103 | + code: ` |
| 104 | + import type * as Name1 from '@scope/name'; |
| 105 | + import type * as Name2 from 'prefix-name'; |
| 106 | + `, |
| 107 | + ...parserConfig, |
| 108 | + }), |
| 109 | + tValid({ |
| 110 | + code: `import type Name from 'other-name';`, |
| 111 | + options, |
| 112 | + ...parserConfig, |
| 113 | + }), |
| 114 | + ], |
| 115 | + invalid: [ |
| 116 | + tInvalid({ |
| 117 | + code: ` |
| 118 | +import type Name1 from '@scope/name'; |
| 119 | +import type Name2 from 'prefix-name'; |
| 120 | +import type Name3 from 'prefix-name' with { type: 'json' }; |
| 121 | +import Name4, { type name4 } from 'prefix-name' with { type: 'json' }; |
| 122 | +import type Name5 from 'specific'; |
| 123 | +import type Name6 from 'other-name'; |
| 124 | + `, |
| 125 | + errors: [ |
| 126 | + { |
| 127 | + messageId: 'preferNamespaceImport', |
| 128 | + data: { source: '@scope/name', specifier: 'Name1' }, |
| 129 | + }, |
| 130 | + { |
| 131 | + messageId: 'preferNamespaceImport', |
| 132 | + data: { source: 'prefix-name', specifier: 'Name2' }, |
| 133 | + }, |
| 134 | + { |
| 135 | + messageId: 'preferNamespaceImport', |
| 136 | + data: { source: 'prefix-name', specifier: 'Name3' }, |
| 137 | + }, |
| 138 | + { |
| 139 | + messageId: 'preferNamespaceImport', |
| 140 | + data: { source: 'prefix-name', specifier: 'Name4' }, |
| 141 | + }, |
| 142 | + { |
| 143 | + messageId: 'preferNamespaceImport', |
| 144 | + data: { source: 'specific', specifier: 'Name5' }, |
| 145 | + }, |
| 146 | + ], |
| 147 | + options, |
| 148 | + output: ` |
| 149 | +import type * as Name1 from '@scope/name'; |
| 150 | +import type * as Name2 from 'prefix-name'; |
| 151 | +import type * as Name3 from 'prefix-name' with { type: 'json' }; |
| 152 | +import * as Name4 from 'prefix-name' with { type: 'json' }; |
| 153 | +import { type name4 } from 'prefix-name' with { type: 'json' }; |
| 154 | +import type * as Name5 from 'specific'; |
| 155 | +import type Name6 from 'other-name'; |
| 156 | + `, |
| 157 | + ...parserConfig, |
| 158 | + }), |
| 159 | + ], |
| 160 | + }) |
| 161 | + } |
| 162 | +}) |
0 commit comments