generated from u3u/ts-lib-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
no-export-default-arrow
rule
- Loading branch information
Showing
8 changed files
with
218 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Disallow export default anonymous arrow function<br/>_**Automatically fix using the current file name.**_ (`arrow-return-style/no-export-default-arrow`) | ||
|
||
⚠️ This rule _warns_ in the ✅ `recommended` config. | ||
|
||
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
If the default export is an anonymous arrow function, it will automatically be converted to a named function using the current file name for exporting. | ||
|
||
```ts | ||
export default () => { | ||
// ... | ||
}; | ||
|
||
// ↓↓↓↓↓↓↓↓↓↓ | ||
|
||
const autoFixToCurrentFileName = () => { | ||
// ... | ||
}; | ||
|
||
export default autoFixToCurrentFileName; | ||
``` |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,113 @@ | ||
import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
import dedent from 'dedent'; | ||
import { afterAll, describe, it } from 'vitest'; | ||
import { noExportDefaultArrowRule, RULE_NAME } from './no-export-default-arrow'; | ||
|
||
RuleTester.afterAll = afterAll; | ||
RuleTester.describe = describe; | ||
RuleTester.it = it; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
|
||
parserOptions: { | ||
ecmaFeatures: { jsx: true }, | ||
}, | ||
}); | ||
|
||
ruleTester.run(RULE_NAME, noExportDefaultArrowRule, { | ||
invalid: [ | ||
{ | ||
code: dedent` | ||
import { useState } from 'react' | ||
export default () => { | ||
const [, update] = useState({}) | ||
const forceUpdate = () => { | ||
update({}) | ||
} | ||
return forceUpdate | ||
} | ||
`, | ||
|
||
errors: [{ messageId: 'disallowExportDefaultArrow' }], | ||
|
||
filename: 'useForceUpdate.ts', | ||
|
||
output: dedent` | ||
import { useState } from 'react' | ||
const useForceUpdate = () => { | ||
const [, update] = useState({}) | ||
const forceUpdate = () => { | ||
update({}) | ||
} | ||
return forceUpdate | ||
} | ||
export default useForceUpdate | ||
`, | ||
}, | ||
|
||
{ | ||
code: dedent` | ||
export default () => {} | ||
export const foo = () => 'foo' | ||
`, | ||
|
||
errors: [{ messageId: 'disallowExportDefaultArrow' }], | ||
|
||
filename: 'use-mouse.tsx', | ||
|
||
output: dedent` | ||
const useMouse = () => {} | ||
export const foo = () => 'foo' | ||
export default useMouse | ||
`, | ||
}, | ||
|
||
{ | ||
code: dedent` | ||
export default () => 1 | ||
// line comment | ||
/* block comment */ | ||
`, | ||
|
||
errors: [{ messageId: 'disallowExportDefaultArrow' }], | ||
|
||
filename: 'just_for_fun.js', | ||
|
||
output: dedent` | ||
const justForFun = () => 1 | ||
// line comment | ||
/* block comment */ | ||
export default justForFun | ||
`, | ||
}, | ||
], | ||
|
||
valid: [ | ||
dedent` | ||
const foo = () => { | ||
return 'foo' | ||
} | ||
export default foo | ||
`, | ||
|
||
'const now = () => Date.now()', | ||
'export const useQuery = () => {}', | ||
], | ||
}); |
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,64 @@ | ||
import path from 'node:path'; | ||
import { AST_NODE_TYPES, ASTUtils, type TSESTree } from '@typescript-eslint/utils'; | ||
import { camelCase } from 'scule'; | ||
import { createRule } from '../utils/create-rule'; | ||
|
||
export const RULE_NAME = 'no-export-default-arrow'; | ||
|
||
export const noExportDefaultArrowRule = createRule({ | ||
create: (context) => { | ||
const sourceCode = context.getSourceCode(); | ||
let program: TSESTree.Program; | ||
|
||
return { | ||
ArrowFunctionExpression: (arrowFunction) => { | ||
const { body: arrowBody, parent: arrowFunctionParent } = arrowFunction; | ||
|
||
if (arrowFunctionParent.type === AST_NODE_TYPES.ExportDefaultDeclaration) { | ||
context.report({ | ||
fix: (fixer) => { | ||
const fixes = []; | ||
const lastToken = sourceCode.getLastToken(program, { includeComments: true }) || arrowFunctionParent; | ||
const fileName = context.getPhysicalFilename?.() || context.getFilename() || 'namedFunction'; | ||
const { name: fileNameWithoutExtension } = path.parse(fileName); | ||
const funcName = camelCase(fileNameWithoutExtension); | ||
|
||
fixes.push( | ||
fixer.replaceText(arrowFunctionParent, `const ${funcName} = ${sourceCode.getText(arrowFunction)}`), | ||
fixer.insertTextAfter(lastToken, `\n\nexport default ${funcName}`) | ||
); | ||
|
||
return fixes; | ||
}, | ||
|
||
messageId: 'disallowExportDefaultArrow', | ||
node: arrowFunction, | ||
}); | ||
} | ||
}, | ||
|
||
Program: (node) => (program = node), | ||
}; | ||
}, | ||
|
||
defaultOptions: [], | ||
|
||
meta: { | ||
docs: { | ||
description: | ||
'Disallow export default anonymous arrow function<br/>_**Automatically fix using the current file name.**_', | ||
}, | ||
|
||
fixable: 'code', | ||
|
||
messages: { | ||
disallowExportDefaultArrow: 'Disallow export default anonymous arrow function', | ||
}, | ||
|
||
schema: [], | ||
|
||
type: 'suggestion', | ||
}, | ||
|
||
name: RULE_NAME, | ||
}); |