mirror of
https://github.com/chenasraf/DefinitelyTyped-tools.git
synced 2026-05-17 17:48:07 +00:00
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:
@@ -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;
|
||||
`
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
{
|
||||
"extends": "./dtslint.json",
|
||||
"rules": {
|
||||
"no-self-import": true,
|
||||
|
||||
"no-redundant-jsdoc": false,
|
||||
"no-redundant-jsdoc-2": true,
|
||||
|
||||
|
||||
40
packages/dtslint/src/rules/no-self-import.ts
Normal file
40
packages/dtslint/src/rules/no-self-import.ts
Normal 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;
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
47
packages/dtslint/test/no-self-import.test.ts
Normal file
47
packages/dtslint/test/no-self-import.test.ts
Normal 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",
|
||||
},
|
||||
],
|
||||
});
|
||||
7
packages/dtslint/test/tsconfig.no-self-import.json
Normal file
7
packages/dtslint/test/tsconfig.no-self-import.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"strict": true,
|
||||
"target": "esnext"
|
||||
},
|
||||
"files": ["this-package/index.d.ts"]
|
||||
}
|
||||
Reference in New Issue
Block a user