Skip to content

Commit

Permalink
Add support for V8 intrinsics via Babel
Browse files Browse the repository at this point in the history
This plugin was recently added on Babel side and allows to parse custom syntax for V8 intrinsics.

They don't clash with any real-world JavaScript syntax, so adding this option should be as safe as any other plugin, and would allow to format JavaScript that uses such intrinsics (e.g. code inside Node.js or V8 itself).
  • Loading branch information
RReverser committed Sep 18, 2019
1 parent 70d6352 commit 62021b6
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 10 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.unreleased.md
Expand Up @@ -598,6 +598,28 @@ export {
export { fooooooooooooooooooooooooooooooooooooooooooooooooo } from "fooooooooooooooooooooooooooooo";
```
#### JavaScript: Support formatting code with V8 intrinsics. ([#6496] by [@rreverser])
```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
[#6186]: https://github.com/prettier/prettier/pull/6186
[#6206]: https://github.com/prettier/prettier/pull/6206
Expand All @@ -619,10 +641,12 @@ export { fooooooooooooooooooooooooooooooooooooooooooooooooo } from "fooooooooooo
[#6438]: https://github.com/prettier/prettier/pull/6411
[#6441]: https://github.com/prettier/prettier/pull/6441
[#6446]: https://github.com/prettier/prettier/pull/6446
[#6496]: https://github.com/prettier/prettier/pull/6496
[@duailibe]: https://github.com/duailibe
[@gavinjoyce]: https://github.com/gavinjoyce
[@sosukesuzuki]: https://github.com/sosukesuzuki
[@g-harel]: https://github.com/g-harel
[@jounqin]: https://github.com/JounQin
[@bakkot]: https://gibhub.com/bakkot
[@thorn0]: https://github.com/thorn0
[@rreverser]: https://github.com/RReverser
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -16,7 +16,7 @@
"dependencies": {
"@angular/compiler": "7.2.9",
"@babel/code-frame": "7.5.5",
"@babel/parser": "7.2.0",
"@babel/parser": "7.6.0",
"@glimmer/syntax": "0.41.0",
"@iarna/toml": "2.2.3",
"@typescript-eslint/typescript-estree": "1.11.0",
Expand Down
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
4 changes: 4 additions & 0 deletions src/language-js/postprocess.js
Expand Up @@ -6,6 +6,10 @@ const { getLast } = require("../common/util");
function postprocess(ast, options) {
visitNode(ast, node => {
switch (node.type) {
case "V8IntrinsicIdentifier":
node.name = `%${node.name}`;
node.type = "Identifier";
break;
case "VariableDeclaration": {
const lastDeclaration = getLast(node.declarations);
if (lastDeclaration && lastDeclaration.init) {
Expand Down
21 changes: 21 additions & 0 deletions tests/v8_intrinsic/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`intrinsic_call.js 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
function doSmth() {
%DebugPrint
(
foo )
}
=====================================output=====================================
function doSmth() {
%DebugPrint(foo);
}
================================================================================
`;
5 changes: 5 additions & 0 deletions tests/v8_intrinsic/intrinsic_call.js
@@ -0,0 +1,5 @@
function doSmth() {
%DebugPrint
(
foo )
}
1 change: 1 addition & 0 deletions tests/v8_intrinsic/jsfmt.spec.js
@@ -0,0 +1 @@
run_spec(__dirname, ["babel"]);
12 changes: 4 additions & 8 deletions yarn.lock
Expand Up @@ -282,9 +282,10 @@
esutils "^2.0.2"
js-tokens "^4.0.0"

"@babel/parser@7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.2.0.tgz#02d01dbc330b6cbf36b76ac93c50752c69027065"
"@babel/parser@7.6.0", "@babel/parser@^7.6.0":
version "7.6.0"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.6.0.tgz#3e05d0647432a8326cb28d0de03895ae5a57f39b"
integrity sha512-+o2q111WEx4srBs7L9eJmcwi655eD8sXniLqMB93TBK9GrNzGrxDWSjiqz2hLU0Ha8MTXFIP0yd9fNdP+m43ZQ==

"@babel/parser@^7.1.2":
version "7.1.2"
Expand All @@ -304,11 +305,6 @@
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.5.5.tgz#02f077ac8817d3df4a832ef59de67565e71cca4b"
integrity sha512-E5BN68cqR7dhKan1SfqgPGhQ178bkVKpXTPEXnFJBrEt8/DKRZlybmy+IgYLTeN7tp1R5Ccmbm2rBk17sHYU3g==

"@babel/parser@^7.6.0":
version "7.6.0"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.6.0.tgz#3e05d0647432a8326cb28d0de03895ae5a57f39b"
integrity sha512-+o2q111WEx4srBs7L9eJmcwi655eD8sXniLqMB93TBK9GrNzGrxDWSjiqz2hLU0Ha8MTXFIP0yd9fNdP+m43ZQ==

"@babel/plugin-proposal-async-generator-functions@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-async-generator-functions/-/plugin-proposal-async-generator-functions-7.2.0.tgz#b289b306669dce4ad20b0252889a15768c9d417e"
Expand Down

0 comments on commit 62021b6

Please sign in to comment.