Skip to content

Commit 7da76f6

Browse files
authored
test: migrate some test cases (#47)
1 parent 0742e7c commit 7da76f6

File tree

8 files changed

+202
-133
lines changed

8 files changed

+202
-133
lines changed

src/export-map.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,8 +847,11 @@ export class ExportMap {
847847

848848
reportErrors(
849849
context: RuleContext,
850-
declaration: { source: TSESTree.Literal },
850+
declaration: { source: TSESTree.Literal | null },
851851
) {
852+
if (!declaration.source) {
853+
throw new Error('declaration.source is null')
854+
}
852855
const msg = this.errors
853856
.map(err => `${err.message} (${err.lineNumber}:${err.column})`)
854857
.join(', ')

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import type { PluginConfig } from './types'
44

55
import noUnresolved from './rules/no-unresolved'
66
import named from './rules/named'
7+
import default_ from './rules/default'
78

89
export const rules = {
910
'no-unresolved': noUnresolved,
1011
named,
11-
default: require('./rules/default'),
12+
default: default_,
1213
namespace: require('./rules/namespace'),
1314
'no-namespace': require('./rules/no-namespace'),
1415
export: require('./rules/export'),

src/rules/default.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/rules/default.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { TSESTree } from '@typescript-eslint/utils'
2+
import { ExportMap } from '../export-map'
3+
import { createRule } from '../utils'
4+
5+
type MessageId = 'noDefaultExport'
6+
7+
export = createRule<[], MessageId>({
8+
name: 'default',
9+
meta: {
10+
type: 'problem',
11+
docs: {
12+
category: 'Static analysis',
13+
description:
14+
'Ensure a default export is present, given a default import.',
15+
recommended: 'warn',
16+
},
17+
schema: [],
18+
messages: {
19+
noDefaultExport:
20+
'No default export found in imported module "{{module}}".',
21+
},
22+
},
23+
defaultOptions: [],
24+
create(context) {
25+
function checkDefault(
26+
specifierType: 'ImportDefaultSpecifier' | 'ExportDefaultSpecifier',
27+
node: TSESTree.ImportDeclaration | TSESTree.ExportNamedDeclaration,
28+
) {
29+
const defaultSpecifier = (
30+
node.specifiers as Array<
31+
TSESTree.ImportClause | TSESTree.ExportSpecifier
32+
>
33+
).find(specifier => specifier.type === specifierType)
34+
35+
if (!defaultSpecifier) {
36+
return
37+
}
38+
const imports = ExportMap.get(node.source!.value, context)
39+
if (imports == null) {
40+
return
41+
}
42+
43+
if (imports.errors.length) {
44+
imports.reportErrors(context, node)
45+
} else if (imports.get('default') === undefined) {
46+
context.report({
47+
node: defaultSpecifier,
48+
messageId: 'noDefaultExport',
49+
data: {
50+
module: node.source!.value,
51+
},
52+
})
53+
}
54+
}
55+
56+
return {
57+
ImportDeclaration: checkDefault.bind(null, 'ImportDefaultSpecifier'),
58+
ExportNamedDeclaration: checkDefault.bind(null, 'ExportDefaultSpecifier'),
59+
}
60+
},
61+
})

test/rules/default.spec.js renamed to test/rules/default.spec.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import path from 'path'
2-
import { test, testVersion, SYNTAX_CASES, parsers } from '../utils'
3-
import { RuleTester } from 'eslint'
2+
3+
import { TSESLint } from '@typescript-eslint/utils'
44

55
import { CASE_SENSITIVE_FS } from '../../src/utils/resolve'
6+
import rule from '../../src/rules/default'
7+
import { test, testVersion, SYNTAX_CASES, parsers } from '../utils'
68

7-
const ruleTester = new RuleTester()
8-
const rule = require('rules/default')
9+
const ruleTester = new TSESLint.RuleTester()
910

1011
ruleTester.run('default', rule, {
11-
valid: [].concat(
12+
valid: [
1213
test({ code: 'import "./malformed.js"' }),
1314

1415
test({ code: 'import foo from "./empty-folder";' }),
@@ -96,15 +97,15 @@ ruleTester.run('default', rule, {
9697
}),
9798

9899
// es2022: Arbitrary module namespace identifier names
99-
testVersion('>= 8.7', () => ({
100+
...testVersion('>= 8.7', () => ({
100101
code: 'export { "default" as bar } from "./bar"',
101102
parserOptions: {
102103
ecmaVersion: 2022,
103104
},
104105
})),
105106

106107
...SYNTAX_CASES,
107-
),
108+
],
108109

109110
invalid: [
110111
test({
@@ -180,7 +181,7 @@ if (!CASE_SENSITIVE_FS) {
180181
describe('TypeScript', () => {
181182
const parser = parsers.TS
182183
ruleTester.run(`default`, rule, {
183-
valid: [].concat(
184+
valid: [
184185
test({
185186
code: `import foobar from "./typescript-default"`,
186187
parser,
@@ -285,7 +286,7 @@ describe('TypeScript', () => {
285286
'import-x/resolver': { 'eslint-import-resolver-typescript': true },
286287
},
287288
}),
288-
),
289+
],
289290

290291
invalid: [
291292
test({

test/rules/named.spec.js renamed to test/rules/named.spec.ts

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1+
import path from 'path'
2+
3+
import { TSESLint, TSESTree } from '@typescript-eslint/utils'
4+
5+
import { CASE_SENSITIVE_FS } from '../../src/utils/resolve'
6+
17
import {
28
test,
39
SYNTAX_CASES,
410
testFilePath,
511
testVersion,
612
parsers,
713
} from '../utils'
8-
import { RuleTester } from 'eslint'
9-
import path from 'path'
1014

11-
import { CASE_SENSITIVE_FS } from '../../src/utils/resolve'
15+
import rule from '../../src/rules/named'
1216

13-
const ruleTester = new RuleTester()
14-
const rule = require('rules/named')
17+
const ruleTester = new TSESLint.RuleTester()
1518

16-
function error(name, module, type = 'Identifier') {
19+
function error(
20+
name: string,
21+
module: string,
22+
type: `${TSESTree.AST_NODE_TYPES}` = 'Identifier',
23+
) {
1724
return { message: `${name} not found in '${module}'`, type }
1825
}
1926

@@ -195,40 +202,39 @@ ruleTester.run('named', rule, {
195202

196203
...SYNTAX_CASES,
197204

198-
...[].concat(
199-
testVersion('>= 6', () => ({
200-
code: `import { ExtfieldModel, Extfield2Model } from './models';`,
201-
filename: testFilePath('./export-star/downstream.js'),
202-
parserOptions: {
203-
sourceType: 'module',
204-
ecmaVersion: 2020,
205-
},
206-
})),
207-
208-
testVersion('>=7.8.0', () => ({
209-
code: 'const { something } = require("./dynamic-import-in-commonjs")',
210-
parserOptions: { ecmaVersion: 2021 },
211-
options: [{ commonjs: true }],
212-
})),
213-
214-
testVersion('>=7.8.0', () => ({
215-
code: 'import { something } from "./dynamic-import-in-commonjs"',
216-
parserOptions: { ecmaVersion: 2021 },
217-
})),
218-
219-
// es2022: Arbitrary module namespace identifier names
220-
testVersion('>= 8.7', () => ({
221-
code: 'import { "foo" as foo } from "./bar"',
222-
parserOptions: { ecmaVersion: 2022 },
223-
})),
224-
testVersion('>= 8.7', () => ({
225-
code: 'import { "foo" as foo } from "./empty-module"',
226-
parserOptions: { ecmaVersion: 2022 },
227-
})),
228-
),
205+
...testVersion('>= 6', () => ({
206+
code: `import { ExtfieldModel, Extfield2Model } from './models';`,
207+
filename: testFilePath('./export-star/downstream.js'),
208+
parserOptions: {
209+
sourceType: 'module',
210+
ecmaVersion: 2020,
211+
},
212+
})),
213+
214+
...testVersion('>=7.8.0', () => ({
215+
code: 'const { something } = require("./dynamic-import-in-commonjs")',
216+
parserOptions: { ecmaVersion: 2021 },
217+
options: [{ commonjs: true }],
218+
})),
219+
220+
...testVersion('>=7.8.0', () => ({
221+
code: 'import { something } from "./dynamic-import-in-commonjs"',
222+
parserOptions: { ecmaVersion: 2021 },
223+
})),
224+
225+
// es2022: Arbitrary module namespace identifier names
226+
...testVersion('>= 8.7', () => ({
227+
code: 'import { "foo" as foo } from "./bar"',
228+
parserOptions: { ecmaVersion: 2022 },
229+
})),
230+
231+
...testVersion('>= 8.7', () => ({
232+
code: 'import { "foo" as foo } from "./empty-module"',
233+
parserOptions: { ecmaVersion: 2022 },
234+
})),
229235
],
230236

231-
invalid: [].concat(
237+
invalid: [
232238
test({
233239
code: 'import { somethingElse } from "./test-module"',
234240
errors: [error('somethingElse', './test-module')],
@@ -368,25 +374,25 @@ ruleTester.run('named', rule, {
368374
}),
369375

370376
// es2022: Arbitrary module namespace identifier names
371-
testVersion('>= 8.7', () => ({
377+
...testVersion('>= 8.7', () => ({
372378
code: 'import { "somethingElse" as somethingElse } from "./test-module"',
373379
errors: [error('somethingElse', './test-module', 'Literal')],
374380
parserOptions: { ecmaVersion: 2022 },
375381
})),
376-
testVersion('>= 8.7', () => ({
382+
...testVersion('>= 8.7', () => ({
377383
code: 'import { "baz" as baz, "bop" as bop } from "./bar"',
378384
errors: [
379385
error('baz', './bar', 'Literal'),
380386
error('bop', './bar', 'Literal'),
381387
],
382388
parserOptions: { ecmaVersion: 2022 },
383389
})),
384-
testVersion('>= 8.7', () => ({
390+
...testVersion('>= 8.7', () => ({
385391
code: 'import { "default" as barDefault } from "./re-export"',
386392
errors: [`default not found in './re-export'`],
387393
parserOptions: { ecmaVersion: 2022 },
388394
})),
389-
),
395+
],
390396
})
391397

392398
// #311: import of mismatched case

0 commit comments

Comments
 (0)