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-self-import from TSLint to ESLint #652

Merged
merged 2 commits into from Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/definitions-parser/test/definition-parser.test.ts
Expand Up @@ -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;
`
Expand Down
2 changes: 0 additions & 2 deletions packages/dtslint/dt.json
@@ -1,8 +1,6 @@
{
"extends": "./dtslint.json",
"rules": {
"no-self-import": true,

"no-redundant-jsdoc": false,
"no-redundant-jsdoc-2": true,

Expand Down
40 changes: 40 additions & 0 deletions packages/dtslint/src/rules/no-self-import.ts
@@ -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: "Ensure consistency of DefinitelyTyped headers.",
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
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;
37 changes: 0 additions & 37 deletions packages/dtslint/src/rules/noSelfImportRule.ts

This file was deleted.

47 changes: 47 additions & 0 deletions packages/dtslint/test/no-self-import.test.ts
@@ -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 changes: 7 additions & 0 deletions packages/dtslint/test/tsconfig.no-self-import.json
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"strict": true,
"target": "esnext"
},
"files": ["this-package/index.d.ts"]
}