Skip to content

Commit

Permalink
support files ignored by babel
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Pereira committed Feb 9, 2025
1 parent b07053c commit 6b54378
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
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(),
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

0 comments on commit 6b54378

Please sign in to comment.