Skip to content

Commit d518494

Browse files
authored
Merge pull request #338 from rxbryan/node-feat1
feat: change js parser from cherow to espree
2 parents 2ea3534 + 957865b commit d518494

File tree

4 files changed

+83
-25
lines changed

4 files changed

+83
-25
lines changed

source/loaders/node_loader/bootstrap/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ if(NOT OPTION_BUILD_GUIX)
5252
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/lib/package-lock.json ${CMAKE_CURRENT_BINARY_DIR}
5353
COMMAND ${NPM_EXECUTABLE} --prefix ${CMAKE_CURRENT_BINARY_DIR} install ${CMAKE_CURRENT_BINARY_DIR}
5454
COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_OUTPUT_DIR}/node_modules
55-
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_BINARY_DIR}/node_modules/cherow ${PROJECT_OUTPUT_DIR}/node_modules/cherow
55+
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_BINARY_DIR}/node_modules/espree ${PROJECT_OUTPUT_DIR}/node_modules/espree
5656
COMMAND ${CMAKE_COMMAND} -E echo "${target} dependencies copied from ${CMAKE_CURRENT_BINARY_DIR}/node_modules to ${PROJECT_OUTPUT_DIR}/node_modules"
5757
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/lib/package.json ${CMAKE_CURRENT_SOURCE_DIR}/lib/package-lock.json
5858
)
@@ -66,14 +66,14 @@ if(NOT OPTION_BUILD_GUIX)
6666
add_dependencies(${target} ${target}_depends)
6767

6868
install(DIRECTORY
69-
${CMAKE_CURRENT_BINARY_DIR}/node_modules/cherow
69+
${CMAKE_CURRENT_BINARY_DIR}/node_modules/espree
7070
DESTINATION ${INSTALL_LIB}/node_modules
7171
COMPONENT runtime
7272
)
7373
else()
74-
# Guix stores cherow dependency previously to the build in the output directory
74+
# Guix stores espree dependency previously to the build in the output directory
7575
install(DIRECTORY
76-
${PROJECT_OUTPUT_DIR}/node_modules/cherow
76+
${PROJECT_OUTPUT_DIR}/node_modules/espree
7777
DESTINATION ${INSTALL_LIB}/node_modules
7878
COMPONENT runtime
7979
)

source/loaders/node_loader/bootstrap/lib/bootstrap.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const util = require('util');
77
const fs = require('fs');
88

99
/* Require the JavaScript parser */
10-
const cherow = require(path.join(__dirname, 'node_modules', 'cherow'));
10+
const espree = require(path.join(__dirname, 'node_modules', 'espree'));
1111

1212
const node_require = Module.prototype.require;
1313
const node_resolve = require.resolve;
@@ -48,7 +48,8 @@ function node_loader_trampoline_is_callable(value) {
4848

4949
function node_loader_trampoline_is_valid_symbol(node) {
5050
// TODO: Enable more function types
51-
return node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression';
51+
return node.type === 'FunctionDeclaration'
52+
|| node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression';
5253
}
5354

5455
function node_loader_trampoline_module(m) {
@@ -265,17 +266,15 @@ function node_loader_trampoline_discover_function(func) {
265266
if (node_loader_trampoline_is_callable(func)) {
266267
// Cherow can't parse native code functions so we can do a workaround
267268
const str = func.toString().replace('{ [native code] }', '{}');
268-
269-
const ast = cherow.parse(`(${str})`, {
270-
module: false,
271-
skipShebang: true,
272-
}).body[0];
273-
274-
const node = ast.expression;
269+
const ast = espree.parse(str, {
270+
ecmaVersion: 13
271+
});
272+
273+
const node = (ast.body[0].type === 'ExpressionStatement') ?
274+
ast.body[0].expression : ast.body[0];
275275

276276
if (node_loader_trampoline_is_valid_symbol(node)) {
277277
const args = node_loader_trampoline_discover_arguments(node);
278-
279278
const discover = {
280279
ptr: func,
281280
signature: args,
@@ -285,7 +284,7 @@ function node_loader_trampoline_discover_function(func) {
285284
if (node.id && node.id.name) {
286285
discover['name'] = node.id.name;
287286
}
288-
287+
289288
return discover;
290289
}
291290
}

source/loaders/node_loader/bootstrap/lib/package-lock.json

Lines changed: 68 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/loaders/node_loader/bootstrap/lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"author": "",
1111
"license": "ISC",
1212
"dependencies": {
13-
"cherow": "^1.6.9"
13+
"espree": "^9.4.0"
1414
}
1515
}

0 commit comments

Comments
 (0)