From 6b543785c89db98e245361876ae62c1d2dda1947 Mon Sep 17 00:00:00 2001 From: Tom Pereira Date: Sun, 9 Feb 2025 13:55:22 +0000 Subject: [PATCH] support files ignored by babel --- CHANGELOG.md | 3 +++ package.json | 2 +- .../runners/utils/_tests/_transpiler.test.ts | 25 ++++++++++++++++++- source/runner/runners/utils/transpiler.ts | 4 +-- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09d844f42..b532635d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ ## Main +Ensure that [babel ignores](https://babeljs.io/docs/options#ignore) do not cause the transpiler to fall over, by supporting the +`null` return from `loadOptions` which occurs when a file is ignored. ## 12.3.3 @@ -2118,6 +2120,7 @@ Not usable for others, only stubs of classes etc. - [@orta] [@tibdex]: https://github.com/tibdex [@tim3trick]: https://github.com/tim3trick [@thawankeane]: https://github.com/thawankeane +[@tomstrepsil: https://github.com/TomStrepsil] [@tychota]: https://github.com/tychota [@urkle]: https://github.com/urkle [@valscion]: https://github.com/valscion diff --git a/package.json b/package.json index abf85b9bd..c69258bd3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "danger", - "version": "12.3.3", + "version": "12.3.4", "description": "Unit tests for Team Culture", "main": "distribution/danger.js", "typings": "distribution/danger.d.ts", diff --git a/source/runner/runners/utils/_tests/_transpiler.test.ts b/source/runner/runners/utils/_tests/_transpiler.test.ts index f50324c52..6aaf45085 100644 --- a/source/runner/runners/utils/_tests/_transpiler.test.ts +++ b/source/runner/runners/utils/_tests/_transpiler.test.ts @@ -2,13 +2,15 @@ jest.mock("fs", () => ({ readFileSync: jest.fn(), realpathSync: {}, existsSync: jest.fn(), + statSync: jest.fn(), + stat: jest.fn(), })) jest.mock("path", () => { const path = jest.requireActual("path") return { ...path, resolve: jest.fn(path.resolve) } }) -import { typescriptify, lookupTSConfig, dirContains } from "../transpiler" +import { typescriptify, lookupTSConfig, dirContains, babelify } from "../transpiler" import * as fs from "fs" import * as path from "path" @@ -87,3 +89,24 @@ describe("dirContains", () => { }) } }) + +describe("babelify", () => { + it("does not throw when a filepath is ignored due to babel options, and should return the content unchanged", () => { + const dangerfile = "import {a} from 'lodash'; a()" + + const existsSyncMock = fs.existsSync as jest.Mock + const actualFs = jest.requireActual("fs") as typeof fs + existsSyncMock.mockImplementation((path) => path === "/a/b/babel.config.js" || actualFs.existsSync(path)) + jest.mock( + "/a/b/babel.config.js", + () => { + return { ignore: ["/a/b"] } + }, + { virtual: true } + ) + const act = () => babelify(dangerfile, "a/b/", []) + + expect(act).not.toThrow() + expect(act()).toEqual(dangerfile) + }) +}) diff --git a/source/runner/runners/utils/transpiler.ts b/source/runner/runners/utils/transpiler.ts index 15ca593fd..31e74815a 100644 --- a/source/runner/runners/utils/transpiler.ts +++ b/source/runner/runners/utils/transpiler.ts @@ -172,7 +172,7 @@ export const babelify = (content: string, filename: string, extraPlugins: string return content } - const options = babel.loadOptions ? babel.loadOptions({ filename }) : { plugins: [] } + const options = babel.loadOptions?.({ filename: filename }) ?? { plugins: [] } const fileOpts = { filename, @@ -186,7 +186,7 @@ export const babelify = (content: string, filename: string, extraPlugins: string const result = transformSync(content, fileOpts) d("Result from Babel:") d(result) - return result.code + return result?.code ?? content } export default (code: string, filename: string, remoteFile: boolean = false) => {