Skip to content
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
merged 6 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/dtslint/dtslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"only-arrow-functions": false,
// Custom rules
"expect": true,
"no-single-declare-module": true,
"unified-signatures": true,
"void-return": true,
"npm-naming": true,
Expand Down
55 changes: 0 additions & 55 deletions packages/dtslint/src/rules/noSingleDeclareModuleRule.ts

This file was deleted.

This file was deleted.

This file was deleted.

6 changes: 0 additions & 6 deletions packages/dtslint/test/no-single-declare-module/tslint.json

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as noUselessFiles from "./no-useless-files";
import * as preferDeclareFunction from "./prefer-declare-function";
import * as redundantUndefined from "./redundant-undefined";
import * as strictExportDeclareModifiers from "./strict-export-declare-modifiers";
import * as noSingleDeclareModule from './no-single-declare-module';

export const rules = {
"dt-header": dtHeader,
Expand All @@ -34,4 +35,5 @@ export const rules = {
"prefer-declare-function": preferDeclareFunction,
"redundant-undefined": redundantUndefined,
"strict-export-declare-modifiers": strictExportDeclareModifiers,
"no-single-declare-module": noSingleDeclareModule,
};
58 changes: 58 additions & 0 deletions packages/eslint-plugin/src/rules/no-single-declare-module.ts
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({
Copy link
Member

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 😄)

Copy link
Member

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.

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 packages/eslint-plugin/test/no-single-declare-module.test.ts
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" {}
`,
},
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"strict": true,
"target": "esnext"
},
"files": ["file.ts"]
}
Loading