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

Add support for V8 intrinsics via Babel #6496

Merged
merged 1 commit into from Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions CHANGELOG.unreleased.md
Expand Up @@ -836,6 +836,29 @@ type C = (number | string)["toString"];
type D = (keyof T1)["foo"];
```

#### JavaScript: Support formatting code with V8 intrinsics. ([#6496] by [@rreverser])

<!-- prettier-ignore -->
```js
// Input
function doSmth() {
%DebugPrint
(
foo )
}

// Prettier (stable)
SyntaxError: Unexpected token (2:13)
1 | function doSmth() {
> 2 | %DebugPrint
| ^

// Prettier (master)
function doSmth() {
%DebugPrint(foo);
}
```

[#5910]: https://github.com/prettier/prettier/pull/5910
[#6033]: https://github.com/prettier/prettier/pull/6033
[#6186]: https://github.com/prettier/prettier/pull/6186
Expand Down Expand Up @@ -863,6 +886,7 @@ type D = (keyof T1)["foo"];
[#6467]: https://github.com/prettier/prettier/pull/6467
[#6377]: https://github.com/prettier/prettier/pull/6377
[#6604]: https://github.com/prettier/prettier/pull/6604
[#6496]: https://github.com/prettier/prettier/pull/6496
[@brainkim]: https://github.com/brainkim
[@duailibe]: https://github.com/duailibe
[@gavinjoyce]: https://github.com/gavinjoyce
Expand All @@ -872,3 +896,4 @@ type D = (keyof T1)["foo"];
[@bakkot]: https://gibhub.com/bakkot
[@thorn0]: https://github.com/thorn0
[@dcyriller]: https://github.com/dcyriller
[@rreverser]: https://github.com/RReverser
3 changes: 2 additions & 1 deletion src/language-js/parser-babylon.js
Expand Up @@ -37,7 +37,8 @@ function babelOptions(extraOptions, extraPlugins) {
"bigInt",
"throwExpressions",
"logicalAssignment",
"classPrivateMethods"
"classPrivateMethods",
"v8intrinsic"
].concat(extraPlugins)
},
extraOptions
Expand Down
2 changes: 2 additions & 0 deletions src/language-js/printer-estree.js
Expand Up @@ -756,6 +756,8 @@ function printPathNoParens(path, options, print, args) {
printTypeAnnotation(path, options, print)
]);
}
case "V8IntrinsicIdentifier":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you grep the file for “Identifier” to see if there are others to update?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(others don't seem relevant, and, as turns out, even the JSON one is not)

return concat(["%", n.name]);
case "SpreadElement":
case "SpreadElementPattern":
case "RestProperty":
Expand Down
47 changes: 47 additions & 0 deletions tests/v8_intrinsic/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,47 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`intrinsic_call.js 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
function doSmth() {
%DebugPrint
(
foo )
}

function printFunc (
f
) {
if(%
IsAsmWasmCode(f)) console.log("asm.js");
if(

% IsWasmCode(
f))
console.log (
"wasm"
);

console.log
(%
GetFunctioName(f)
);
}

=====================================output=====================================
function doSmth() {
%DebugPrint(foo);
}

function printFunc(f) {
if (%IsAsmWasmCode(f)) console.log("asm.js");
if (%IsWasmCode(f)) console.log("wasm");

console.log(%GetFunctioName(f));
}

================================================================================
`;
24 changes: 24 additions & 0 deletions tests/v8_intrinsic/intrinsic_call.js
@@ -0,0 +1,24 @@
function doSmth() {
%DebugPrint
(
foo )
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you expand the test coverage to have other places where v8 intrinsics are used? One test seems light.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added few more, although it's not very easy to be creative with something like this :)


function printFunc (
f
) {
if(%
IsAsmWasmCode(f)) console.log("asm.js");
if(

% IsWasmCode(
f))
console.log (
"wasm"
);

console.log
(%
GetFunctioName(f)
);
}
1 change: 1 addition & 0 deletions tests/v8_intrinsic/jsfmt.spec.js
@@ -0,0 +1 @@
run_spec(__dirname, ["babel"]);