-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Converted no-single-declare-module from TSLint to ESLint #655
Merged
sandersn
merged 6 commits into
microsoft:master
from
JoshuaKGoldberg:no-single-declare-module
Sep 27, 2023
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d5e6c7d
Converted no-single-declare-module from TSLint to ESLint
JoshuaKGoldberg 7eee69d
lint fix
JoshuaKGoldberg 3ca43c7
Merge branch 'master' into no-single-declare-module
sandersn 8f6f55f
Move eslint rule to eslint-plugin
sandersn 905e9ea
Add no-single-declare-module to default list in eslint-plugin
sandersn 97e794d
Add changeset
sandersn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
packages/dtslint/test/no-single-declare-module/augmentation.d.ts.lint
This file was deleted.
Oops, something went wrong.
2 changes: 0 additions & 2 deletions
2
packages/dtslint/test/no-single-declare-module/multiple.d.ts.lint
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
packages/dtslint/test/no-single-declare-module/wildcard.d.ts.lint
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
packages/dtslint/test/no-single-declare-module/wrong.d.ts.lint
This file was deleted.
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
58 changes: 58 additions & 0 deletions
58
packages/eslint-plugin/src/rules/no-single-declare-module.ts
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,58 @@ | ||
import { ESLintUtils } from "@typescript-eslint/utils"; | ||
import * as ts from "typescript"; | ||
|
||
import { createRule } from "../util"; | ||
|
||
const rule = createRule({ | ||
defaultOptions: [], | ||
meta: { | ||
docs: { | ||
description: "Don't use an ambient module declaration if there's just one -- write it as a normal module.", | ||
recommended: "error", | ||
}, | ||
messages: { | ||
oneModuleDeclaration: | ||
"File has only 1 ambient module declaration. Move the contents outside the ambient module block, rename the file to match the ambient module name, and remove the block.", | ||
}, | ||
schema: [], | ||
type: "problem", | ||
}, | ||
name: "no-single-declare-module", | ||
create(context) { | ||
const services = ESLintUtils.getParserServices(context); | ||
const sourceFile = services.esTreeNodeToTSNodeMap.get(context.getSourceCode().ast); | ||
|
||
// If it's an external module, any module declarations inside are augmentations. | ||
if (ts.isExternalModule(sourceFile)) { | ||
return {}; | ||
} | ||
|
||
let moduleDeclaration: ts.ModuleDeclaration | undefined; | ||
for (const statement of sourceFile.statements) { | ||
if (ts.isModuleDeclaration(statement) && ts.isStringLiteral(statement.name)) { | ||
if (statement.name.text.indexOf("*") !== -1) { | ||
// Ignore wildcard module declarations | ||
return {}; | ||
} | ||
|
||
if (moduleDeclaration === undefined) { | ||
moduleDeclaration = statement; | ||
} else { | ||
// Has more than 1 declaration | ||
return {}; | ||
} | ||
} | ||
} | ||
|
||
if (moduleDeclaration) { | ||
context.report({ | ||
messageId: "oneModuleDeclaration", | ||
node: services.tsNodeToESTreeNodeMap.get(moduleDeclaration), | ||
}); | ||
} | ||
|
||
return {}; | ||
}, | ||
}); | ||
|
||
export = rule; |
50 changes: 50 additions & 0 deletions
50
packages/eslint-plugin/test/no-single-declare-module.test.ts
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,50 @@ | ||
import { ESLintUtils } from "@typescript-eslint/utils"; | ||
import path from "path"; | ||
|
||
import rule from "../src/rules/no-single-declare-module"; | ||
|
||
const ruleTester = new ESLintUtils.RuleTester({ | ||
parser: "@typescript-eslint/parser", | ||
parserOptions: { | ||
tsconfigRootDir: __dirname, | ||
project: "./tsconfig.no-single-declare-module.json", | ||
}, | ||
}); | ||
|
||
ruleTester.run("no-single-declare-module", rule, { | ||
invalid: [ | ||
{ | ||
code: ` | ||
declare module "foo" {} | ||
|
||
// Other global declarations don't affect this. They should go in "declare global". | ||
interface I {} | ||
`, | ||
errors: [ | ||
{ | ||
line: 2, | ||
messageId: "oneModuleDeclaration", | ||
}, | ||
], | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: ` | ||
import x from "x"; | ||
declare module "foo" {} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
declare module "foo" {} | ||
declare module "bar" {} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
declare module "*.svg" {} | ||
`, | ||
}, | ||
], | ||
}); |
7 changes: 7 additions & 0 deletions
7
packages/eslint-plugin/test/tsconfig.no-single-declare-module.json
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,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"target": "esnext" | ||
}, | ||
"files": ["file.ts"] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Obligatory reminder to add this to the preset (tell me if you already know and I'll stop mentioning it in the future 😄)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, every single reminder is helpful.