Skip to content

Commit

Permalink
Allow templates to parse v8intrinsics (#11695)
Browse files Browse the repository at this point in the history
* Allow templates to parse v8intrinsics

The `v8intrinsic` and `placeholders` parser plugins conflict, so enabling `placeholders` unconditionally was causing errors for V8's internal codemods. This allows them to set `syntacticPlaceholders = false` (so they'll use the legacy identifier format) and enable `v8intrinsic` by itself.

* Fix linter

* Linter
  • Loading branch information
jridgewell committed Jun 10, 2020
1 parent 36f9798 commit 3fba971
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/babel-template/src/parse.js
Expand Up @@ -27,15 +27,15 @@ export default function parseAndBuildMetadata<T>(
code: string,
opts: TemplateOpts,
): Metadata {
const ast = parseWithCodeFrame(code, opts.parser);

const {
placeholderWhitelist,
placeholderPattern,
preserveComments,
syntacticPlaceholders,
} = opts;

const ast = parseWithCodeFrame(code, opts.parser, syntacticPlaceholders);

t.removePropertiesDeep(ast, {
preserveComments,
});
Expand Down Expand Up @@ -191,13 +191,19 @@ type MetadataState = {
function parseWithCodeFrame(
code: string,
parserOpts: ParserOpts,
syntacticPlaceholders?: boolean,
): BabelNodeFile {
const plugins = (parserOpts.plugins || []).slice();
if (syntacticPlaceholders !== false) {
plugins.push("placeholders");
}

parserOpts = {
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true,
sourceType: "module",
...parserOpts,
plugins: (parserOpts.plugins || []).concat("placeholders"),
plugins,
};

try {
Expand Down
21 changes: 21 additions & 0 deletions packages/babel-template/test/index.js
Expand Up @@ -294,6 +294,17 @@ describe("@babel/template", function () {
template(`%%FOO%%`, { syntacticPlaceholders: false })({
FOO: t.numericLiteral(1),
});
}).toThrow(/Unexpected token.*/);
});

it("disallow manually enabled placeholders", () => {
expect(() => {
template(`%%FOO%%`, {
syntacticPlaceholders: false,
plugins: ["placeholders"],
})({
FOO: t.numericLiteral(1),
});
}).toThrow(/%%.*placeholders can't be used/);
});

Expand All @@ -303,6 +314,16 @@ describe("@babel/template", function () {
});
expect(generator(output).code).toMatchInlineSnapshot(`"1;"`);
});

it("allows v8intrinsics", () => {
const output = template(`%DebugPrint(1)`, {
syntacticPlaceholders: false,
plugins: ["v8intrinsic"],
})();
expect(generator(output).code).toMatchInlineSnapshot(
`"%DebugPrint(1);"`,
);
});
});

describe("undefined", () => {
Expand Down

0 comments on commit 3fba971

Please sign in to comment.