Converted no-self-import from TSLint to ESLint (#652)

* Converted no-self-import from TSLint to ESLint

* Update packages/dtslint/src/rules/no-self-import.ts
This commit is contained in:
Josh Goldberg
2023-03-24 13:16:09 -04:00
committed by GitHub
parent d5429a4fdb
commit f2d538de14
6 changed files with 94 additions and 40 deletions

View File

@@ -165,7 +165,6 @@ export * from 'buffer';
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
/* tslint:disable-next-line:no-self-import */
import webpack = require('webpack');
export = webpack;
`

View File

@@ -1,8 +1,6 @@
{
"extends": "./dtslint.json",
"rules": {
"no-self-import": true,
"no-redundant-jsdoc": false,
"no-redundant-jsdoc-2": true,

View File

@@ -0,0 +1,40 @@
import { ESLintUtils } from "@typescript-eslint/utils";
import { createRule, getCommonDirectoryName } from "../util";
const rule = createRule({
name: "no-self-import",
defaultOptions: [],
meta: {
type: "problem",
docs: {
description: "Forbids declaration files to import the current package using a global import.",
recommended: "error",
},
messages: {
useRelativeImport: "Declaration file should not use a global import of itself. Use a relative import.",
},
schema: [],
},
create(context) {
if (!context.getFilename().endsWith(".d.ts")) {
return {};
}
const services = ESLintUtils.getParserServices(context);
const packageName = getCommonDirectoryName(services.program.getRootFileNames());
return {
// eslint-disable-next-line @typescript-eslint/naming-convention
ImportDeclaration(node) {
if (node.source.value === packageName || node.source.value.startsWith(packageName + "/")) {
context.report({
messageId: "useRelativeImport",
node,
});
}
},
};
},
});
export = rule;

View File

@@ -1,37 +0,0 @@
import * as Lint from "tslint";
import * as ts from "typescript";
import { failure, getCommonDirectoryName } from "../util";
export class Rule extends Lint.Rules.TypedRule {
static metadata: Lint.IRuleMetadata = {
ruleName: "no-self-import",
description: "Forbids declaration files to import the current package using a global import.",
optionsDescription: "Not configurable.",
options: null,
type: "functionality",
typescriptOnly: false,
};
applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
if (!sourceFile.isDeclarationFile) {
return [];
}
const name = getCommonDirectoryName(program.getRootFileNames());
return this.applyWithFunction(sourceFile, (ctx) => walk(ctx, name));
}
}
const failureMessage = failure(
Rule.metadata.ruleName,
"Declaration file should not use a global import of itself. Use a relative import."
);
function walk(ctx: Lint.WalkContext<void>, packageName: string): void {
for (const i of ctx.sourceFile.imports) {
if (i.text === packageName || i.text.startsWith(packageName + "/")) {
ctx.addFailureAtNode(i, failureMessage);
}
}
}

View File

@@ -0,0 +1,47 @@
import { ESLintUtils } from "@typescript-eslint/utils";
import path from "path";
import * as dtHeader from "../src/rules/no-self-import";
const ruleTester = new ESLintUtils.RuleTester({
parser: "@typescript-eslint/parser",
parserOptions: {
tsconfigRootDir: __dirname,
project: "./tsconfig.no-self-import.json",
},
});
ruleTester.run("no-self-import", dtHeader, {
invalid: [
{
code: `import myself from "this-package";`,
errors: [
{
line: 1,
messageId: "useRelativeImport",
},
],
filename: "this-package/index.d.ts",
},
{
code: `import abc from "this-package/abc.d.ts";`,
errors: [
{
line: 1,
messageId: "useRelativeImport",
},
],
filename: "this-package/index.d.ts",
},
],
valid: [
{
code: `import other from "other-package";`,
filename: "this-package/index.d.ts",
},
{
code: `import other from "other-package/this-package";`,
filename: "this-package/index.d.ts",
},
],
});

View File

@@ -0,0 +1,7 @@
{
"compilerOptions": {
"strict": true,
"target": "esnext"
},
"files": ["this-package/index.d.ts"]
}