Skip to content

Commit

Permalink
Only parse # as comment in .babelignore at line start (#16925)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo authored Oct 23, 2024
1 parent 816b293 commit fdc456a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
6 changes: 4 additions & 2 deletions packages/babel-core/src/config/files/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,10 @@ const readIgnoreConfig = makeStaticFileCache((filepath, content) => {
const ignoreDir = path.dirname(filepath);
const ignorePatterns = content
.split("\n")
.map<string>(line => line.replace(/#.*$/, "").trim())
.filter(line => !!line);
.map(line =>
line.replace(process.env.BABEL_8_BREAKING ? /^#.*$/ : /#.*$/, "").trim(),
)
.filter(Boolean);

for (const pattern of ignorePatterns) {
if (pattern[0] === "!") {
Expand Down
33 changes: 27 additions & 6 deletions packages/babel-core/test/config-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from "path";
import { fileURLToPath } from "url";
import * as babel from "../lib/index.js";
import rimraf from "rimraf";
import { itGte, itLt } from "$repo-utils";
import { itBabel7, itBabel8, itGte, itLt } from "$repo-utils";

import _getTargets from "@babel/helper-compilation-targets";
const getTargets = _getTargets.default || _getTargets;
Expand Down Expand Up @@ -1344,12 +1344,33 @@ describe("buildConfigChain", function () {
);
});

it("should load .babelignore", () => {
const filename = fixture("config-files", "babelignore", "src.js");
itBabel7("should load .babelignore", () => {
const loadOptions = name => {
const filename = fixture("config-files", "babelignore", name);
return loadOptionsSync({ filename, cwd: path.dirname(filename) });
};

expect(
loadOptionsSync({ filename, cwd: path.dirname(filename) }),
).toBeNull();
expect(loadOptions("src.js")).toBeNull();
expect(loadOptions("bar.js")).not.toBeNull();
expect(loadOptions("#baz.js")).not.toBeNull();

// This changes in Babel 8
expect(loadOptions("foo.js#.js")).not.toBeNull();
expect(loadOptions("foo.js")).toBeNull();
});

itBabel8("should load .babelignore", () => {
const loadOptions = name => {
const filename = fixture("config-files", "babelignore", name);
return loadOptionsSync({ filename, cwd: path.dirname(filename) });
};

expect(loadOptions("src.js")).toBeNull();
expect(loadOptions("bar.js")).not.toBeNull();
expect(loadOptions("#baz.js")).not.toBeNull();

expect(loadOptions("foo.js#.js")).toBeNull();
expect(loadOptions("foo.js")).not.toBeNull();
});

test.each(
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
src.js
foo.js#.js
#bar.js
\#baz.js

0 comments on commit fdc456a

Please sign in to comment.