Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

no-bad-reference: Avoid <reference types> of the current package #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 16 additions & 2 deletions docs/no-bad-reference.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# no-bad-reference

(This rule is specific to DefinitelyTyped.)
Avoid using `<reference path>`.
Avoid using `<reference path>` to reference other packages.
Do use `<reference path>` to reference a file in the current package, though this is usually unnecessary.

**Bad**:

Expand All @@ -25,4 +26,17 @@ If not, use `<reference types>` instead:
/// <reference types="foo" />
```

The only time `<reference path>` 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 `<reference path>` 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
/// <reference types="this-package-name" />
```

**Good**:

Usually intra-package references can simply be omitted. If not, use a relative path.

```ts
/// <reference path="./index.d.ts" />
```
29 changes: 28 additions & 1 deletion src/rules/noBadReferenceRule.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import assert = require("assert");
import { basename, dirname } from "path";
import * as Lint from "tslint";
import * as ts from "typescript";

Expand All @@ -18,7 +20,15 @@ export class Rule extends Lint.Rules.AbstractRule {
"Don't use <reference path> to reference another package. Use an import or <reference types> instead.");
static FAILURE_STRING_REFERENCE_IN_TEST = failure(
Rule.metadata.ruleName,
"Don't use <reference path> in test files. Use <reference types> or include the file in 'tsconfig.json'.");
"Don't use <reference path> in test files. " +
"Use <reference types> 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 '<reference path>' instead.");
}

apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
Expand All @@ -27,6 +37,14 @@ export class Rule extends Lint.Rules.AbstractRule {

function walk(ctx: Lint.WalkContext<void>): 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("..")) {
Expand All @@ -37,3 +55,12 @@ function walk(ctx: Lint.WalkContext<void>): void {
}
}
}

function getCurrentPackageName(fileName: string): string {
let dir = dirname(fileName);
while (basename(dirname(dir)) !== "types") {
assert(dir !== "");
dir = dirname(dir);
}
return basename(dir);
}
2 changes: 1 addition & 1 deletion test/no-bad-reference/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/// <reference path="foo" />
~~~ [0]

[0]: Don't use <reference path> in test files. Use <reference types> or include the file in 'tsconfig.json'. See: https://github.com/Microsoft/dtslint/blob/master/docs/no-bad-reference.md
[0]: Don't use <reference path> in test files. Use <reference types> 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