feat(ts): ban cjs exports in ts file (#167)

This commit is contained in:
曾明健 2023-05-19 16:29:54 +08:00 committed by GitHub
parent 96dd9a1797
commit 3ca0e7ea8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 136 additions and 0 deletions

View File

@ -158,6 +158,8 @@ module.exports = {
// antfu
'antfu/generic-spacing': 'error',
'antfu/no-cjs-exports': 'error',
'antfu/no-ts-export-equal': 'error',
// off
'@typescript-eslint/consistent-indexed-object-style': 'off',

View File

@ -3,6 +3,8 @@ import ifNewline from './rules/if-newline'
import importDedupe from './rules/import-dedupe'
import preferInlineTypeImport from './rules/prefer-inline-type-import'
import topLevelFunction from './rules/top-level-function'
import noTsExportEqual from './rules/no-ts-export-equal'
import noCjsExports from './rules/no-cjs-exports'
export default {
rules: {
@ -11,5 +13,7 @@ export default {
'prefer-inline-type-import': preferInlineTypeImport,
'generic-spacing': genericSpacing,
'top-level-function': topLevelFunction,
'no-cjs-exports': noCjsExports,
'no-ts-export-equal': noTsExportEqual,
},
}

View File

@ -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' }],
})),
})
})

View 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',
})
},
}
},
})

View File

@ -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' }],
})),
})
})

View 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',
})
},
}
},
})