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

Update typescript-eslint to 2.6.0 #1702

Merged
merged 9 commits into from Oct 29, 2019
Merged
6 changes: 3 additions & 3 deletions eslint-bridge/package.json
Expand Up @@ -40,9 +40,9 @@
"typescript": "3.5.3"
},
"dependencies": {
"@typescript-eslint/eslint-plugin": "2.0.0",
"@typescript-eslint/experimental-utils": "2.0.0",
"@typescript-eslint/parser": "2.0.0",
"@typescript-eslint/eslint-plugin": "2.6.0",
"@typescript-eslint/experimental-utils": "2.6.0",
"@typescript-eslint/parser": "2.6.0",
"babel-eslint": "10.0.1",
"body-parser": "1.18.3",
"eslint": "5.16.0",
Expand Down
4 changes: 1 addition & 3 deletions eslint-bridge/src/analyzer.ts
Expand Up @@ -36,7 +36,6 @@ import {
} from "./runner/symbol-highlighter";
import * as fs from "fs";
import { rules as sonarjsRules } from "eslint-plugin-sonarjs";
import * as path from "path";

const COGNITIVE_COMPLEXITY_RULE_ID = "internal-cognitive-complexity";

Expand Down Expand Up @@ -137,8 +136,7 @@ function analyze(input: AnalysisInput, parse: Parse): AnalysisResponse {
if (!fileContent) {
fileContent = getFileContent(input.filePath);
}
const filePath = path.normalize(input.filePath);
const result = parse(fileContent, filePath, input.tsConfigs);
const result = parse(fileContent, input.filePath, input.tsConfigs);
if (result instanceof SourceCode) {
return analyzeFile(result, input);
} else {
Expand Down
42 changes: 16 additions & 26 deletions eslint-bridge/src/parser.ts
Expand Up @@ -27,7 +27,7 @@ import * as VueJS from "vue-eslint-parser";
// still we might consider extending this range
// if everything which we need is working on older/newer versions
const TYPESCRIPT_MINIMUM_VERSION = "3.2.1";
const TYPESCRIPT_MAXIMUM_VERSION = "3.6.0";
const TYPESCRIPT_MAXIMUM_VERSION = "3.8.0";

export const PARSER_CONFIG_MODULE: Linter.ParserOptions = {
tokens: true,
Expand Down Expand Up @@ -55,30 +55,6 @@ export type Parse = (
tsConfigs?: string[],
) => SourceCode | ParsingError;

// exported for testing
export function loggerFn(msg: string) {
if (
msg.includes(
`WARNING: You are currently running a version of TypeScript which is not officially supported by typescript-estree.`,
)
) {
const currentVersionMatch = msg.match(/YOUR TYPESCRIPT VERSION: (.+)\n/);
const currentVersion = currentVersionMatch ? currentVersionMatch[1] : "";
if (currentVersion >= TYPESCRIPT_MAXIMUM_VERSION) {
console.log(
`WARN You are using version of TypeScript ${currentVersion} which is not officially supported; supported versions >=${TYPESCRIPT_MINIMUM_VERSION} <${TYPESCRIPT_MAXIMUM_VERSION}`,
);
} else {
throw {
message: `You are using version of TypeScript ${currentVersion} which is not supported; supported versions >=${TYPESCRIPT_MINIMUM_VERSION}`,
};
}
} else {
// fall back to default behavior of 'typescript-estree'
console.log(msg);
}
}

export function parseJavaScriptSourceFile(fileContent: string): SourceCode | ParsingError {
let parseFunctions = [espree.parse, babel.parse];
if (fileContent.includes("@flow")) {
Expand Down Expand Up @@ -111,13 +87,14 @@ export function parseTypeScriptSourceFile(
tsConfigs?: string[],
): SourceCode | ParsingError {
try {
checkTypeScriptVersionCompatibility(require("typescript").version);
// we load the typescript parser dynamically, so we don't need typescript dependency when analyzing pure JS project
const tsParser = require("@typescript-eslint/parser");
const result = tsParser.parseForESLint(fileContent, {
...PARSER_CONFIG_MODULE,
filePath: filePath,
project: tsConfigs,
loggerFn,
loggerFn: console.log,
});
return new SourceCode({ ...result, parserServices: result.services, text: fileContent });
} catch (exception) {
Expand All @@ -129,6 +106,19 @@ export function parseTypeScriptSourceFile(
}
}

// exported for testing
export function checkTypeScriptVersionCompatibility(currentVersion: string) {
if (currentVersion >= TYPESCRIPT_MAXIMUM_VERSION) {
console.log(
`WARN You are using version of TypeScript ${currentVersion} which is not officially supported; supported versions >=${TYPESCRIPT_MINIMUM_VERSION} <${TYPESCRIPT_MAXIMUM_VERSION}`,
);
} else if (currentVersion < TYPESCRIPT_MINIMUM_VERSION) {
throw {
message: `You are using version of TypeScript ${currentVersion} which is not supported; supported versions >=${TYPESCRIPT_MINIMUM_VERSION}`,
};
}
}

export function unloadTypeScriptEslint() {
const tsParser = require.resolve("@typescript-eslint/parser");
delete require.cache[tsParser];
Expand Down
48 changes: 7 additions & 41 deletions eslint-bridge/tests/parser.test.ts
Expand Up @@ -6,14 +6,15 @@ import {
parseJavaScriptSourceFile,
parseTypeScriptSourceFile,
parseVueSourceFile,
loggerFn,
checkTypeScriptVersionCompatibility,
ParseExceptionCode,
parseExceptionCodeOf,
} from "../src/parser";
import * as espree from "espree";
import { SourceCode } from "eslint";
import { ParsingError } from "../src/analyzer";
import visit from "../src/utils/visitor";
import * as path from "path";

describe("parseJavaScriptSourceFile", () => {
beforeEach(() => {
Expand Down Expand Up @@ -132,25 +133,21 @@ describe("parseTypeScriptSourceFile", () => {
});

it("should return ParsingError with undefined line when file is not part of typescript project", () => {
const file = __dirname + "/fixtures/ts-project/excluded.ts";
const file = path.join(path.basename(__dirname), "/fixtures/ts-project/excluded.ts");
const parsingError = parseTypeScriptSourceFile(`if (b == 0) {}`, file, [
__dirname + "/fixtures/ts-project/tsconfig.json",
]) as ParsingError;
expect(parsingError).toBeDefined();
expect(parsingError.line).toBeUndefined();
expect(parsingError.message).toEqual(
`If \"parserOptions.project\" has been set for @typescript-eslint/parser, ${file} must be included in at least one of the projects provided.`,
`\"parserOptions.project\" has been set for @typescript-eslint/parser.\nThe file does not match your project config: ${file}.\nThe file must be included in at least one of the projects provided.`,
);
});

it("should throw a parsing exception with TypeScript version below minimum expected", () => {
let parsingException = undefined;
try {
loggerFn(
`WARNING: You are currently running a version of TypeScript which is not officially supported by typescript-estree.
YOUR TYPESCRIPT VERSION: 1.2.3
`,
);
checkTypeScriptVersionCompatibility("1.2.3");
} catch (exception) {
parsingException = exception;
}
Expand All @@ -164,45 +161,14 @@ describe("parseTypeScriptSourceFile", () => {
it("should log a warning with TypeScript version above maximum expected", () => {
console.log = jest.fn();

loggerFn(
`WARNING: You are currently running a version of TypeScript which is not officially supported by typescript-estree.
YOUR TYPESCRIPT VERSION: 3.6.0
`,
);
checkTypeScriptVersionCompatibility("3.8.5");
expect(console.log).toHaveBeenCalledWith(
"WARN You are using version of TypeScript 3.6.0 which is not officially supported; supported versions >=3.2.1 <3.6.0",
"WARN You are using version of TypeScript 3.8.5 which is not officially supported; supported versions >=3.2.1 <3.8.0",
);

jest.resetAllMocks();
});

it("should throw a parsing exception with missing TypeScript version", () => {
let parsingException = undefined;
try {
loggerFn(
`WARNING: You are currently running a version of TypeScript which is not officially supported by typescript-estree.
YOUR TYPESCRIPT VERSION:
`,
);
} catch (exception) {
parsingException = exception;
}
expect(parsingException).toBeDefined;
expect(parsingException).toEqual({
message:
"You are using version of TypeScript which is not supported; supported versions >=3.2.1",
});
});

it("should fall back to default logging behaviour of 'typescript-estree'", () => {
console.log = jest.fn();

loggerFn("Just message");
expect(console.log).toHaveBeenCalledWith("Just message");

jest.resetAllMocks();
});

it("should return correct parsing exception code from exception message", () => {
expect(parseExceptionCodeOf("Cannot find module 'typescript'")).toEqual(
ParseExceptionCode.MissingTypeScript,
Expand Down
90 changes: 59 additions & 31 deletions eslint-bridge/yarn.lock
Expand Up @@ -467,43 +467,46 @@
dependencies:
"@types/yargs-parser" "*"

"@typescript-eslint/eslint-plugin@2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.0.0.tgz#609a5d7b00ce21a6f94d7ef282eba9da57ca1e42"
integrity sha512-Mo45nxTTELODdl7CgpZKJISvLb+Fu64OOO2ZFc2x8sYSnUpFrBUW3H+H/ZGYmEkfnL6VkdtOSxgdt+Av79j0sA==
"@typescript-eslint/eslint-plugin@2.6.0":
version "2.6.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.6.0.tgz#e82ed43fc4527b21bfe35c20a2d6e4ed49fc7957"
integrity sha512-iCcXREU4RciLmLniwKLRPCOFVXrkF7z27XuHq5DrykpREv/mz6ztKAyLg2fdkM0hQC7659p5ZF5uStH7uzAJ/w==
dependencies:
"@typescript-eslint/experimental-utils" "2.0.0"
eslint-utils "^1.4.0"
"@typescript-eslint/experimental-utils" "2.6.0"
eslint-utils "^1.4.2"
functional-red-black-tree "^1.0.1"
regexpp "^2.0.1"
tsutils "^3.14.0"
tsutils "^3.17.1"

"@typescript-eslint/experimental-utils@2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-2.0.0.tgz#f3d298bb411357f35c4184e24280b256b6321949"
integrity sha512-XGJG6GNBXIEx/mN4eTRypN/EUmsd0VhVGQ1AG+WTgdvjHl0G8vHhVBHrd/5oI6RRYBRnedNymSYWW1HAdivtmg==
"@typescript-eslint/experimental-utils@2.6.0":
version "2.6.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-2.6.0.tgz#ed70bef72822bff54031ff0615fc888b9e2b6e8a"
integrity sha512-34BAFpNOwHXeqT+AvdalLxOvcPYnCxA5JGmBAFL64RGMdP0u65rXjii7l/nwpgk5aLEE1LaqF+SsCU0/Cb64xA==
dependencies:
"@types/json-schema" "^7.0.3"
"@typescript-eslint/typescript-estree" "2.0.0"
eslint-scope "^4.0.0"
"@typescript-eslint/typescript-estree" "2.6.0"
eslint-scope "^5.0.0"

"@typescript-eslint/parser@2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-2.0.0.tgz#4273bb19d03489daf8372cdaccbc8042e098178f"
integrity sha512-ibyMBMr0383ZKserIsp67+WnNVoM402HKkxqXGlxEZsXtnGGurbnY90pBO3e0nBUM7chEEOcxUhgw9aPq7fEBA==
"@typescript-eslint/parser@2.6.0":
version "2.6.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-2.6.0.tgz#5106295c6a7056287b4719e24aae8d6293d5af49"
integrity sha512-AvLejMmkcjRTJ2KD72v565W4slSrrzUIzkReu1JN34b8JnsEsxx7S9Xx/qXEuMQas0mkdUfETr0j3zOhq2DIqQ==
dependencies:
"@types/eslint-visitor-keys" "^1.0.0"
"@typescript-eslint/experimental-utils" "2.0.0"
"@typescript-eslint/typescript-estree" "2.0.0"
eslint-visitor-keys "^1.0.0"
"@typescript-eslint/experimental-utils" "2.6.0"
"@typescript-eslint/typescript-estree" "2.6.0"
eslint-visitor-keys "^1.1.0"

"@typescript-eslint/typescript-estree@2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-2.0.0.tgz#c9f6c0efd1b11475540d6a55dc973cc5b9a67e77"
integrity sha512-NXbmzA3vWrSgavymlzMWNecgNOuiMMp62MO3kI7awZRLRcsA1QrYWo6q08m++uuAGVbXH/prZi2y1AWuhSu63w==
"@typescript-eslint/typescript-estree@2.6.0":
version "2.6.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-2.6.0.tgz#d3e9d8e001492e2b9124c4d4bd4e7f03c0fd7254"
integrity sha512-A3lSBVIdj2Gp0lFEL6in2eSPqJ33uAc3Ko+Y4brhjkxzjbzLnwBH22CwsW2sCo+iwogfIyvb56/AJri15H0u5Q==
dependencies:
debug "^4.1.1"
glob "^7.1.4"
is-glob "^4.0.1"
lodash.unescape "4.0.1"
semver "^6.2.0"
semver "^6.3.0"

abab@^2.0.0:
version "2.0.2"
Expand Down Expand Up @@ -1324,14 +1327,22 @@ eslint-scope@^4.0.0, eslint-scope@^4.0.3:
esrecurse "^4.1.0"
estraverse "^4.1.1"

eslint-utils@^1.3.1, eslint-utils@^1.4.0:
eslint-scope@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-5.0.0.tgz#e87c8887c73e8d1ec84f1ca591645c358bfc8fb9"
integrity sha512-oYrhJW7S0bxAFDvWqzvMPRm6pcgcnWc4QnofCAqRTRfQC0JcwenzGglTtsLyIuuWFfkqDG9vz67cnttSd53djw==
dependencies:
esrecurse "^4.1.0"
estraverse "^4.1.1"

eslint-utils@^1.3.1, eslint-utils@^1.4.2:
version "1.4.2"
resolved "https://registry.yarnpkg.com/eslint-utils/-/eslint-utils-1.4.2.tgz#166a5180ef6ab7eb462f162fd0e6f2463d7309ab"
integrity sha512-eAZS2sEUMlIeCjBeubdj45dmBHQwPHWyBcT1VSYB7o9x9WRRqKxyUoiXlRjyAwzN7YEzHJlYg0NmzDRWx6GP4Q==
dependencies:
eslint-visitor-keys "^1.0.0"

eslint-visitor-keys@^1.0.0:
eslint-visitor-keys@^1.0.0, eslint-visitor-keys@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.1.0.tgz#e2a82cea84ff246ad6fb57f9bde5b46621459ec2"
integrity sha512-8y9YjtM1JBJU/A9Kc+SbaOV4y29sSWckBwMHa+FGtVj5gN/sbnKDf6xJUl+8g7FAij9LVaP8C24DUiH/f/2Z9A==
Expand Down Expand Up @@ -1756,7 +1767,7 @@ getpass@^0.1.1:
dependencies:
assert-plus "^1.0.0"

glob@^7.1.1, glob@^7.1.2, glob@^7.1.3:
glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4:
version "7.1.4"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.4.tgz#aa608a2f6c577ad357e1ae5a5c26d9a8d1969255"
integrity sha512-hkLPepehmnKk41pUGm3sYxoFs/umurYfYJCerbXEyFIWcAzvpipAgVkBqqT9RBKMGjnq6kMuyYwha6csxbiM1A==
Expand Down Expand Up @@ -2092,6 +2103,11 @@ is-extendable@^1.0.1:
dependencies:
is-plain-object "^2.0.4"

is-extglob@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2"
integrity sha1-qIwCU1eR8C7TfHahueqXc8gz+MI=

is-fullwidth-code-point@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz#ef9e31386f031a7f0d643af82fde50c457ef00cb"
Expand All @@ -2109,6 +2125,13 @@ is-generator-fn@^2.0.0:
resolved "https://registry.yarnpkg.com/is-generator-fn/-/is-generator-fn-2.1.0.tgz#7d140adc389aaf3011a8f2a2a4cfa6faadffb118"
integrity sha512-cTIB4yPYL/Grw0EaSzASzg6bBy9gqCofvWN8okThAYIxKJZC+udlRAmGbM0XLeniEJSs8uEgHPGuHSe1XsOLSQ==

is-glob@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-4.0.1.tgz#7567dbe9f2f5e2467bc77ab83c4a29482407a5dc"
integrity sha512-5G0tKtBTFImOqDnLB2hG6Bp2qcKEFduo4tZu9MT/H6NQv/ghhy30o55ufafxJ/LdH79LLs2Kfrn85TLKyA7BUg==
dependencies:
is-extglob "^2.1.1"

is-number@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/is-number/-/is-number-3.0.0.tgz#24fd6201a4782cf50561c810276afc7d12d71195"
Expand Down Expand Up @@ -3731,7 +3754,7 @@ sax@^1.2.4:
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==

semver@^6.0.0, semver@^6.2.0:
semver@^6.0.0, semver@^6.2.0, semver@^6.3.0:
version "6.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
Expand Down Expand Up @@ -4217,12 +4240,17 @@ ts-jest@24.1.0:
semver "^5.5"
yargs-parser "10.x"

tslib@^1.8.1, tslib@^1.9.0:
tslib@^1.8.1:
version "1.10.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a"
integrity sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==

tsutils@^3.14.0:
tslib@^1.9.0:
version "1.9.3"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286"
integrity sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ==

tsutils@^3.17.1:
version "3.17.1"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-3.17.1.tgz#ed719917f11ca0dee586272b2ac49e015a2dd759"
integrity sha512-kzeQ5B8H3w60nFY2g8cJIuH7JDpsALXySGtwGJ0p2LSjLgay3NdIpqq5SoOBe46bKDW2iq25irHCr8wjomUS2g==
Expand Down
Expand Up @@ -28,7 +28,6 @@
import org.apache.commons.io.FileUtils;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Test;
import org.sonarqube.ws.Issues.Issue;

Expand Down Expand Up @@ -153,7 +152,7 @@ public void test_incompatible_typescript() throws Exception {
@Test
public void test_new_typescript() throws Exception {
File dir = TestUtils.projectDir("tsproject-no-typescript");
TestUtils.npmInstall(dir, "typescript@3.6.2", "--no-save");
TestUtils.npmInstall(dir, "typescript@3.8.0-dev.20191026", "--no-save");
String projectKey = "tsproject-new-typescript";
SonarScanner build = SonarScanner.create()
.setProjectKey(projectKey)
Expand All @@ -164,7 +163,7 @@ public void test_new_typescript() throws Exception {
Tests.setProfile(projectKey, "eslint-based-rules-profile", "ts");
BuildResult result = orchestrator.executeBuild(build);
assertThat(result.isSuccess()).isTrue();
assertThat(result.getLogsLines(l -> l.contains("You are using version of TypeScript 3.6.2 which is not officially supported; supported versions >=3.2.1 <3.6.0"))).hasSize(1);
assertThat(result.getLogsLines(l -> l.contains("You are using version of TypeScript 3.8.0-dev.20191026 which is not officially supported; supported versions >=3.2.1 <3.8.0"))).hasSize(1);
}

@Test
Expand Down