Skip to content

Commit c29471e

Browse files
committed
switch jest to hermes
1 parent 80f2e10 commit c29471e

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"google-closure-compiler": "^20200517.0.0",
6868
"gzip-size": "^5.1.1",
6969
"hermes-eslint": "^0.9.0",
70+
"hermes-parser": "^0.9.0",
7071
"jasmine-check": "^1.0.0-rc.0",
7172
"jest": "^26.6.3",
7273
"jest-cli": "^26.6.3",

scripts/babel/transform-test-gate-pragma.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ function transform(babel) {
278278
callee.name === 'it' ||
279279
callee.name === 'fit'
280280
) {
281-
const comments = statement.leadingComments;
281+
const comments = getComments(path);
282282
if (comments !== undefined) {
283283
const condition = buildGateCondition(comments);
284284
if (condition !== null) {
@@ -304,7 +304,7 @@ function transform(babel) {
304304
callee.property.type === 'Identifier' &&
305305
callee.property.name === 'only'
306306
) {
307-
const comments = statement.leadingComments;
307+
const comments = getComments(path);
308308
if (comments !== undefined) {
309309
const condition = buildGateCondition(comments);
310310
if (condition !== null) {
@@ -331,4 +331,26 @@ function transform(babel) {
331331
};
332332
}
333333

334+
function getComments(path) {
335+
if (path.node.leadingComments) {
336+
// Babel AST includes comments.
337+
return path.node.leadingComments;
338+
}
339+
// In Hermes AST we need to find the comments by range.
340+
const comments = path.hub.file.ast.comments;
341+
let prevSibling = path.getPrevSibling();
342+
let searchStart;
343+
if (prevSibling.node) {
344+
searchStart = prevSibling.node.end;
345+
} else if (path.parentPath.node) {
346+
searchStart = path.parentPath.node.start;
347+
} else {
348+
throw new Error('Unexpected AST structure');
349+
}
350+
const filteredComments = comments.filter(
351+
c => c.start >= searchStart && c.end <= path.node.start
352+
);
353+
return filteredComments.length > 0 ? filteredComments : undefined;
354+
}
355+
334356
module.exports = transform;

scripts/jest/preprocessor.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require('path');
44

55
const babel = require('@babel/core');
66
const coffee = require('coffee-script');
7+
const hermesParser = require('hermes-parser');
78

89
const tsPreprocessor = require('./typescript/preprocessor');
910
const createCacheKeyFunction = require('fbjs-scripts/jest/createCacheKeyFunction');
@@ -93,7 +94,9 @@ module.exports = {
9394
) {
9495
plugins.push(pathToTransformReactVersionPragma);
9596
}
96-
return babel.transform(
97+
let sourceAst = hermesParser.parse(src, {babel: true});
98+
return babel.transformFromAstSync(
99+
sourceAst,
97100
src,
98101
Object.assign(
99102
{filename: path.relative(process.cwd(), filePath)},

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8749,7 +8749,7 @@ hermes-estree@0.9.0:
87498749
resolved "https://registry.yarnpkg.com/hermes-estree/-/hermes-estree-0.9.0.tgz#026e0abe6db1dcf50a81a79014b779a83db3b814"
87508750
integrity sha512-5DZ7Y0CbHVk8zPqgRCvqp8iw+P05svnQDI1aJFjdqCfXJ/1CZ+8aYpGlhJ29zCG5SE5duGTzSxogAYYI4QqXqw==
87518751

8752-
hermes-parser@0.9.0:
8752+
hermes-parser@0.9.0, hermes-parser@^0.9.0:
87538753
version "0.9.0"
87548754
resolved "https://registry.yarnpkg.com/hermes-parser/-/hermes-parser-0.9.0.tgz#ede3044d50479c61843cef5bbdcea83933d4e4ec"
87558755
integrity sha512-IcvJIlAn+9tpHkP+HTsxWKrIdQPp0gvGrrQmxlL4XnNS+Oh6R/Fpxbcoflm2kY3zgQjEvxZxLiK/2+k3/5wsrw==

0 commit comments

Comments
 (0)