From 7f96672851e8493f18870426c31e13d5aaf7ffde Mon Sep 17 00:00:00 2001 From: lha Date: Mon, 13 Feb 2023 09:44:18 +0700 Subject: [PATCH 1/8] Add support for tsconfig.json files that have an extends property --- package.json | 3 +- .../utils/_tests/_fixtures/tsconfig.base.json | 6 +++ .../utils/_tests/_fixtures/tsconfig.json | 3 ++ .../runners/utils/_tests/_transpiler.test.ts | 32 ++++++++----- source/runner/runners/utils/transpiler.ts | 47 ++++++++++--------- yarn.lock | 9 +++- 6 files changed, 64 insertions(+), 36 deletions(-) create mode 100644 source/runner/runners/utils/_tests/_fixtures/tsconfig.base.json create mode 100644 source/runner/runners/utils/_tests/_fixtures/tsconfig.json diff --git a/package.json b/package.json index 9a1850884..b21a6f524 100644 --- a/package.json +++ b/package.json @@ -101,6 +101,7 @@ "@types/async-retry": "^1.4.1", "@types/debug": "0.0.30", "@types/get-stdin": "^5.0.1", + "@types/http-proxy-agent": "^4.0.1", "@types/jest": "^24.0.11", "@types/json5": "^0.0.30", "@types/jsonpointer": "^4.0.0", @@ -142,6 +143,7 @@ "typescript-json-schema": "^0.53.0" }, "dependencies": { + "@gitbeaker/node": "^21.3.0", "@octokit/rest": "^18.12.0", "async-retry": "1.2.3", "chalk": "^2.3.0", @@ -150,7 +152,6 @@ "debug": "^4.1.1", "fast-json-patch": "^3.0.0-1", "get-stdin": "^6.0.0", - "@gitbeaker/node": "^21.3.0", "http-proxy-agent": "^5.0.0", "https-proxy-agent": "^5.0.1", "hyperlinker": "^1.0.0", diff --git a/source/runner/runners/utils/_tests/_fixtures/tsconfig.base.json b/source/runner/runners/utils/_tests/_fixtures/tsconfig.base.json new file mode 100644 index 000000000..9bc1fe55d --- /dev/null +++ b/source/runner/runners/utils/_tests/_fixtures/tsconfig.base.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "target": "ES6", + "module": "ES2015" + } +} diff --git a/source/runner/runners/utils/_tests/_fixtures/tsconfig.json b/source/runner/runners/utils/_tests/_fixtures/tsconfig.json new file mode 100644 index 000000000..ffcbb9477 --- /dev/null +++ b/source/runner/runners/utils/_tests/_fixtures/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "./tsconfig.base.json" +} diff --git a/source/runner/runners/utils/_tests/_transpiler.test.ts b/source/runner/runners/utils/_tests/_transpiler.test.ts index f50324c52..4d32ef86b 100644 --- a/source/runner/runners/utils/_tests/_transpiler.test.ts +++ b/source/runner/runners/utils/_tests/_transpiler.test.ts @@ -1,16 +1,12 @@ -jest.mock("fs", () => ({ - readFileSync: jest.fn(), - realpathSync: {}, - existsSync: jest.fn(), -})) jest.mock("path", () => { const path = jest.requireActual("path") return { ...path, resolve: jest.fn(path.resolve) } }) import { typescriptify, lookupTSConfig, dirContains } from "../transpiler" -import * as fs from "fs" +import fs from "fs" import * as path from "path" +import ts from "typescript" describe("typescriptify", () => { it("removes the module option in a tsconfig", () => { @@ -21,11 +17,25 @@ describe("typescriptify", () => { module: "es2015", }, } - const fsMock = fs.readFileSync as jest.Mock - fsMock.mockImplementationOnce(() => JSON.stringify(fakeTSConfig)) - + jest.spyOn(ts.sys, "readFile").mockImplementationOnce(() => JSON.stringify(fakeTSConfig)) expect(typescriptify(dangerfile, "/a/b")).not.toContain("import") }) + + it("resolves extended tsconfigs", () => { + const actualPath = jest.requireActual("path") as typeof path + const resolve = path.resolve as jest.Mock + resolve.mockImplementation((p: string = "") => actualPath.resolve(__dirname, p)) + + const dangerfile = `import { a } from 'lodash'; (() => a())()` + + const transpiledCode = typescriptify(dangerfile, actualPath.resolve(__dirname, "./_fixtures")) + console.log(transpiledCode) + expect(transpiledCode).not.toContain("import") + + // Arrow functions (=>) are not compiled to functions when the target is ES6. + // The ES6 target is defined in the base tsconfig so it must be inheriting from it. + expect(transpiledCode).toContain("=>") + }) }) /** Normalizes path to platform-specific */ @@ -45,9 +55,9 @@ describe("lookupTSConfig", () => { const resolve = path.resolve as jest.Mock resolve.mockImplementation((p: string = "") => actualPath.resolve(cwd, p)) - const existsSync = fs.existsSync as jest.Mock + const existsSync = jest.spyOn(fs, "existsSync") const tsconfigPath = path.resolve(path.join(configDir, "tsconfig.json")) - existsSync.mockImplementation((f: string) => path.resolve(f) === tsconfigPath) + existsSync.mockImplementation((f: fs.PathLike) => path.resolve(f as string) === tsconfigPath) } it("can find in the same folder as dangerfile", () => { diff --git a/source/runner/runners/utils/transpiler.ts b/source/runner/runners/utils/transpiler.ts index ab4c434ce..3eb52e3b3 100644 --- a/source/runner/runners/utils/transpiler.ts +++ b/source/runner/runners/utils/transpiler.ts @@ -1,6 +1,6 @@ import * as fs from "fs" import * as path from "path" -import JSON5 from "json5" +import ts from "typescript" import { debug } from "../../../debug" const enum BabelPackagePrefix { @@ -120,40 +120,41 @@ export const lookupTSConfig = (dir: string): string | null => { } export const typescriptify = (content: string, dir: string): string => { - const ts = require("typescript") - // Support custom TSC options, but also fallback to defaults - let compilerOptions: any + let compilerOptions: ts.CompilerOptions const tsConfigPath = lookupTSConfig(dir) if (tsConfigPath) { - compilerOptions = JSON5.parse(fs.readFileSync(tsConfigPath, "utf8")) + compilerOptions = ts.parseJsonConfigFileContent( + ts.readConfigFile(tsConfigPath, ts.sys.readFile).config, + { + fileExists: ts.sys.fileExists, + readFile: ts.sys.readFile, + readDirectory: ts.sys.readDirectory, + useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames, + }, + dir + ).options } else { compilerOptions = ts.getDefaultCompilerOptions() } - let result = ts.transpileModule(content, sanitizeTSConfig(compilerOptions)) + let result = ts.transpileModule(content, { compilerOptions: sanitizeTSConfig(compilerOptions) }) return result.outputText } -const sanitizeTSConfig = (config: any) => { - if (!config.compilerOptions) { - return config - } - - const safeConfig = config - - // It can make sense to ship TS code with modules - // for `import`/`export` syntax, but as we're running - // the transpiled code on vanilla node - it'll need to - // be used with plain old commonjs - // - // @see https://github.com/apollographql/react-apollo/pull/1402#issuecomment-351810274 - // - if (safeConfig.compilerOptions.module) { - safeConfig.compilerOptions.module = "commonjs" +const sanitizeTSConfig = (config: ts.CompilerOptions) => { + if (config.module) { + // It can make sense to ship TS code with modules + // for `import`/`export` syntax, but as we're running + // the transpiled code on vanilla node - it'll need to + // be used with plain old commonjs + // + // @see https://github.com/apollographql/react-apollo/pull/1402#issuecomment-351810274 + // + config.module = ts.ModuleKind.CommonJS } - return safeConfig + return config } export const babelify = (content: string, filename: string, extraPlugins: string[]): string => { diff --git a/yarn.lock b/yarn.lock index 7304d434d..6cc822ef3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1576,6 +1576,13 @@ resolved "https://registry.yarnpkg.com/@types/http-cache-semantics/-/http-cache-semantics-4.0.0.tgz#9140779736aa2655635ee756e2467d787cfe8a2a" integrity sha512-c3Xy026kOF7QOTn00hbIllV1dLR9hG9NkSrLQgCVs8NF6sBU+VGWjD3wLPhmh1TYAc7ugCFsvHYMN4VcBN1U1A== +"@types/http-proxy-agent@^4.0.1": + version "4.0.1" + resolved "https://registry.yarnpkg.com/@types/http-proxy-agent/-/http-proxy-agent-4.0.1.tgz#8866d3d0a05095b09be488aa8584c29631d8cfb7" + integrity sha512-2XJTByP9C14Kcq5ClvPRfOgFJxklhuru7xmuSPcNr2CPwRGdpCgIdUUNc0MDb25+bzaSolVN0u4RKH7M+CMimg== + dependencies: + http-proxy-agent "*" + "@types/jest-diff@*": version "20.0.1" resolved "https://registry.yarnpkg.com/@types/jest-diff/-/jest-diff-20.0.1.tgz#35cc15b9c4f30a18ef21852e255fdb02f6d59b89" @@ -5005,7 +5012,7 @@ http-errors@~1.6.1: setprototypeof "1.0.3" statuses ">= 1.3.1 < 2" -http-proxy-agent@^5.0.0: +http-proxy-agent@*, http-proxy-agent@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/http-proxy-agent/-/http-proxy-agent-5.0.0.tgz#5129800203520d434f142bc78ff3c170800f2b43" integrity sha512-n2hY8YdoRE1i7r6M0w9DIw5GgZN0G25P8zLCRQ8rjXtTU3vsNFBI/vWK/UIeE6g5MUUz6avwAPXmL6Fy9D/90w== From 391fda6e3492e07a0a3ea6282d41c9267b742f05 Mon Sep 17 00:00:00 2001 From: lha Date: Mon, 13 Feb 2023 10:05:38 +0700 Subject: [PATCH 2/8] Fixed tests --- .../runner/templates/_tests/_markdownTableTemplate.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/source/runner/templates/_tests/_markdownTableTemplate.test.ts b/source/runner/templates/_tests/_markdownTableTemplate.test.ts index f06205391..dacc404f2 100644 --- a/source/runner/templates/_tests/_markdownTableTemplate.test.ts +++ b/source/runner/templates/_tests/_markdownTableTemplate.test.ts @@ -3,7 +3,10 @@ import { template as markdownTableTemplate } from "../../templates/markdownTable describe("generating markdown tables", () => { it("build a markdown table with the content given", () => { const headers = ["", "Warnings"] - const rows = [["⚠️", "This is a very unimportant warning."], ["⚠️", "But, this warning is pretty important."]] + const rows = [ + ["⚠️", "This is a very unimportant warning."], + ["⚠️", "But, this warning is pretty important."], + ] const table = markdownTableTemplate(headers, rows) From 8a2bba21a087bda3d5a0808a25c9983e546332bd Mon Sep 17 00:00:00 2001 From: lha Date: Mon, 13 Feb 2023 10:13:32 +0700 Subject: [PATCH 3/8] test --- .github/workflows/CI.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 7af685cf3..ee6d25629 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -15,7 +15,7 @@ jobs: # Get local dependencies & test - run: yarn install - run: yarn build - - run: yarn test + - run: yarn test -u # Validate related tooling - run: yarn declarations From 0c757ede6bc379428c74c77f45e38f1ec17021be Mon Sep 17 00:00:00 2001 From: lha Date: Mon, 13 Feb 2023 10:21:11 +0700 Subject: [PATCH 4/8] test --- .github/workflows/CI.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index ee6d25629..37214ccc3 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -16,6 +16,8 @@ jobs: - run: yarn install - run: yarn build - run: yarn test -u + env: + FORCE_COLOR: true # Validate related tooling - run: yarn declarations From 6e2a9394ca22af88e65843cb5b3e94c09a3468dd Mon Sep 17 00:00:00 2001 From: lha Date: Mon, 13 Feb 2023 10:27:52 +0700 Subject: [PATCH 5/8] test --- .github/workflows/CI.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 37214ccc3..5ce45026f 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -15,7 +15,7 @@ jobs: # Get local dependencies & test - run: yarn install - run: yarn build - - run: yarn test -u + - run: yarn test env: FORCE_COLOR: true From 95ebb5f7a836aca5b57b5ace305dde7275c2c6df Mon Sep 17 00:00:00 2001 From: lha Date: Mon, 13 Feb 2023 10:39:53 +0700 Subject: [PATCH 6/8] test --- .../__snapshots__/_markdownTableTemplate.test.ts.snap | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/source/runner/templates/_tests/__snapshots__/_markdownTableTemplate.test.ts.snap b/source/runner/templates/_tests/__snapshots__/_markdownTableTemplate.test.ts.snap index e671c8724..d0065d0e2 100644 --- a/source/runner/templates/_tests/__snapshots__/_markdownTableTemplate.test.ts.snap +++ b/source/runner/templates/_tests/__snapshots__/_markdownTableTemplate.test.ts.snap @@ -1,8 +1,10 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`generating markdown tables build a markdown table with the content given 1`] = ` -"| | Warnings | +" +| | Warnings | | --- | --- | | ⚠️ | This is a very unimportant warning. | -| ⚠️ | But, this warning is pretty important. |" +| ⚠️ | But, this warning is pretty important. | + " `; From 74c05b40cdfe728c2b3f8d43cbd753ceb19fa1c1 Mon Sep 17 00:00:00 2001 From: lha Date: Mon, 13 Feb 2023 10:40:17 +0700 Subject: [PATCH 7/8] test --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index b21a6f524..05ffeca4a 100644 --- a/package.json +++ b/package.json @@ -101,7 +101,6 @@ "@types/async-retry": "^1.4.1", "@types/debug": "0.0.30", "@types/get-stdin": "^5.0.1", - "@types/http-proxy-agent": "^4.0.1", "@types/jest": "^24.0.11", "@types/json5": "^0.0.30", "@types/jsonpointer": "^4.0.0", From 9b5df4f8a6ec52f4c8b533dd422e8386c492d771 Mon Sep 17 00:00:00 2001 From: lha Date: Mon, 13 Feb 2023 10:53:13 +0700 Subject: [PATCH 8/8] test --- .github/workflows/CI.yml | 2 -- .../__snapshots__/_markdownTableTemplate.test.ts.snap | 6 ++---- yarn.lock | 9 +-------- 3 files changed, 3 insertions(+), 14 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 5ce45026f..7af685cf3 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -16,8 +16,6 @@ jobs: - run: yarn install - run: yarn build - run: yarn test - env: - FORCE_COLOR: true # Validate related tooling - run: yarn declarations diff --git a/source/runner/templates/_tests/__snapshots__/_markdownTableTemplate.test.ts.snap b/source/runner/templates/_tests/__snapshots__/_markdownTableTemplate.test.ts.snap index d0065d0e2..e671c8724 100644 --- a/source/runner/templates/_tests/__snapshots__/_markdownTableTemplate.test.ts.snap +++ b/source/runner/templates/_tests/__snapshots__/_markdownTableTemplate.test.ts.snap @@ -1,10 +1,8 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`generating markdown tables build a markdown table with the content given 1`] = ` -" -| | Warnings | +"| | Warnings | | --- | --- | | ⚠️ | This is a very unimportant warning. | -| ⚠️ | But, this warning is pretty important. | - " +| ⚠️ | But, this warning is pretty important. |" `; diff --git a/yarn.lock b/yarn.lock index 6cc822ef3..7304d434d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1576,13 +1576,6 @@ resolved "https://registry.yarnpkg.com/@types/http-cache-semantics/-/http-cache-semantics-4.0.0.tgz#9140779736aa2655635ee756e2467d787cfe8a2a" integrity sha512-c3Xy026kOF7QOTn00hbIllV1dLR9hG9NkSrLQgCVs8NF6sBU+VGWjD3wLPhmh1TYAc7ugCFsvHYMN4VcBN1U1A== -"@types/http-proxy-agent@^4.0.1": - version "4.0.1" - resolved "https://registry.yarnpkg.com/@types/http-proxy-agent/-/http-proxy-agent-4.0.1.tgz#8866d3d0a05095b09be488aa8584c29631d8cfb7" - integrity sha512-2XJTByP9C14Kcq5ClvPRfOgFJxklhuru7xmuSPcNr2CPwRGdpCgIdUUNc0MDb25+bzaSolVN0u4RKH7M+CMimg== - dependencies: - http-proxy-agent "*" - "@types/jest-diff@*": version "20.0.1" resolved "https://registry.yarnpkg.com/@types/jest-diff/-/jest-diff-20.0.1.tgz#35cc15b9c4f30a18ef21852e255fdb02f6d59b89" @@ -5012,7 +5005,7 @@ http-errors@~1.6.1: setprototypeof "1.0.3" statuses ">= 1.3.1 < 2" -http-proxy-agent@*, http-proxy-agent@^5.0.0: +http-proxy-agent@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/http-proxy-agent/-/http-proxy-agent-5.0.0.tgz#5129800203520d434f142bc78ff3c170800f2b43" integrity sha512-n2hY8YdoRE1i7r6M0w9DIw5GgZN0G25P8zLCRQ8rjXtTU3vsNFBI/vWK/UIeE6g5MUUz6avwAPXmL6Fy9D/90w==