From d4ec74f762e2906703268522b43a3c409faff14a Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 31 Oct 2018 17:34:06 -0700 Subject: [PATCH] no-bad-reference: Avoid `` of the current package --- docs/no-bad-reference.md | 18 ++++++++++++++++-- src/rules/noBadReferenceRule.ts | 29 ++++++++++++++++++++++++++++- test/no-bad-reference/test.ts.lint | 2 +- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/docs/no-bad-reference.md b/docs/no-bad-reference.md index 69329bdd..61a9bec6 100644 --- a/docs/no-bad-reference.md +++ b/docs/no-bad-reference.md @@ -1,7 +1,8 @@ # no-bad-reference (This rule is specific to DefinitelyTyped.) -Avoid using ``. +Avoid using `` to reference other packages. +Do use `` to reference a file in the current package, though this is usually unnecessary. **Bad**: @@ -25,4 +26,17 @@ If not, use `` instead: /// ``` -The only time `` should be necessary if for global (not module) libraries that are separated into multiple files; the index file must include references to the others to bring them into the compilation. +The only time `` should be necessary is for global (not module) libraries that are separated into multiple files; the index file must include references to the others to bring them into the compilation. + +**Bad**: +```ts +/// +``` + + **Good**: + +Usually intra-package references can simply be omitted. If not, use a relative path. + +```ts +/// +``` diff --git a/src/rules/noBadReferenceRule.ts b/src/rules/noBadReferenceRule.ts index e597214e..5b81c9f9 100644 --- a/src/rules/noBadReferenceRule.ts +++ b/src/rules/noBadReferenceRule.ts @@ -1,3 +1,5 @@ +import assert = require("assert"); +import { basename, dirname } from "path"; import * as Lint from "tslint"; import * as ts from "typescript"; @@ -18,7 +20,15 @@ export class Rule extends Lint.Rules.AbstractRule { "Don't use to reference another package. Use an import or instead."); static FAILURE_STRING_REFERENCE_IN_TEST = failure( Rule.metadata.ruleName, - "Don't use in test files. Use or include the file in 'tsconfig.json'."); + "Don't use in test files. " + + "Use for external dependencies. " + + "To reference a file in this package, include it in 'tsconfig.json'."); + static FAILURE_STRING_TYPE_REFERENCE_TO_SELF(packageName: string): string { + return failure( + Rule.metadata.ruleName, + `Type reference to ${packageName} refers to the current package.\n` + + "normally this can simply be removed, or you should use a '' instead."); + } apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { return this.applyWithFunction(sourceFile, walk); @@ -27,6 +37,14 @@ export class Rule extends Lint.Rules.AbstractRule { function walk(ctx: Lint.WalkContext): void { const { sourceFile } = ctx; + + for (const ref of sourceFile.typeReferenceDirectives) { + const packageName = getCurrentPackageName(sourceFile.fileName); + if (ref.fileName === packageName) { + ctx.addFailure(ref.pos, ref.end, Rule.FAILURE_STRING_TYPE_REFERENCE_TO_SELF(basename(packageName))); + } + } + for (const ref of sourceFile.referencedFiles) { if (sourceFile.isDeclarationFile) { if (ref.fileName.startsWith("..")) { @@ -37,3 +55,12 @@ function walk(ctx: Lint.WalkContext): void { } } } + +function getCurrentPackageName(fileName: string): string { + let dir = dirname(fileName); + while (basename(dirname(dir)) !== "types") { + assert(dir !== ""); + dir = dirname(dir); + } + return basename(dir); +} diff --git a/test/no-bad-reference/test.ts.lint b/test/no-bad-reference/test.ts.lint index 9284936e..f6774387 100644 --- a/test/no-bad-reference/test.ts.lint +++ b/test/no-bad-reference/test.ts.lint @@ -4,4 +4,4 @@ /// ~~~ [0] -[0]: Don't use in test files. Use or include the file in 'tsconfig.json'. See: https://github.com/Microsoft/dtslint/blob/master/docs/no-bad-reference.md +[0]: Don't use in test files. Use for external dependencies. To reference a file in this package, include it in 'tsconfig.json'. See: https://github.com/Microsoft/dtslint/blob/master/docs/no-bad-reference.md