From c306b0408b8db1c2eac4b499c7f3b4e72aaa9e24 Mon Sep 17 00:00:00 2001 From: Tibor Blenessy Date: Wed, 29 Jul 2020 14:56:21 +0200 Subject: [PATCH] use semver to compare TS versions --- eslint-bridge/package.json | 7 +++++-- eslint-bridge/src/parser.ts | 5 +++-- eslint-bridge/tests/parser.test.ts | 6 +++--- eslint-bridge/yarn.lock | 17 ++++++++++++----- .../it/plugin/TypeScriptAnalysisTest.java | 5 +++-- 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/eslint-bridge/package.json b/eslint-bridge/package.json index 7b9733dba20..1730701d4e2 100644 --- a/eslint-bridge/package.json +++ b/eslint-bridge/package.json @@ -31,6 +31,7 @@ "@types/express": "4.16.0", "@types/jest": "23.3.2", "@types/node": "13.1.4", + "@types/semver": "^7.3.1", "fs-extra": "7.0.0", "jest": "24.9.0", "jest-sonar-reporter": "1.3.0", @@ -52,6 +53,7 @@ "espree": "7.2.0", "express": "4.16.3", "run-node": "1.0.0", + "semver": "7.3.2", "vue-eslint-parser": "6.0.4" }, "bundledDependencies": [ @@ -66,8 +68,9 @@ "eslint-plugin-sonarjs", "espree", "express", - "vue-eslint-parser", - "run-node" + "run-node", + "semver", + "vue-eslint-parser" ], "prettier": { "printWidth": 100, diff --git a/eslint-bridge/src/parser.ts b/eslint-bridge/src/parser.ts index b8cdf393cfd..157b43fdd7d 100644 --- a/eslint-bridge/src/parser.ts +++ b/eslint-bridge/src/parser.ts @@ -22,6 +22,7 @@ import * as babel from 'babel-eslint'; import { Linter, SourceCode } from 'eslint'; import { ParsingError } from './analyzer'; import * as VueJS from 'vue-eslint-parser'; +import * as semver from 'semver'; // this value is taken from typescript-estree // still we might consider extending this range @@ -122,13 +123,13 @@ export function resetReportedNewerTypeScriptVersion() { // exported for testing export function checkTypeScriptVersionCompatibility(currentVersion: string) { - if (currentVersion >= TYPESCRIPT_MAXIMUM_VERSION && !reportedNewerTypeScriptVersion) { + if (semver.gt(currentVersion, TYPESCRIPT_MAXIMUM_VERSION) && !reportedNewerTypeScriptVersion) { reportedNewerTypeScriptVersion = true; 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) { + } else if (semver.lt(currentVersion, TYPESCRIPT_MINIMUM_VERSION)) { throw { message: `You are using version of TypeScript ${currentVersion} which is not supported; supported versions >=${TYPESCRIPT_MINIMUM_VERSION}`, }; diff --git a/eslint-bridge/tests/parser.test.ts b/eslint-bridge/tests/parser.test.ts index e3b375ed106..10a1b44aff4 100644 --- a/eslint-bridge/tests/parser.test.ts +++ b/eslint-bridge/tests/parser.test.ts @@ -194,12 +194,12 @@ describe('parseTypeScriptSourceFile', () => { it('should log a warning with TypeScript version above maximum expected', () => { console.log = jest.fn(); resetReportedNewerTypeScriptVersion(); - checkTypeScriptVersionCompatibility('3.8.5'); + checkTypeScriptVersionCompatibility('5.0.0'); expect(console.log).toHaveBeenCalledWith( - 'WARN You are using version of TypeScript 3.8.5 which is not officially supported; supported versions >=3.3.1 <3.10.0', + 'WARN You are using version of TypeScript 5.0.0 which is not officially supported; supported versions >=3.3.1 <3.10.0', ); console.log = jest.fn(); - checkTypeScriptVersionCompatibility('3.8.5'); + checkTypeScriptVersionCompatibility('5.0.0'); // should log only once expect(console.log).not.toHaveBeenCalled(); diff --git a/eslint-bridge/yarn.lock b/eslint-bridge/yarn.lock index d43153161a0..eb3cf1df10c 100644 --- a/eslint-bridge/yarn.lock +++ b/eslint-bridge/yarn.lock @@ -503,6 +503,13 @@ resolved "https://registry.yarnpkg.com/@types/range-parser/-/range-parser-1.2.3.tgz#7ee330ba7caafb98090bece86a5ee44115904c2c" integrity sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA== +"@types/semver@^7.3.1": + version "7.3.1" + resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.1.tgz#7a9a5d595b6d873f338c867dcef64df289468cfa" + integrity sha512-ooD/FJ8EuwlDKOI6D9HWxgIgJjMg2cuziXm/42npDC8y4NjxplBUn9loewZiBNCt44450lHAU0OSb51/UqXeag== + dependencies: + "@types/node" "*" + "@types/serve-static@*": version "1.13.5" resolved "https://registry.yarnpkg.com/@types/serve-static/-/serve-static-1.13.5.tgz#3d25d941a18415d3ab092def846e135a08bbcf53" @@ -3597,16 +3604,16 @@ sax@^1.2.4: resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7" integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ== +semver@7.3.2, semver@^7.2.1, semver@^7.3.2: + version "7.3.2" + resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.2.tgz#604962b052b81ed0786aae84389ffba70ffd3938" + integrity sha512-OrOb32TeeambH6UrhtShmF7CRDqhL6/5XpPNp2DuRH6+9QLw/orhp72j87v8Qa1ScDkvrrBNpZcDejAirJmfXQ== + semver@^6.0.0, semver@^6.2.0: version "6.3.0" resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d" integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw== -semver@^7.2.1, semver@^7.3.2: - version "7.3.2" - resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.2.tgz#604962b052b81ed0786aae84389ffba70ffd3938" - integrity sha512-OrOb32TeeambH6UrhtShmF7CRDqhL6/5XpPNp2DuRH6+9QLw/orhp72j87v8Qa1ScDkvrrBNpZcDejAirJmfXQ== - send@0.16.2: version "0.16.2" resolved "https://registry.yarnpkg.com/send/-/send-0.16.2.tgz#6ecca1e0f8c156d141597559848df64730a6bbc1" diff --git a/its/plugin/tests/src/test/java/com/sonar/javascript/it/plugin/TypeScriptAnalysisTest.java b/its/plugin/tests/src/test/java/com/sonar/javascript/it/plugin/TypeScriptAnalysisTest.java index 3c30916d76a..25f62d1cfcb 100644 --- a/its/plugin/tests/src/test/java/com/sonar/javascript/it/plugin/TypeScriptAnalysisTest.java +++ b/its/plugin/tests/src/test/java/com/sonar/javascript/it/plugin/TypeScriptAnalysisTest.java @@ -152,7 +152,8 @@ 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.8.0-dev.20191026", "--no-save"); + String tsVersion = "4.0.0-beta"; + TestUtils.npmInstall(dir, "typescript@" + tsVersion, "--no-save"); String projectKey = "tsproject-new-typescript"; SonarScanner build = SonarScanner.create() .setProjectKey(projectKey) @@ -163,7 +164,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.8.0-dev.20191026 which is not officially supported; supported versions >=3.3.1 <3.10.0"))).hasSize(1); + assertThat(result.getLogsLines(l -> l.contains("You are using version of TypeScript " + tsVersion + " which is not officially supported; supported versions >=3.3.1 <3.10.0"))).hasSize(1); } @Test