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

Commit 8696ac9

Browse files
author
Andy Hanson
committed
Add tests
1 parent 9b6e8a4 commit 8696ac9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+302
-35
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
.vscode
21
bin
32
node_modules
43
typescript-installs

.vscode/launch.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
3+
"version": "0.2.0",
4+
"configurations": [
5+
{
6+
"type": "node",
7+
"request": "launch",
8+
"name": "Launch Program",
9+
"program": "${workspaceRoot}/test/test.js",
10+
"outFiles": ["${workspaceRoot}/bin"]
11+
}
12+
]
13+
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"scripts": {
2121
"watch": "tsc --watch",
2222
"build": "tsc",
23-
"lint": "tslint --project tsconfig.json --type-check --format stylish"
23+
"lint": "tslint --project tsconfig.json --type-check --format stylish",
24+
"test": "node test/test.js"
2425
},
2526
"dependencies": {
2627
"fs-promise": "^2.0.0",

src/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@ async function test(dirPath: string, options: Options, version: TypeScriptVersio
129129

130130
export interface TestError { message: string; }
131131

132-
async function testWithVersion(dirPath: string, options: Options, version: TypeScriptVersion | "next"): Promise<TestError | undefined> {
132+
async function testWithVersion(
133+
dirPath: string,
134+
options: Options,
135+
version: TypeScriptVersion | "next",
136+
): Promise<TestError | undefined> {
133137
await install(version);
134138
if (options.noLint) {
135139
// Special for old DefinitelyTyped packages that aren't linted yet.

src/lint.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ export async function lintWithVersion(
3232
return result.failures.length ? { message: result.output } : undefined;
3333
}
3434

35-
async function getLintConfig(configuration: Configuration, configPath: string, options: Options): Promise<IConfigurationFile> {
35+
async function getLintConfig(
36+
configuration: Configuration,
37+
configPath: string,
38+
options: Options,
39+
): Promise<IConfigurationFile> {
3640
if (!await exists(configPath)) {
3741
if (options.dt) {
3842
throw new Error('On DefinitelyTyped, must include `tslint.json` containing `{ "extends": "../tslint.json" }`');

src/rules/noBadReferenceRule.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class Rule extends Lint.Rules.AbstractRule {
1414
static FAILURE_STRING =
1515
"Don't use <reference path> to reference another package. Use an import or <reference types> instead.";
1616
static FAILURE_STRING_REFERENCE_IN_TEST =
17-
"Don't use <reference path> in test files. Use <reference types> or include the file in 'tsconfig.json'";
17+
"Don't use <reference path> in test files. Use <reference types> or include the file in 'tsconfig.json'.";
1818

1919
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
2020
return this.applyWithFunction(sourceFile, walk);
@@ -26,10 +26,10 @@ function walk(ctx: Lint.WalkContext<void>): void {
2626
for (const ref of sourceFile.referencedFiles) {
2727
if (sourceFile.isDeclarationFile) {
2828
if (ref.fileName.startsWith("..")) {
29-
ctx.addFailureAt(ref.pos, ref.end, Rule.FAILURE_STRING);
29+
ctx.addFailure(ref.pos, ref.end, Rule.FAILURE_STRING);
3030
}
3131
} else {
32-
ctx.addFailureAt(ref.pos, ref.end, Rule.FAILURE_STRING_REFERENCE_IN_TEST);
32+
ctx.addFailure(ref.pos, ref.end, Rule.FAILURE_STRING_REFERENCE_IN_TEST);
3333
}
3434
}
3535
}

src/rules/noDeadReferenceRule.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,26 @@ export class Rule extends Lint.Rules.AbstractRule {
2020
}
2121

2222
function walk(ctx: Lint.WalkContext<void>): void {
23-
const { sourceFile } = ctx;
24-
if (!sourceFile.statements.length) {
23+
const { sourceFile: { statements, text } } = ctx;
24+
if (!statements.length) {
2525
return;
2626
}
2727

2828
// 'm' flag makes it multiline, so `^` matches the beginning of any line.
2929
// 'g' flag lets us set rgx.lastIndex
30-
const rgx = /^\s*\/\/\/ <reference/mg;
30+
const rgx = /^\s*(\/\/\/ <reference)/mg;
31+
3132
// Start search at the first statement. (`/// <reference>` before that is OK.)
32-
rgx.lastIndex = sourceFile.statements[0].getStart();
33-
const match = rgx.exec(sourceFile.text);
34-
if (match !== null) {
35-
ctx.addFailureAt(match.index, 0, Rule.FAILURE_STRING);
33+
rgx.lastIndex = statements[0].getStart();
34+
35+
while (true) {
36+
const match = rgx.exec(text);
37+
if (match === null) {
38+
break;
39+
}
40+
41+
const length = match[1].length;
42+
const start = match.index + match[0].length - length;
43+
ctx.addFailureAt(start, length, Rule.FAILURE_STRING);
3644
}
3745
}

src/rules/noPaddingRule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Lint from "tslint";
22
import * as ts from "typescript";
33

44
export class Rule extends Lint.Rules.AbstractRule {
5-
public static metadata: Lint.IRuleMetadata = {
5+
static metadata: Lint.IRuleMetadata = {
66
ruleName: "no-padding",
77
description: "Forbids unnecessary 'export' or 'declare' modifiers in declaration files.",
88
optionsDescription: "Not configurable.",

src/rules/noRedundantModifiersRule.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Lint from "tslint";
22
import * as ts from "typescript";
33

44
export class Rule extends Lint.Rules.AbstractRule {
5-
public static metadata: Lint.IRuleMetadata = {
5+
static metadata: Lint.IRuleMetadata = {
66
ruleName: "no-redundant-modifiers",
77
description: "Forbids unnecessary 'export' or 'declare' modifiers in declaration files.",
88
optionsDescription: "Not configurable.",
@@ -37,8 +37,10 @@ function walk(ctx: Lint.WalkContext<void>): void {
3737
// Types do not need 'declare'.
3838
switch (node.kind) {
3939
case ts.SyntaxKind.InterfaceDeclaration:
40-
case ts.SyntaxKind.TypeAliasDeclaration:
41-
ctx.addFailureAtNode(node, "'declare' keyword is redundant here.");
40+
case ts.SyntaxKind.TypeAliasDeclaration: {
41+
const { name } = node as ts.TypeAliasDeclaration | ts.InterfaceDeclaration;
42+
ctx.addFailureAtNode(name, "'declare' keyword is redundant here.");
43+
}
4244
}
4345
}
4446
}

src/rules/noRelativeImportInTestRule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Lint from "tslint";
22
import * as ts from "typescript";
33

44
export class Rule extends Lint.Rules.TypedRule {
5-
public static metadata: Lint.IRuleMetadata = {
5+
static metadata: Lint.IRuleMetadata = {
66
ruleName: "no-relative-import-in-test",
77
description: "Forbids test (non-declaration) files to use relative imports.",
88
optionsDescription: "Not configurable.",

src/rules/noUselessFilesRule.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as ts from "typescript";
55
// Remove when that PR is in.
66

77
export class Rule extends Lint.Rules.AbstractRule {
8-
public static metadata: Lint.IRuleMetadata = {
8+
static metadata: Lint.IRuleMetadata = {
99
ruleName: "no-useless-files",
1010
description: "Forbids files with no content.",
1111
optionsDescription: "Not configurable.",
@@ -14,9 +14,9 @@ export class Rule extends Lint.Rules.AbstractRule {
1414
typescriptOnly: false,
1515
};
1616

17-
public static FAILURE_STRING = "File has no content.";
17+
static FAILURE_STRING = "File has no content.";
1818

19-
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
19+
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
2020
if (sourceFile.statements.length) {
2121
return [];
2222
}

src/rules/noVarRule.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as ts from "typescript";
44
// TODO: pull request to update tslint's `no-var-keyword`
55

66
export class Rule extends Lint.Rules.AbstractRule {
7-
public static metadata: Lint.IRuleMetadata = {
7+
static metadata: Lint.IRuleMetadata = {
88
ruleName: "no-var",
99
description: "Forbids 'var'.",
1010
optionsDescription: "Not configurable.",
@@ -13,9 +13,9 @@ export class Rule extends Lint.Rules.AbstractRule {
1313
typescriptOnly: false,
1414
};
1515

16-
public static FAILURE_STRING = "Do not use 'var'.";
16+
static FAILURE_STRING = "Do not use 'var'.";
1717

18-
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
18+
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
1919
return this.applyWithFunction(sourceFile, walk);
2020
}
2121
}

src/rules/preferDeclareFunctionRule.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Lint from "tslint";
22
import * as ts from "typescript";
33

44
export class Rule extends Lint.Rules.AbstractRule {
5-
public static metadata: Lint.IRuleMetadata = {
5+
static metadata: Lint.IRuleMetadata = {
66
ruleName: "prefer-declare-function",
77
description: "Forbids `export const x = () => void`.",
88
optionsDescription: "Not configurable.",
@@ -11,9 +11,9 @@ export class Rule extends Lint.Rules.AbstractRule {
1111
typescriptOnly: true,
1212
};
1313

14-
public static FAILURE_STRING = "Use a function declaration instead of a variable of function type.";
14+
static FAILURE_STRING = "Use a function declaration instead of a variable of function type.";
1515

16-
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
16+
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
1717
return this.applyWithFunction(sourceFile, walk);
1818
}
1919
}

src/rules/trimFileRule.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Lint from "tslint";
22
import * as ts from "typescript";
33

44
export class Rule extends Lint.Rules.AbstractRule {
5-
public static metadata: Lint.IRuleMetadata = {
5+
static metadata: Lint.IRuleMetadata = {
66
ruleName: "trim-file",
77
description: "Forbids leading/trailing blank lines in a file. Allows file to end in '\n'.",
88
optionsDescription: "Not configurable.",
@@ -11,10 +11,11 @@ export class Rule extends Lint.Rules.AbstractRule {
1111
typescriptOnly: false,
1212
};
1313

14-
public static FAILURE_STRING_LEADING = "File should not begin with a blank line.";
15-
public static FAILURE_STRING_TRAILING = "File should not end with a blank line. (Ending in '\\n' OK, ending in '\\n\\n' not OK.)";
14+
static FAILURE_STRING_LEADING = "File should not begin with a blank line.";
15+
static FAILURE_STRING_TRAILING =
16+
"File should not end with a blank line. (Ending in '\\n' OK, ending in '\\n\\n' not OK.)";
1617

17-
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
18+
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
1819
return this.applyWithFunction(sourceFile, walk);
1920
}
2021
}
@@ -26,6 +27,6 @@ function walk(ctx: Lint.WalkContext<void>): void {
2627
}
2728

2829
if (text.endsWith("\n\n") || text.endsWith("\r\n\r\n")) {
29-
ctx.addFailureAt(text.length - 1, 1, Rule.FAILURE_STRING_TRAILING);
30+
ctx.addFailureAt(text.length - 2, 1, Rule.FAILURE_STRING_TRAILING);
3031
}
3132
}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Type definitions for dt-header 1.0
2+
// Project: https://github.com/bobby-headers/dt-header
3+
// Definitions by: Jane Doe <https://github.com/janedoe>
4+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

test/dt-header/correct/notIndex.d.ts.lint

Whitespace-only changes.

test/dt-header/correct/tslint.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"dt-header": true
4+
}
5+
}

test/dt-header/wrong/index.d.ts.lint

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Type definitions for dt-header v1.0.3
2+
~ [Error parsing header. Expected: foo MAJOR.MINOR (patch version not allowed)]
3+
// Project: https://github.com/bobby-headers/dt-header
4+
// Definitions by: Jane Doe <https://github.com/janedoe>
5+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Type definitions for
2+
~~~~~~~~~~~~~~~~~~~~~~~ [Header should only be in `index.d.ts`.]

test/dt-header/wrong/tslint.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"dt-header": true
4+
}
5+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace N {
2+
export const x: number;
3+
}
4+
function N(): void;
5+
export = N;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace N {
2+
export const x: number;
3+
}
4+
export = N;
5+
~~~~~~~~~~~ [Instead of `export =`-ing a namespace, use the body of the namespace as the module body.]
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"export-just-namespace": true
4+
}
5+
}

test/no-bad-reference/decl.d.ts.lint

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/// <reference path="../foo" />
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Don't use <reference path> to reference another package. Use an import or <reference types> instead.]
3+
4+
/// <reference path="foo" />

test/no-bad-reference/test.ts.lint

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference path="../foo" />
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]
3+
4+
/// <reference path="foo" />
5+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]
6+
7+
[0]: Don't use <reference path> in test files. Use <reference types> or include the file in 'tsconfig.json'.

test/no-bad-reference/tslint.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"no-bad-reference": true
4+
}
5+
}

test/no-dead-reference/test.ts.lint

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference types="foo" />
2+
import * as bar from "bar";
3+
/// <reference path="baz" />
4+
~~~~~~~~~~~~~~ [0]
5+
/// <reference types="bang" />
6+
~~~~~~~~~~~~~~ [0]
7+
8+
[0]: `/// <reference>` directive must be at top of file to take effect.

test/no-dead-reference/tslint.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"no-dead-reference": true
4+
}
5+
}

test/no-padding/test.ts.lint

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function f() {
2+
~ [Don't leave a blank line after '{']
3+
4+
return [
5+
~ [Don't leave a blank line after '[']
6+
7+
f(
8+
~ [Don't leave a blank line after '(']
9+
10+
0
11+
12+
)
13+
~ [Don't leave a blank line before ')']
14+
15+
];
16+
~ [Don't leave a blank line before ']']
17+
18+
}
19+
~ [Don't leave a blank line before '}']
20+
21+
function f() {
22+
return [
23+
f(
24+
0
25+
)
26+
];
27+
}

test/no-padding/tslint.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"no-padding": true
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
declare function f(): void;
2+
3+
declare module "m" {}
4+
5+
declare type T = number;
6+
~ [0]
7+
8+
declare interface I {}
9+
~ [0]
10+
11+
[0]: 'declare' keyword is redundant here.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export declare function f(): void;
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ['export declare' is redundant, just use 'export'.]
3+
4+
declare function g(): void;
5+
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Prefer 'export' to 'declare' in an external module.]
6+
7+
export namespace N {
8+
export function f(): void;
9+
~~~~~~~~~~~~~~~~~~~~~~~~~~ ['export' keyword is redundant here.]
10+
// TS compiler warns for 'declare' here.
11+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"no-redundant-modifiers": true
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { x } from "./declarationFile.d";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const x: number;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { x } from "./declarationFile.d";
2+
~~~~~~~~~~~~~~~~~~~~~ [0]
3+
import { y } from "./testFile";
4+
5+
[0]: Test file should not use a relative import. Use a global import as if this were a user of the package.

0 commit comments

Comments
 (0)