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

fix: Fix for ReferenceError: pattern is not defined #506

Merged
merged 1 commit into from Apr 24, 2023

Conversation

harmony7
Copy link
Contributor

@harmony7 harmony7 commented Apr 24, 2023

This PR fixes the following error thrown by the compiler when import-ing certain libraries:

> build
> js-compute-runtime bin/index.js bin/main.wasm

file:///Users/komuro/Developer/my-app/node_modules/@fastly/js-compute/src/precompile.js:37
        transpiledPattern = pattern;
        ^

ReferenceError: pattern is not defined
    at Literal (file:///Users/komuro/Developer/my-app/node_modules/@fastly/js-compute/src/precompile.js:37:9)
    at c (file:///Users/komuro/Developer/my-app/node_modules/acorn-walk/dist/walk.mjs:24:18)
    at Object.skipThrough (file:///Users/komuro/Developer/my-app/node_modules/acorn-walk/dist/walk.mjs:180:37)
    at c (file:///Users/komuro/Developer/my-app/node_modules/acorn-walk/dist/walk.mjs:23:22)
    at base.ArrayExpression (file:///Users/komuro/Developer/my-app/node_modules/acorn-walk/dist/walk.mjs:330:16)
    at c (file:///Users/komuro/Developer/my-app/node_modules/acorn-walk/dist/walk.mjs:23:22)
    at Object.skipThrough (file:///Users/komuro/Developer/my-app/node_modules/acorn-walk/dist/walk.mjs:180:37)
    at c (file:///Users/komuro/Developer/my-app/node_modules/acorn-walk/dist/walk.mjs:23:22)
    at base.ArrayExpression (file:///Users/komuro/Developer/my-app/node_modules/acorn-walk/dist/walk.mjs:330:16)
    at c (file:///Users/komuro/Developer/my-app/node_modules/acorn-walk/dist/walk.mjs:23:22)

Node.js v18.6.0
--------------------------------------------------------------------------------

✗ Running [scripts.build]

ERROR: error during execution process (see 'command output' above): exit status 1.

Some time between 1.7 and 1.8 changes were made to use acorn to walk code during src/precompile.js in processing regular expressions. Some of the worker code during the walking was modified, from

const pattern = m.captures[0].node.text;
const flags = m.captures[1]?.node.text || "";
// transpile unicode property escapes
let patternTranspiled;
try {
patternTranspiled = regexpuc(pattern, flags, { unicodePropertyEscapes: 'transform' });
} catch {
// swallow regex parse errors here to instead throw them at the engine level
// this then also avoids regex parser bugs being thrown unnecessarily
patternTranspiled = pattern;
}

to

if (!node.regex) return;
let transpiledPattern;
try {
transpiledPattern = regexpuc(node.regex.pattern, node.regex.flags, {
unicodePropertyEscapes: "transform",
});
} catch {
// swallow regex parse errors here to instead throw them at the engine level
// this then also avoids regex parser bugs being thrown unnecessarily
transpiledPattern = pattern;
}

When this was done, it looks like pattern in the catch block had not also been updated to node.regex.pattern.
This results in the error ReferenceError: pattern is not defined, as an identifier named pattern does not exist in scope.

This PR fixes this error.

@JakeChampion JakeChampion changed the title Fix for ReferenceError: pattern is not defined fix: Fix for ReferenceError: pattern is not defined Apr 24, 2023
@JakeChampion JakeChampion merged commit 107c9be into main Apr 24, 2023
73 checks passed
@JakeChampion JakeChampion deleted the kats/regexfix branch April 24, 2023 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants