feat(ts): ban cjs exports in ts file (#167)
This commit is contained in:
parent
96dd9a1797
commit
3ca0e7ea8b
@ -158,6 +158,8 @@ module.exports = {
|
|||||||
|
|
||||||
// antfu
|
// antfu
|
||||||
'antfu/generic-spacing': 'error',
|
'antfu/generic-spacing': 'error',
|
||||||
|
'antfu/no-cjs-exports': 'error',
|
||||||
|
'antfu/no-ts-export-equal': 'error',
|
||||||
|
|
||||||
// off
|
// off
|
||||||
'@typescript-eslint/consistent-indexed-object-style': 'off',
|
'@typescript-eslint/consistent-indexed-object-style': 'off',
|
||||||
|
@ -3,6 +3,8 @@ import ifNewline from './rules/if-newline'
|
|||||||
import importDedupe from './rules/import-dedupe'
|
import importDedupe from './rules/import-dedupe'
|
||||||
import preferInlineTypeImport from './rules/prefer-inline-type-import'
|
import preferInlineTypeImport from './rules/prefer-inline-type-import'
|
||||||
import topLevelFunction from './rules/top-level-function'
|
import topLevelFunction from './rules/top-level-function'
|
||||||
|
import noTsExportEqual from './rules/no-ts-export-equal'
|
||||||
|
import noCjsExports from './rules/no-cjs-exports'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
rules: {
|
rules: {
|
||||||
@ -11,5 +13,7 @@ export default {
|
|||||||
'prefer-inline-type-import': preferInlineTypeImport,
|
'prefer-inline-type-import': preferInlineTypeImport,
|
||||||
'generic-spacing': genericSpacing,
|
'generic-spacing': genericSpacing,
|
||||||
'top-level-function': topLevelFunction,
|
'top-level-function': topLevelFunction,
|
||||||
|
'no-cjs-exports': noCjsExports,
|
||||||
|
'no-ts-export-equal': noTsExportEqual,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -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' }],
|
||||||
|
})),
|
||||||
|
})
|
||||||
|
})
|
41
packages/eslint-plugin-antfu/src/rules/no-cjs-exports.ts
Normal file
41
packages/eslint-plugin-antfu/src/rules/no-cjs-exports.ts
Normal file
@ -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',
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
@ -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
packages/eslint-plugin-antfu/src/rules/no-ts-export-equal.ts
Normal file
35
packages/eslint-plugin-antfu/src/rules/no-ts-export-equal.ts
Normal file
@ -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',
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user