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

Allow templates to parse v8intrinsics #11695

Merged
merged 3 commits into from Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
39 changes: 30 additions & 9 deletions packages/babel-template/test/index.js
Expand Up @@ -4,36 +4,36 @@ import * as t from "@babel/types";

const comments = "// Sum two numbers\nconst add = (a, b) => a + b;";

describe("@babel/template", function () {
it("import statements are allowed by default", function () {
expect(function () {
describe("@babel/template", function() {
it("import statements are allowed by default", function() {
expect(function() {
template("import foo from 'foo'")({});
}).not.toThrow();
});

it("with statements are allowed with sourceType: script", function () {
expect(function () {
it("with statements are allowed with sourceType: script", function() {
expect(function() {
template("with({}){}", { sourceType: "script" })({});
}).not.toThrow();
});

it("should strip comments by default", function () {
it("should strip comments by default", function() {
const code = "const add = (a, b) => a + b;";
const output = template(comments)();
expect(generator(output).code).toBe(code);
});

it("should preserve comments with a flag", function () {
it("should preserve comments with a flag", function() {
const output = template(comments, { preserveComments: true })();
expect(generator(output).code).toBe(comments);
});

it("should preserve comments with a flag", function () {
it("should preserve comments with a flag", function() {
const output = template(comments, { preserveComments: true })();
expect(generator(output).code).toBe(comments);
});

it("should preserve comments with a flag when using .ast", function () {
it("should preserve comments with a flag when using .ast", function() {
const output1 = template.ast(comments, { preserveComments: true });
const output2 = template({ preserveComments: true }).ast(comments);
expect(generator(output1).code).toBe(comments);
Expand Down 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