Skip to content

Commit

Permalink
Enable if-else in test markup, fix build (palantir#3226)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajafff authored and adidahiya committed Sep 19, 2017
1 parent c434183 commit 4ac145e
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ export function runTest(testDirectory: string, rulesDirectory?: string | string[
let fileText = isEncodingRule ? readBufferWithDetectedEncoding(fs.readFileSync(fileToLint)) : fs.readFileSync(fileToLint, "utf-8");
const tsVersionRequirement = parse.getTypescriptVersionRequirement(fileText);
if (tsVersionRequirement !== undefined) {
const tsVersion = new semver.SemVer(ts.version);
// remove prerelease suffix when matching to allow testing with nightly builds
if (!semver.satisfies(`${tsVersion.major}.${tsVersion.minor}.${tsVersion.patch}`, tsVersionRequirement)) {
if (!semver.satisfies(parse.getNormalizedTypescriptVersion(), tsVersionRequirement)) {
results.results[fileToLint] = {
requirement: tsVersionRequirement,
skipped: true,
Expand All @@ -107,6 +106,7 @@ export function runTest(testDirectory: string, rulesDirectory?: string | string[
const lineBreak = fileText.search(/\n/);
fileText = lineBreak === -1 ? "" : fileText.substr(lineBreak + 1);
}
fileText = parse.preprocessDirectives(fileText);
const fileTextWithoutMarkup = parse.removeErrorMarkup(fileText);
const errorsFromMarkup = parse.parseErrorsFromMarkup(fileText);

Expand Down
52 changes: 52 additions & 0 deletions src/verify/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import * as semver from "semver";
import * as ts from "typescript";
import { format } from "util";

Expand Down Expand Up @@ -41,6 +42,57 @@ export function getTypescriptVersionRequirement(text: string): string | undefine
return undefined;
}

export function getNormalizedTypescriptVersion(): string {
const tsVersion = new semver.SemVer(ts.version);
// remove prerelease suffix when matching to allow testing with nightly builds
return `${tsVersion.major}.${tsVersion.minor}.${tsVersion.patch}`;
}

export function preprocessDirectives(text: string): string {
if (!/^#(?:if|else|endif)\b/m.test(text)) {
return text; // If there are no directives, just return the input unchanged
}
const tsVersion = getNormalizedTypescriptVersion();
const enum State {
Initial,
If,
Else,
}
const lines = text.split(/\n/);
const result = [];
let collecting = true;
let state = State.Initial;
for (const line of lines) {
if (line.startsWith("#if typescript")) {
if (state !== State.Initial) {
throw lintSyntaxError("#if directives cannot be nested");
}
state = State.If;
collecting = semver.satisfies(tsVersion, line.slice("#if typescript".length).trim());
} else if (/^#else\s*$/.test(line)) {
if (state !== State.If) {
throw lintSyntaxError("unexpected #else");
}
state = State.Else;
collecting = !collecting;
} else if (/^#endif\s*$/.test(line)) {
if (state === State.Initial) {
throw lintSyntaxError("unexpected #endif");
}
state = State.Initial;
collecting = true;
} else if (collecting) {
result.push(line);
}
}

if (state !== State.Initial) {
throw lintSyntaxError("expected #endif");
}

return result.join("\n");
}

/**
* Takes the full text of a .lint file and returns the contents of the file
* with all error markup removed
Expand Down
21 changes: 21 additions & 0 deletions test/rules/_integration/typescript-version/if-else.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'foo';
~~~~~ [' should be "]
#if typescript 0.0.0
'bar'
#else
"bar"
#endif

#if typescript >= 2.0.0
'baz'
#else
"baz"
#endif
~~~~~ [err]

'bas'
~~~~~ [err]

#if typescript >= 2.0.0
[err]: ' should be "
#endif
26 changes: 17 additions & 9 deletions test/rules/no-unused-variable/check-parameters/test.ts.lint
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export function func1(x: number, y: number, ...args: number[]) {
~~~~ ['args' is declared but never used.]
~~~~ [err % ('args')]
return x + y;
}

export function func2(x: number, y: number, ...args: number[]) {
~ ['y' is declared but never used.]
~ [err % ('y')]
return x + args[0];
}

export function func3(x?: number, y?: number) {
~ ['y' is declared but never used.]
~ [err % ('y')]
return x;
}

Expand All @@ -19,7 +19,7 @@ export interface ITestInterface {

export class ABCD {
constructor(private x: number, public y: number, private z: number) {
~ [Property 'x' is declared but never used.]
~ [prop % ('x')]
}

func5() {
Expand All @@ -40,7 +40,7 @@ export function func7(f: (x: number) => number) {
}

export function func8([x, y]: [number, number]) {
~ ['y' is declared but never used.]
~ [err % ('y')]
return x;
}

Expand All @@ -49,23 +49,31 @@ export class DestructuringTests {
}

public func9({a, b}) {
~ ['b' is declared but never used.]
~ [err % ('b')]
return a;
}

public func10([a, b]) {
~ ['b' is declared but never used.]
~ [err % ('b')]
return [a];
}

// destructuring with default value
public func11([x = 0]) {
~ ['x' is declared but never used.]
~ [err % ('x')]
return;
}
}

abstract class AbstractTest {
~~~~~~~~~~~~ ['AbstractTest' is declared but never used.]
~~~~~~~~~~~~ [err % ('AbstractTest')]
abstract foo(x);
}

#if typescript >= 2.6.0
[err]: '%s' is declared but its value is never read.
[prop]: Property '%s' is declared but its value is never read.
#else
[err]: '%s' is declared but never used.
[prop]: Property '%s' is declared but never used.
#endif
10 changes: 8 additions & 2 deletions test/rules/no-unused-variable/default/class.ts.lint
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ABCD {
private z2: number;
~~ ['z2' is declared but never used.]
~~ [err % ('z2')]
constructor() {
}

Expand All @@ -17,8 +17,14 @@ class ABCD {
}

private mfunc4() {
~~~~~~ ['mfunc4' is declared but never used.]
~~~~~~ [err % ('mfunc4')]
//
}
static stillPublic: number;
}

#if typescript >= 2.6.0
[err]: '%s' is declared but its value is never read.
#else
[err]: '%s' is declared but never used.
#endif
10 changes: 8 additions & 2 deletions test/rules/no-unused-variable/default/false-positives.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const fs = require("fs");

module Foo {
~~~ ['Foo' is declared but never used.]
~~~ [err % ('Foo')]
const path = require("path");

console.log(fs);
Expand All @@ -24,7 +24,7 @@ hello.sayHello();
import Bar = whatever.that.Foo;

module some.module.blah {
~~~~ ['some' is declared but never used.]
~~~~ [err % ('some')]
export class bar {
private bar: Bar;
constructor() {
Expand Down Expand Up @@ -52,3 +52,9 @@ myFunc(bar);

import a from "a";
export { a };

#if typescript >= 2.6.0
[err]: '%s' is declared but its value is never read.
#else
[err]: '%s' is declared but never used.
#endif
12 changes: 9 additions & 3 deletions test/rules/no-unused-variable/default/function.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ function func1(x: number, y: number) {
}

var func2 = () => {
~~~~~ ['func2' is declared but never used.]
~~~~~ [err % ('func2')]
//
};

function func3() {
~~~~~ ['func3' is declared but never used.]
~~~~~ [err % ('func3')]
return func1(1, 2);
}

Expand All @@ -17,8 +17,14 @@ export function func4() {
}

declare function func5(): any;
~~~~~ ['func5' is declared but never used.]
~~~~~ [err % ('func5')]

export default function () {
return 0;
}

#if typescript >= 2.6.0
[err]: '%s' is declared but its value is never read.
#else
[err]: '%s' is declared but never used.
#endif
1 change: 1 addition & 0 deletions test/rules/no-unused-variable/default/import.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ import "a";
import abc = require('a');
import def = abc.someVar;
console.log(def);

30 changes: 18 additions & 12 deletions test/rules/no-unused-variable/default/import.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
* This text will not be deleted.
*/
import xyz = require("a");
~~~ ['xyz' is declared but never used.]
~~~ [err % ('xyz')]
import $ = require("a");
import _ = require("a");
import {a1 as aa1, a2 as aa2} from "a";
~~~ ['aa1' is declared but never used.]
~~~ [err % ('aa1')]
aa2;
import {a3 as aa3, a4 as aa4} from "a";
~~~ ['aa4' is declared but never used.]
~~~ [err % ('aa4')]
aa3;
// This import statement is unused and will be deleted along with this comment.
import {a5, a6} from "a";
~~~~~~~~~~~~~~~~~~~~~~~~~ [All imports are unused.]
import {a7} from "a";
~~~~~~~~~~~~~~~~~~~~~ [All imports are unused.]
import {a8, a9, a10} from "a";
~~ ['a9' is declared but never used.]
~~~ ['a10' is declared but never used.]
~~ [err % ('a9')]
~~~ [err % ('a10')]
a8;
import {a11, a12, a13} from "a";
~~~ ['a11' is declared but never used.]
~~~ ['a12' is declared but never used.]
~~~ [err % ('a11')]
~~~ [err % ('a12')]
a13;
import {a14, a15, a16} from "a";
~~~ ['a15' is declared but never used.]
~~~ [err % ('a15')]
a14;
a16;

Expand All @@ -42,17 +42,17 @@ $(_.xyz());
/// <reference path="../externalFormatter.test.ts" />

module S {
~ ['S' is declared but never used.]
~ [err % ('S')]
var template = "";
~~~~~~~~ ['template' is declared but never used.]
~~~~~~~~ [err % ('template')]
}

import * as foo from "a";
~~~ ['foo' is declared but never used.]
~~~ [err % ('foo')]
import * as bar from "a";
import baz from "a";
import defaultExport, { namedExport } from "a";
~~~~~~~~~~~~~ ['defaultExport' is declared but never used.]
~~~~~~~~~~~~~ [err % ('defaultExport')]
import d1, { d2 } from "a";
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [All imports are unused.]
import d3, { d4 } from "a";
Expand All @@ -75,3 +75,9 @@ import "a";
import abc = require('a');
import def = abc.someVar;
console.log(def);

#if typescript >= 2.6.0
[err]: '%s' is declared but its value is never read.
#else
[err]: '%s' is declared but never used.
#endif
6 changes: 5 additions & 1 deletion test/rules/no-unused-variable/default/react-addons3.tsx.lint
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
import * as React from "react/addons";
~~~~~ ['React' is declared but never used.]
#if typescript >= 2.6.0
~~~~~ ['React' is declared but its value is never read.]
#else
~~~~~ ['React' is declared but never used.]
#endif
4 changes: 4 additions & 0 deletions test/rules/no-unused-variable/default/react3.tsx.lint
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
import * as React from "react";
#if typescript >= 2.6.0
~~~~~ ['React' is declared but its value is never read.]
#else
~~~~~ ['React' is declared but never used.]
#endif
4 changes: 4 additions & 0 deletions test/rules/no-unused-variable/default/react4.tsx.lint
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
import * as React from "react";
#if typescript >= 2.6.0
~~~~~ ['React' is declared but its value is never read.]
#else
~~~~~ ['React' is declared but never used.]
#endif
Loading

0 comments on commit 4ac145e

Please sign in to comment.