Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ parse(source, filename, { mode: 'extended' });

Omitting the options, using `{}`, or selecting `mode: 'default'` preserves default detection.
An invalid mode throws a `TypeError`.
`allowMinifiedEnumerable` can be used with either mode.
Both modes use the existing lexer and assume valid JavaScript input.
`allowMinifiedEnumerable` can be used with any mode.
All modes use the existing lexer and assume valid JavaScript input.
They do not execute the source or validate the complete JavaScript grammar.

Extended mode detects literal names in `Object.defineProperty` calls regardless of the descriptor expression or getter body.
Expand All @@ -120,6 +120,31 @@ Division and regular-expression detection remains heuristic.
Ambiguous cases can cause extended mode's bracket tracking to report parse errors on valid JavaScript.
Selecting extended mode in this package does not change Node.js's built-in CommonJS detection.

### Conservative detection

Select `mode: 'conservative'` to detect more object literal exports while retaining the default getter and reexport policies:

```js
parse('module.exports = { value: call(), method() {} };', filename, { mode: 'conservative' });
// { exports: ['value', 'method'], reexports: [] }
```

Unparenthesized object literals assigned to `module.exports` support arbitrary values, methods, shorthand properties, and computed string literal keys.
Accessor names can be identifiers without escapes, quoted strings, or computed string literals.
These accessors veto their names, including names detected elsewhere in the source.
Numeric, dynamic computed, and escaped identifier keys can leave previously detected names intact.

`Object.defineProperty` retains the default descriptor patterns and getter allowlist.
`Object.defineProperties` and parenthesized assignments retain default detection.
This excludes uncertain comma-expression names instead of selecting an object that might not become the exported value.
Each direct `module.exports` assignment replaces earlier reexports, as in default mode.

The default getter allowlist includes forwarding getters.
Reading those exports can still execute JavaScript in a consumer such as Node.js.
This mode is conservative detection, not a guarantee of side-effect-free namespace creation.
It retains the lexer's limits for shadowed bindings, control flow, and ambiguous bracket tracking.
Selecting this mode does not change Node.js's built-in detection.

### Grammar (default mode)

CommonJS exports matches are run against the source token stream. The grammar below describes default detection.
Expand Down
2 changes: 1 addition & 1 deletion build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ lib/lexer.wasm: include-wasm/cjs-module-lexer.h src/lexer.c
$(WASM_LDFLAGS) $(WASM_EXTRA_LDFLAGS)

optimize: lib/lexer.wasm
${WASM_OPT} -Oz lib/lexer.wasm -o lib/lexer.wasm
${WASM_OPT} -Oz --converge lib/lexer.wasm -o lib/lexer.wasm

clean:
rm lib/*
2 changes: 1 addition & 1 deletion lexer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface Exports {

export interface ParseOptions {
allowMinifiedEnumerable?: boolean;
mode?: 'default' | 'extended';
mode?: 'default' | 'extended' | 'conservative';
}

export declare function parse(source: string, name?: string, options?: ParseOptions): Exports;
Expand Down
21 changes: 13 additions & 8 deletions lexer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let source, pos, end;
let extended, exportObjectPos;
let extended, conservative, exportObjectPos;
const STACK_DEPTH = 2048;
const ClassToken = 1;
const ExportObjectToken = 2;
Expand Down Expand Up @@ -63,12 +63,14 @@ function pushTemplate () {
*/
function parseCJS (source, name = '@', options) {
const mode = options ? options.mode : undefined;
if (mode !== undefined && mode !== 'default' && mode !== 'extended')
throw new TypeError('Expected options.mode to be "default" or "extended".');
const modeFlags = mode === 'extended' ? 2 : mode === 'conservative' ? 4 : 0;
if (!modeFlags && mode !== undefined && mode !== 'default')
throw new TypeError('Invalid mode');
const minifiedEnumerable = options ? options.allowMinifiedEnumerable === true : false;
resetState();
allowMinifiedEnumerable = minifiedEnumerable;
extended = mode === 'extended';
conservative = modeFlags === 4;
extended = modeFlags !== 0;
exportObjectPos = -1;
try {
parseSource(source);
Expand Down Expand Up @@ -413,7 +415,7 @@ function tryParseObjectDefineOrKeys (keys) {
if (ch === 46/*.*/) {
pos++;
ch = commentWhitespace();
if (extended && ch === 100/*d*/) {
if (extended && !conservative && ch === 100/*d*/) {
readExportDefinition();
pos = revertPos;
return;
Expand Down Expand Up @@ -921,6 +923,7 @@ function tryParseObjectDefineOrKeys (keys) {
pos = revertPos;
}

/** @param {number} ch */
function readExportsOrModuleDotExports (ch) {
const revertPos = pos;
if (ch === 109/*m*/ && source.startsWith('odule', pos + 1)) {
Expand Down Expand Up @@ -999,12 +1002,12 @@ function tryParseExportsDotAssign (assign) {
// module.exports =
case 61/*=*/: {
if (assign) {
if (!extended && reexports.size)
if ((!extended || conservative) && reexports.size)
reexports = new Set();
pos++;
ch = commentWhitespace();
if (extended) {
if (ch === 40/*(*/) ch = readExportExpressionStart();
if (!conservative && ch === 40/*(*/) ch = readExportExpressionStart();
if (ch === 123/*{*/) exportObjectPos = pos;
else if (ch === 114/*r*/) tryParseRequire(ExportAssign);
pos = revertPos;
Expand Down Expand Up @@ -1131,6 +1134,7 @@ function readExportProperty () {
pos++;
ch = commentWhitespace();
}
let accessor = false;
let computed = ch === 91/*[*/;
if (computed) {
pos++;
Expand All @@ -1149,6 +1153,7 @@ function readExportProperty () {
source.startsWith('set', nameStart) && nameEnd - nameStart === 3 ||
source.startsWith('async', nameStart) && nameEnd - nameStart === 5) &&
ch !== 58/*:*/ && ch !== 40/*(*/ && ch !== 44/*,*/ && ch !== 125/*}*/) {
accessor = nameEnd - nameStart === 3;
if (ch === 42/***/) {
pos++;
ch = commentWhitespace();
Expand Down Expand Up @@ -1177,7 +1182,7 @@ function readExportProperty () {
}
if (nameEnd !== undefined && (ch === 58/*:*/ || ch === 40/*(*/ ||
!computed && (ch === 44/*,*/ || ch === 125/*}*/)))
_exports.add(decode(source.slice(nameStart, nameEnd)));
(conservative && accessor ? unsafeGetters : _exports).add(decode(source.slice(nameStart, nameEnd)));
pos = revertPos;
}

Expand Down
Binary file modified lib/lexer.wasm
Binary file not shown.
Loading
Loading