-
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.
Added no-exported-types-in-tsx-files
- Loading branch information
1 parent
2bdfea6
commit aefc977
Showing
7 changed files
with
172 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"tabWidth": 4, | ||
"useTabs": false, | ||
"trailingComma": "es5", | ||
"arrowParens": "always" | ||
} |
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
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,40 @@ | ||
import { ESLintUtils, TSESTree } from "@typescript-eslint/utils"; | ||
|
||
export const noExportedTypesInTsxFiles = ESLintUtils.RuleCreator.withoutDocs({ | ||
create(context) { | ||
const filename = context.filename; | ||
const isTsxFile = filename.endsWith(".tsx"); | ||
|
||
return { | ||
TSInterfaceDeclaration(node: TSESTree.TSInterfaceDeclaration) { | ||
checkExportedType(node); | ||
}, | ||
TSTypeAliasDeclaration(node: TSESTree.TSTypeAliasDeclaration) { | ||
checkExportedType(node); | ||
}, | ||
TSEnumDeclaration(node: TSESTree.TSEnumDeclaration) { | ||
checkExportedType(node); | ||
}, | ||
}; | ||
|
||
function checkExportedType(node: TSESTree.Node) { | ||
const isExported = node.parent?.type === "ExportNamedDeclaration"; | ||
|
||
if (isExported && isTsxFile) { | ||
context.report({ | ||
node, | ||
messageId: "noExportedTypesInTsxFiles", | ||
}); | ||
} | ||
} | ||
}, | ||
meta: { | ||
type: "problem", | ||
messages: { | ||
noExportedTypesInTsxFiles: | ||
"Exported types are not allowed in '.tsx' files.", | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
}); |
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,70 @@ | ||
import { RuleTester } from "@typescript-eslint/rule-tester"; | ||
import * as vitest from "vitest"; | ||
import { noExportedTypesInTsxFiles } from "../rules/noExportedTypesInTsxFiles"; | ||
|
||
RuleTester.afterAll = vitest.afterAll; | ||
RuleTester.it = vitest.it; | ||
RuleTester.itOnly = vitest.it.only; | ||
RuleTester.describe = vitest.describe; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: "@typescript-eslint/parser", | ||
}); | ||
|
||
ruleTester.run("noExportedTypesInTsxFiles", noExportedTypesInTsxFiles, { | ||
valid: [ | ||
{ | ||
code: ` | ||
export interface MyInterface { | ||
prop: string; | ||
} | ||
`, | ||
filename: "types.ts", | ||
parserOptions: { ecmaVersion: 2020, sourceType: "module" }, | ||
}, | ||
{ | ||
code: ` | ||
interface MyInterface { | ||
prop: string; | ||
} | ||
`, | ||
filename: "component.tsx", | ||
parserOptions: { ecmaVersion: 2020, sourceType: "module" }, | ||
}, | ||
{ | ||
code: ` | ||
export type MyType = string; | ||
`, | ||
filename: "someFile.ts", | ||
parserOptions: { ecmaVersion: 2020, sourceType: "module" }, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
export interface MyInterface { | ||
prop: string; | ||
} | ||
`, | ||
filename: "component.tsx", | ||
parserOptions: { ecmaVersion: 2020, sourceType: "module" }, | ||
errors: [ | ||
{ | ||
messageId: "noExportedTypesInTsxFiles", | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
export type MyType = string; | ||
`, | ||
filename: "component.tsx", | ||
parserOptions: { ecmaVersion: 2020, sourceType: "module" }, | ||
errors: [ | ||
{ | ||
messageId: "noExportedTypesInTsxFiles", | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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