Skip to content

Commit

Permalink
use semver to compare TS versions
Browse files Browse the repository at this point in the history
  • Loading branch information
saberduck committed Jul 29, 2020
1 parent b9816fa commit c306b04
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
7 changes: 5 additions & 2 deletions eslint-bridge/package.json
Expand Up @@ -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",
Expand All @@ -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": [
Expand All @@ -66,8 +68,9 @@
"eslint-plugin-sonarjs",
"espree",
"express",
"vue-eslint-parser",
"run-node"
"run-node",
"semver",
"vue-eslint-parser"
],
"prettier": {
"printWidth": 100,
Expand Down
5 changes: 3 additions & 2 deletions eslint-bridge/src/parser.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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}`,
};
Expand Down
6 changes: 3 additions & 3 deletions eslint-bridge/tests/parser.test.ts
Expand Up @@ -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();

Expand Down
17 changes: 12 additions & 5 deletions eslint-bridge/yarn.lock
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit c306b04

Please sign in to comment.