diff --git a/packages/babel-template/src/parse.js b/packages/babel-template/src/parse.js index b704d5b3195d..3a0b6596d402 100644 --- a/packages/babel-template/src/parse.js +++ b/packages/babel-template/src/parse.js @@ -27,8 +27,6 @@ export default function parseAndBuildMetadata( code: string, opts: TemplateOpts, ): Metadata { - const ast = parseWithCodeFrame(code, opts.parser); - const { placeholderWhitelist, placeholderPattern, @@ -36,6 +34,8 @@ export default function parseAndBuildMetadata( syntacticPlaceholders, } = opts; + const ast = parseWithCodeFrame(code, opts.parser, syntacticPlaceholders); + t.removePropertiesDeep(ast, { preserveComments, }); @@ -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 { diff --git a/packages/babel-template/test/index.js b/packages/babel-template/test/index.js index cae920c2b5b4..117254bc6fb4 100644 --- a/packages/babel-template/test/index.js +++ b/packages/babel-template/test/index.js @@ -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/); }); @@ -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", () => {