Skip to content

Commit

Permalink
test: migrate some test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Mar 13, 2024
1 parent 0742e7c commit 0a73f73
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 133 deletions.
5 changes: 4 additions & 1 deletion src/export-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,11 @@ export class ExportMap {

reportErrors(
context: RuleContext,
declaration: { source: TSESTree.Literal },
declaration: { source: TSESTree.Literal | null },
) {
if (!declaration.source) {
throw new Error('declaration.source is null')

Check warning on line 853 in src/export-map.ts

View check run for this annotation

Codecov / codecov/patch

src/export-map.ts#L853

Added line #L853 was not covered by tests
}
const msg = this.errors
.map(err => `${err.message} (${err.lineNumber}:${err.column})`)
.join(', ')
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import type { PluginConfig } from './types'

import noUnresolved from './rules/no-unresolved'
import named from './rules/named'
import default_ from './rules/default'

export const rules = {
'no-unresolved': noUnresolved,
named,
default: require('./rules/default'),
default: default_,
namespace: require('./rules/namespace'),
'no-namespace': require('./rules/no-namespace'),
export: require('./rules/export'),
Expand Down
45 changes: 0 additions & 45 deletions src/rules/default.js

This file was deleted.

61 changes: 61 additions & 0 deletions src/rules/default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { TSESTree } from '@typescript-eslint/utils'
import { ExportMap } from '../export-map'
import { createRule } from '../utils'

type MessageId = 'noDefaultExport'

export = createRule<[], MessageId>({
name: 'default',
meta: {
type: 'problem',
docs: {
category: 'Static analysis',
description:
'Ensure a default export is present, given a default import.',
recommended: 'warn',
},
schema: [],
messages: {
noDefaultExport:
'No default export found in imported module "{{module}}".',
},
},
defaultOptions: [],
create(context) {
function checkDefault(
specifierType: 'ImportDefaultSpecifier' | 'ExportDefaultSpecifier',
node: TSESTree.ImportDeclaration | TSESTree.ExportNamedDeclaration,
) {
const defaultSpecifier = (
node.specifiers as Array<
TSESTree.ImportClause | TSESTree.ExportSpecifier
>
).find(specifier => specifier.type === specifierType)

if (!defaultSpecifier) {
return
}
const imports = ExportMap.get(node.source!.value, context)
if (imports == null) {
return
}

if (imports.errors.length) {
imports.reportErrors(context, node)
} else if (imports.get('default') === undefined) {
context.report({
node: defaultSpecifier,
messageId: 'noDefaultExport',
data: {
module: node.source!.value,
},
})
}
}

return {
ImportDeclaration: checkDefault.bind(null, 'ImportDefaultSpecifier'),
ExportNamedDeclaration: checkDefault.bind(null, 'ExportDefaultSpecifier'),
}
},
})
19 changes: 10 additions & 9 deletions test/rules/default.spec.js → test/rules/default.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import path from 'path'
import { test, testVersion, SYNTAX_CASES, parsers } from '../utils'
import { RuleTester } from 'eslint'

import { TSESLint } from '@typescript-eslint/utils'

import { CASE_SENSITIVE_FS } from '../../src/utils/resolve'
import rule from '../../src/rules/default'
import { test, testVersion, SYNTAX_CASES, parsers } from '../utils'

const ruleTester = new RuleTester()
const rule = require('rules/default')
const ruleTester = new TSESLint.RuleTester()

ruleTester.run('default', rule, {
valid: [].concat(
valid: [
test({ code: 'import "./malformed.js"' }),

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

// es2022: Arbitrary module namespace identifier names
testVersion('>= 8.7', () => ({
...testVersion('>= 8.7', () => ({
code: 'export { "default" as bar } from "./bar"',
parserOptions: {
ecmaVersion: 2022,
},
})),

...SYNTAX_CASES,
),
],

invalid: [
test({
Expand Down Expand Up @@ -180,7 +181,7 @@ if (!CASE_SENSITIVE_FS) {
describe('TypeScript', () => {
const parser = parsers.TS
ruleTester.run(`default`, rule, {
valid: [].concat(
valid: [
test({
code: `import foobar from "./typescript-default"`,
parser,
Expand Down Expand Up @@ -285,7 +286,7 @@ describe('TypeScript', () => {
'import-x/resolver': { 'eslint-import-resolver-typescript': true },
},
}),
),
],

invalid: [
test({
Expand Down
90 changes: 48 additions & 42 deletions test/rules/named.spec.js → test/rules/named.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import path from 'path'

import { TSESLint, TSESTree } from '@typescript-eslint/utils'

import { CASE_SENSITIVE_FS } from '../../src/utils/resolve'

import {
test,
SYNTAX_CASES,
testFilePath,
testVersion,
parsers,
} from '../utils'
import { RuleTester } from 'eslint'
import path from 'path'

import { CASE_SENSITIVE_FS } from '../../src/utils/resolve'
import rule from '../../src/rules/named'

const ruleTester = new RuleTester()
const rule = require('rules/named')
const ruleTester = new TSESLint.RuleTester()

function error(name, module, type = 'Identifier') {
function error(
name: string,
module: string,
type: `${TSESTree.AST_NODE_TYPES}` = 'Identifier',
) {
return { message: `${name} not found in '${module}'`, type }
}

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

...SYNTAX_CASES,

...[].concat(
testVersion('>= 6', () => ({
code: `import { ExtfieldModel, Extfield2Model } from './models';`,
filename: testFilePath('./export-star/downstream.js'),
parserOptions: {
sourceType: 'module',
ecmaVersion: 2020,
},
})),

testVersion('>=7.8.0', () => ({
code: 'const { something } = require("./dynamic-import-in-commonjs")',
parserOptions: { ecmaVersion: 2021 },
options: [{ commonjs: true }],
})),

testVersion('>=7.8.0', () => ({
code: 'import { something } from "./dynamic-import-in-commonjs"',
parserOptions: { ecmaVersion: 2021 },
})),

// es2022: Arbitrary module namespace identifier names
testVersion('>= 8.7', () => ({
code: 'import { "foo" as foo } from "./bar"',
parserOptions: { ecmaVersion: 2022 },
})),
testVersion('>= 8.7', () => ({
code: 'import { "foo" as foo } from "./empty-module"',
parserOptions: { ecmaVersion: 2022 },
})),
),
...testVersion('>= 6', () => ({
code: `import { ExtfieldModel, Extfield2Model } from './models';`,
filename: testFilePath('./export-star/downstream.js'),
parserOptions: {
sourceType: 'module',
ecmaVersion: 2020,
},
})),

...testVersion('>=7.8.0', () => ({
code: 'const { something } = require("./dynamic-import-in-commonjs")',
parserOptions: { ecmaVersion: 2021 },
options: [{ commonjs: true }],
})),

...testVersion('>=7.8.0', () => ({
code: 'import { something } from "./dynamic-import-in-commonjs"',
parserOptions: { ecmaVersion: 2021 },
})),

// es2022: Arbitrary module namespace identifier names
...testVersion('>= 8.7', () => ({
code: 'import { "foo" as foo } from "./bar"',
parserOptions: { ecmaVersion: 2022 },
})),

...testVersion('>= 8.7', () => ({
code: 'import { "foo" as foo } from "./empty-module"',
parserOptions: { ecmaVersion: 2022 },
})),
],

invalid: [].concat(
invalid: [
test({
code: 'import { somethingElse } from "./test-module"',
errors: [error('somethingElse', './test-module')],
Expand Down Expand Up @@ -368,25 +374,25 @@ ruleTester.run('named', rule, {
}),

// es2022: Arbitrary module namespace identifier names
testVersion('>= 8.7', () => ({
...testVersion('>= 8.7', () => ({
code: 'import { "somethingElse" as somethingElse } from "./test-module"',
errors: [error('somethingElse', './test-module', 'Literal')],
parserOptions: { ecmaVersion: 2022 },
})),
testVersion('>= 8.7', () => ({
...testVersion('>= 8.7', () => ({
code: 'import { "baz" as baz, "bop" as bop } from "./bar"',
errors: [
error('baz', './bar', 'Literal'),
error('bop', './bar', 'Literal'),
],
parserOptions: { ecmaVersion: 2022 },
})),
testVersion('>= 8.7', () => ({
...testVersion('>= 8.7', () => ({
code: 'import { "default" as barDefault } from "./re-export"',
errors: [`default not found in './re-export'`],
parserOptions: { ecmaVersion: 2022 },
})),
),
],
})

// #311: import of mismatched case
Expand Down
Loading

0 comments on commit 0a73f73

Please sign in to comment.