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

Support files being ignored by babel configuration #1476

Merged
merged 6 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
## Main

<!-- Your comment below this -->
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.
<!-- Your comment above this -->

## 12.3.3
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
25 changes: 24 additions & 1 deletion source/runner/runners/utils/_tests/_transpiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ jest.mock("fs", () => ({
readFileSync: jest.fn(),
realpathSync: {},
existsSync: jest.fn(),
statSync: jest.fn(),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

N.B. These are needed to prevent an early-return within babel.

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"

Expand Down Expand Up @@ -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)
})
})
4 changes: 2 additions & 2 deletions source/runner/runners/utils/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) => {
Expand Down
Loading