-
-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ts): ban cjs exports in ts file (#167)
- Loading branch information
1 parent
96dd9a1
commit 3ca0e7e
Showing
6 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/eslint-plugin-antfu/src/rules/no-cjs-exports.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { RuleTester } from '@typescript-eslint/utils/dist/ts-eslint' | ||
import { it } from 'vitest' | ||
import rule, { RULE_NAME } from './no-cjs-exports' | ||
|
||
const valids = [ | ||
{ code: 'export = {}', filename: 'test.ts' }, | ||
{ code: 'exports.a = {}', filename: 'test.js' }, | ||
{ code: 'module.exports.a = {}', filename: 'test.js' }, | ||
] | ||
|
||
const invalids = [ | ||
{ code: 'exports.a = {}', filename: 'test.ts' }, | ||
{ code: 'module.exports.a = {}', filename: 'test.ts' }, | ||
] | ||
|
||
it('runs', () => { | ||
const ruleTester: RuleTester = new RuleTester({ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
}) | ||
|
||
ruleTester.run(RULE_NAME, rule, { | ||
valid: valids, | ||
invalid: invalids.map(i => ({ | ||
...i, | ||
errors: [{ messageId: 'noCjsExports' }], | ||
})), | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { createEslintRule } from '../utils' | ||
|
||
export const RULE_NAME = 'no-cjs-exports' | ||
export type MessageIds = 'noCjsExports' | ||
export type Options = [] | ||
|
||
export default createEslintRule<Options, MessageIds>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Do not use CJS exports', | ||
recommended: false, | ||
}, | ||
schema: [], | ||
messages: { | ||
noCjsExports: 'Use ESM export instead', | ||
}, | ||
}, | ||
defaultOptions: [], | ||
create: (context) => { | ||
const extension = context.getFilename().split('.').pop() | ||
if (!['ts', 'tsx', 'mts', 'cts'].includes(extension)) | ||
return {} | ||
|
||
return { | ||
'MemberExpression[object.name="exports"]': function (node) { | ||
context.report({ | ||
node, | ||
messageId: 'noCjsExports', | ||
}) | ||
}, | ||
'MemberExpression[object.name="module"][property.name="exports"]': function (node) { | ||
context.report({ | ||
node, | ||
messageId: 'noCjsExports', | ||
}) | ||
}, | ||
} | ||
}, | ||
}) |
26 changes: 26 additions & 0 deletions
26
packages/eslint-plugin-antfu/src/rules/no-ts-export-equal.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { RuleTester } from '@typescript-eslint/utils/dist/ts-eslint' | ||
import { it } from 'vitest' | ||
import rule, { RULE_NAME } from './no-ts-export-equal' | ||
|
||
const valids = [ | ||
{ code: 'export default {}', filename: 'test.ts' }, | ||
{ code: 'export = {}', filename: 'test.js' }, | ||
] | ||
|
||
const invalids = [ | ||
{ code: 'export = {}', filename: 'test.ts' }, | ||
] | ||
|
||
it('runs', () => { | ||
const ruleTester: RuleTester = new RuleTester({ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
}) | ||
|
||
ruleTester.run(RULE_NAME, rule, { | ||
valid: valids, | ||
invalid: invalids.map(i => ({ | ||
...i, | ||
errors: [{ messageId: 'noTsExportEqual' }], | ||
})), | ||
}) | ||
}) |
35 changes: 35 additions & 0 deletions
35
packages/eslint-plugin-antfu/src/rules/no-ts-export-equal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { createEslintRule } from '../utils' | ||
|
||
export const RULE_NAME = 'no-ts-export-equal' | ||
export type MessageIds = 'noTsExportEqual' | ||
export type Options = [] | ||
|
||
export default createEslintRule<Options, MessageIds>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Do not use `exports =`', | ||
recommended: false, | ||
}, | ||
schema: [], | ||
messages: { | ||
noTsExportEqual: 'Use ESM `export default` instead', | ||
}, | ||
}, | ||
defaultOptions: [], | ||
create: (context) => { | ||
const extension = context.getFilename().split('.').pop() | ||
if (!['ts', 'tsx', 'mts', 'cts'].includes(extension)) | ||
return {} | ||
|
||
return { | ||
TSExportAssignment(node) { | ||
context.report({ | ||
node, | ||
messageId: 'noTsExportEqual', | ||
}) | ||
}, | ||
} | ||
}, | ||
}) |