Skip to content

Replace eval() dependency check with a globalThis lookup - #1807

Open
YuEfSaEDU wants to merge 1 commit into
jspreadsheet:masterfrom
YuEfSaEDU:fix/no-eval-dependency-check-1805
Open

YuEfSaEDU wants to merge 1 commit into
jspreadsheet:masterfrom
YuEfSaEDU:fix/no-eval-dependency-check-1805

Conversation

@YuEfSaEDU

Copy link
Copy Markdown

The dependency-token check in src/utils/internal.js used

eval('typeof(' + tokens[i] + ') == "undefined"')

to tell undefined names from cell-reference tokens, which bundlers and CSP policies flag. Replaced with:

typeof globalThis[tokens[i]] === 'undefined'

globalThis[token] was chosen over token in globalThis deliberately: the tokens always match [A-Z]+[0-9]+ (valid identifiers, never reserved words), and the typeof form is behaviorally identical to the eval version in every case, including declared-but-undefined globals (where in would diverge) and throwing getters. Verified old-vs-new on six cases — real global, undefined name, A1, AB12, declared-undefined, throwing getter — identical outcomes on all six, including the thrown error on the getter.

dist/index.js is patched in place with the equivalent minified expression (repo convention keeps dist committed in sync with src); no eval( occurrences remain in it. Removing the eval also unblocks scope mangling over that code during future minified rebuilds.

Requires an ES2020 runtime (Node 12+, evergreen browsers); src already relies on ES2017+ (Object.entries in factory.js), so this is no support regression.

Fixes #1805

The dependency-token existence check in executeFormula used
eval('typeof(' + tokens[i] + ') == "undefined"'), which bundlers such as
Vite/Rollup flag and which is unnecessary for a plain global lookup.

Dependency tokens always match /[A-Z]+[0-9]+/ (cell references such as A1),
so checking typeof globalThis[token] === 'undefined' is equivalent: it
returns true for unresolvable names and for globals holding undefined, and
false for any defined global, without invoking the evaluator.

Fixes jspreadsheet#1805
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.

eval() used for formula dependency-token type checking in index.js (bundler flags it, and it's built from parsed formula tokens)

1 participant