eslint-plugin-erasable-syntax-only
Advanced tools
Comparing version
@@ -35,3 +35,2 @@ import { AST_NODE_TYPES } from "@typescript-eslint/utils"; | ||
}, | ||
fixable: "code", | ||
hasSuggestions: true, | ||
@@ -38,0 +37,0 @@ messages: { |
import { AST_NODE_TYPES } from "@typescript-eslint/utils"; | ||
import { CachedFactory } from "cached-factory"; | ||
import { createRule } from "../utils.js"; | ||
function skipExportParent(node) { | ||
return node.parent.type == AST_NODE_TYPES.ExportNamedDeclaration ? node.parent : node; | ||
} | ||
function skipModuleParent(node) { | ||
return node.parent.type === AST_NODE_TYPES.TSModuleDeclaration ? node.parent : node; | ||
} | ||
const rule = createRule({ | ||
create(context) { | ||
const hasValueStatementCache = new CachedFactory( | ||
(node) => !node.declare && node.id.type !== AST_NODE_TYPES.Literal && // https://github.com/typescript-eslint/typescript-eslint/issues/10486 | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
node.body.body.some?.(isValueStatement) | ||
); | ||
function isValueStatement(node) { | ||
switch (node.type) { | ||
case AST_NODE_TYPES.ExportNamedDeclaration: | ||
return !node.declaration || isValueStatement(node.declaration); | ||
case AST_NODE_TYPES.TSInterfaceDeclaration: | ||
case AST_NODE_TYPES.TSTypeAliasDeclaration: | ||
return false; | ||
case AST_NODE_TYPES.TSModuleDeclaration: | ||
return hasValueStatementCache.get(node); | ||
default: | ||
return true; | ||
} | ||
} | ||
return { | ||
TSModuleDeclaration(node) { | ||
if (!node.declare && node.id.type !== AST_NODE_TYPES.Literal && node.parent.type !== AST_NODE_TYPES.TSModuleDeclaration) { | ||
if (hasValueStatementCache.get(node) && skipExportParent(node).parent.type !== AST_NODE_TYPES.TSModuleBlock) { | ||
context.report({ | ||
messageId: "namespace", | ||
node | ||
node: skipModuleParent(node) | ||
}); | ||
@@ -12,0 +37,0 @@ } |
{ | ||
"name": "eslint-plugin-erasable-syntax-only", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "ESLint plugin to granularly enforce TypeScript's `erasableSyntaxOnly` flag. ❎", | ||
@@ -40,3 +40,4 @@ "repository": { | ||
"dependencies": { | ||
"@typescript-eslint/utils": "^8.24.0" | ||
"@typescript-eslint/utils": "^8.24.0", | ||
"cached-factory": "^0.1.0" | ||
}, | ||
@@ -56,2 +57,3 @@ "devDependencies": { | ||
"eslint-doc-generator": "2.0.2", | ||
"eslint-plugin-eslint-plugin": "6.4.0", | ||
"eslint-plugin-jsdoc": "50.6.3", | ||
@@ -58,0 +60,0 @@ "eslint-plugin-jsonc": "2.19.1", |
@@ -11,3 +11,3 @@ <h1 align="center">eslint-plugin-erasable-syntax-only</h1> | ||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> | ||
<a href="#contributors" target="_blank"><img alt="👪 All Contributors: 1" src="https://img.shields.io/badge/%F0%9F%91%AA_all_contributors-1-21bb42.svg" /></a> | ||
<a href="#contributors" target="_blank"><img alt="👪 All Contributors: 3" src="https://img.shields.io/badge/%F0%9F%91%AA_all_contributors-3-21bb42.svg" /></a> | ||
<!-- ALL-CONTRIBUTORS-BADGE:END --> | ||
@@ -48,9 +48,11 @@ <!-- prettier-ignore-end --> | ||
| Name | Description | | ||
| :--------------------------------------------------------- | :--------------------------------------------------- | | ||
| [enums](docs/rules/enums.md) | Avoid using TypeScript's enums. | | ||
| [import-aliases](docs/rules/import-aliases.md) | Avoid using TypeScript's import aliases. | | ||
| [namespaces](docs/rules/namespaces.md) | Avoid using TypeScript's namespaces. | | ||
| [parameter-properties](docs/rules/parameter-properties.md) | Avoid using TypeScript's class parameter properties. | | ||
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions). | ||
| Name | Description | 💡 | | ||
| :--------------------------------------------------------- | :--------------------------------------------------- | :-- | | ||
| [enums](docs/rules/enums.md) | Avoid using TypeScript's enums. | | | ||
| [import-aliases](docs/rules/import-aliases.md) | Avoid using TypeScript's import aliases. | 💡 | | ||
| [namespaces](docs/rules/namespaces.md) | Avoid using TypeScript's namespaces. | | | ||
| [parameter-properties](docs/rules/parameter-properties.md) | Avoid using TypeScript's class parameter properties. | | | ||
<!-- end auto-generated rules list --> | ||
@@ -119,2 +121,24 @@ | ||
## Contributors | ||
<!-- spellchecker: disable --> | ||
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> | ||
<!-- prettier-ignore-start --> | ||
<!-- markdownlint-disable --> | ||
<table> | ||
<tbody> | ||
<tr> | ||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/AlexMunoz"><img src="https://avatars.githubusercontent.com/u/3093946?v=4?s=100" width="100px;" alt="Alex Muñoz"/><br /><sub><b>Alex Muñoz</b></sub></a><br /><a href="https://github.com/JoshuaKGoldberg/eslint-plugin-erasable-syntax-only/commits?author=alexmunoz" title="Code">💻</a></td> | ||
<td align="center" valign="top" width="14.28%"><a href="https://jakebailey.dev"><img src="https://avatars.githubusercontent.com/u/5341706?v=4?s=100" width="100px;" alt="Jake Bailey"/><br /><sub><b>Jake Bailey</b></sub></a><br /><a href="https://github.com/JoshuaKGoldberg/eslint-plugin-erasable-syntax-only/issues?q=author%3Ajakebailey" title="Bug reports">🐛</a></td> | ||
<td align="center" valign="top" width="14.28%"><a href="http://www.joshuakgoldberg.com/"><img src="https://avatars.githubusercontent.com/u/3335181?v=4?s=100" width="100px;" alt="Josh Goldberg ✨"/><br /><sub><b>Josh Goldberg ✨</b></sub></a><br /><a href="https://github.com/JoshuaKGoldberg/eslint-plugin-erasable-syntax-only/commits?author=JoshuaKGoldberg" title="Code">💻</a> <a href="#content-JoshuaKGoldberg" title="Content">🖋</a> <a href="https://github.com/JoshuaKGoldberg/eslint-plugin-erasable-syntax-only/commits?author=JoshuaKGoldberg" title="Documentation">📖</a> <a href="#ideas-JoshuaKGoldberg" title="Ideas, Planning, & Feedback">🤔</a> <a href="#infra-JoshuaKGoldberg" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="#maintenance-JoshuaKGoldberg" title="Maintenance">🚧</a> <a href="#projectManagement-JoshuaKGoldberg" title="Project Management">📆</a> <a href="#tool-JoshuaKGoldberg" title="Tools">🔧</a> <a href="https://github.com/JoshuaKGoldberg/eslint-plugin-erasable-syntax-only/issues?q=author%3AJoshuaKGoldberg" title="Bug reports">🐛</a></td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
<!-- markdownlint-restore --> | ||
<!-- prettier-ignore-end --> | ||
<!-- ALL-CONTRIBUTORS-LIST:END --> | ||
<!-- spellchecker: enable --> | ||
> 💝 This package was templated with [`create-typescript-app`](https://github.com/JoshuaKGoldberg/create-typescript-app) using the [Bingo engine](https://create.bingo). |
21487
19.79%276
9.52%142
20.34%5
25%36
2.86%+ Added
+ Added