-
Notifications
You must be signed in to change notification settings - Fork 91
feat(senpi-codemode): expose CommonJS in JS eval cells #1286
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
base: main
Are you sure you want to change the base?
Changes from all commits
dfaa375
a94e4e3
1f5b5a0
94a3504
8c6f1eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,14 +11,15 @@ export async function awaitMaybePromise(value) { | |
|
|
||
| export function wrapUserCode(code) { | ||
| const persistentCode = persistTopLevelDeclarations(code); | ||
| if (/\breturn\b/u.test(persistentCode)) return `(async () => {\n${persistentCode}\n})()`; | ||
| return `(async () => {\n${captureLastExpression(persistentCode)}\n})()`; | ||
| const body = /\breturn\b/u.test(persistentCode) ? persistentCode : captureLastExpression(persistentCode); | ||
| return `(async (exports, require, module, __filename, __dirname) => {\n${body}\n})(globalThis.exports, globalThis.require, globalThis.module, globalThis.__filename, globalThis.__dirname)`; | ||
| } | ||
|
|
||
| const IDENTIFIER_START_RE = /[$_\p{ID_Start}]/u; | ||
| const IDENTIFIER_CONTINUE_RE = /[$_\p{ID_Continue}\u200c\u200d]/u; | ||
| const IDENTIFIER_RE = /^[\p{ID_Start}$_][\p{ID_Continue}\u200c\u200d]*$/u; | ||
| const DECLARATION_KEYWORDS = new Set(["const", "let", "var"]); | ||
| const COMMONJS_RESERVED_NAMES = new Set(["exports", "require", "module", "__filename", "__dirname"]); | ||
| const REGEX_PREFIX_KEYWORDS = new Set([ | ||
| "await", | ||
| "case", | ||
|
|
@@ -265,6 +266,7 @@ function rewriteDeclaration(code, declarationStart, start, end, keyword) { | |
| const bindings = []; | ||
| collectPatternNames(pattern, bindings); | ||
| if (bindings.length === 0) return undefined; | ||
| if (bindings.some((name) => COMMONJS_RESERVED_NAMES.has(name))) return undefined; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: The guard makes a reserved-name declaration "SyntaxError against the CJS wrapper parameters instead of overwriting the persistent bindings", but that holds only for Prompt for AI agents |
||
| if (preserveDeclaration) { | ||
| for (const name of bindings) assignments.push(`globalThis[${JSON.stringify(name)}] = ${name};`); | ||
| continue; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.