Skip to content

Commit 4c2b81c

Browse files
authored
Support specific getter forms (#25)
1 parent 638f6d6 commit 4c2b81c

File tree

4 files changed

+361
-61
lines changed

4 files changed

+361
-61
lines changed

README.md

Lines changed: 73 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,35 @@ STRING_LITERAL: A `"` or `'` bounded ECMA-262 string literal.
7070
7171
IDENTIFIER_STRING: ( `"` IDENTIFIER `"` | `'` IDENTIFIER `'` )
7272
73-
COMMENT_SPACE: Any ECMA-262 whitespace, ECMA-262 block comment or ECMA-262 line comment
74-
75-
MODULE_EXPORTS: `module` COMMENT_SPACE `.` COMMENT_SPACE `exports`
73+
MODULE_EXPORTS: `module` `.` `exports`
7674
7775
EXPORTS_IDENTIFIER: MODULE_EXPORTS_IDENTIFIER | `exports`
7876
79-
EXPORTS_DOT_ASSIGN: EXPORTS_IDENTIFIER COMMENT_SPACE `.` COMMENT_SPACE IDENTIFIER COMMENT_SPACE `=`
77+
EXPORTS_DOT_ASSIGN: EXPORTS_IDENTIFIER `.` IDENTIFIER `=`
8078
81-
EXPORTS_LITERAL_COMPUTED_ASSIGN: EXPORTS_IDENTIFIER COMMENT_SPACE `[` COMMENT_SPACE IDENTIFIER_STRING COMMENT_SPACE `]` COMMENT_SPACE `=`
79+
EXPORTS_LITERAL_COMPUTED_ASSIGN: EXPORTS_IDENTIFIER `[` IDENTIFIER_STRING `]` `=`
8280
83-
EXPORTS_LITERAL_PROP: (IDENTIFIER (COMMENT_SPACE `:` COMMENT_SPACE IDENTIFIER)?) | (IDENTIFIER_STRING COMMENT_SPACE `:` COMMENT_SPACE IDENTIFIER)
81+
EXPORTS_LITERAL_PROP: (IDENTIFIER `:` IDENTIFIER)?) | (IDENTIFIER_STRING `:` IDENTIFIER)
8482
85-
EXPORTS_SPREAD: `...` COMMENT_SPACE (IDENTIFIER | REQUIRE)
83+
EXPORTS_SPREAD: `...` (IDENTIFIER | REQUIRE)
8684
8785
EXPORTS_MEMBER: EXPORTS_DOT_ASSIGN | EXPORTS_LITERAL_COMPUTED_ASSIGN
8886
89-
ES_MODULE_DEFINE: `Object` COMMENT_SPACE `.` COMMENT_SPACE `defineProperty COMMENT_SPACE `(` COMMENT_SPACE `__esModule` COMMENT_SPACE `,` COMMENT_SPACE IDENTIFIER_STRING
87+
EXPORTS_DEFINE: `Object` `.` `defineProperty `(` IDENTIFIER_STRING `, {`
88+
(`enumerable: true,`)?
89+
(
90+
`value:` |
91+
`get:` `function`? `()` {` return IDENTIFIER (`.` IDENTIFIER | `[` IDENTIFIER_STRING `]`)? `;`? `}`
92+
)
93+
`})`
9094
91-
EXPORTS_LITERAL: MODULE_EXPORTS COMMENT_SPACE `=` COMMENT_SPACE `{` COMMENT_SPACE (EXPORTS_LITERAL_PROP | EXPORTS_SPREAD) COMMENT_SPACE `,` COMMENT_SPACE)+ `}`
95+
EXPORTS_LITERAL: MODULE_EXPORTS `=` `{` (EXPORTS_LITERAL_PROP | EXPORTS_SPREAD) `,`)+ `}`
9296
93-
REQUIRE: `require` COMMENT_SPACE `(` COMMENT_SPACE STRING_LITERAL COMMENT_SPACE `)`
97+
REQUIRE: `require` `(` STRING_LITERAL `)`
9498
9599
EXPORTS_ASSIGN: (`var` | `const` | `let`) IDENTIFIER `=` REQUIRE
96100
97-
MODULE_EXPORTS_ASSIGN: MODULE_EXPORTS COMMENT_SPACE `=` COMMENT_SPACE REQUIRE
101+
MODULE_EXPORTS_ASSIGN: MODULE_EXPORTS `=` REQUIRE
98102
99103
EXPORT_STAR: (`__export` | `__exportStar`) `(` REQUIRE
100104
@@ -113,9 +117,9 @@ EXPORT_STAR_LIB: `Object.keys(` IDENTIFIER$1 `).forEach(function (` IDENTIFIER$2
113117
`})`
114118
```
115119

116-
* The returned export names are taken to be the combination of:
117-
1. `IDENTIFIER` and `IDENTIFIER_STRING` slots for all `EXPORTS_MEMBER` and `EXPORTS_LITERAL` matches.
118-
2. `__esModule` if there is an `ES_MODULE_DEFINE` match.
120+
Spacing between tokens is taken to be any ECMA-262 whitespace, ECMA-262 block comment or ECMA-262 line comment.
121+
122+
* The returned export names are taken to be the combination of the `IDENTIFIER` and `IDENTIFIER_STRING` slots for all `EXPORTS_MEMBER`, `EXPORTS_LITERAL` and `EXPORTS_DEFINE` matches.
119123
* The reexport specifiers are taken to be the the combination of:
120124
1. The `REQUIRE` matches of the last matched of either `MODULE_EXPORTS_ASSIGN` or `EXPORTS_LITERAL`.
121125
2. All _top-level_ `EXPORT_STAR` `REQUIRE` matches and `EXPORTS_ASSIGN` matches whose `IDENTIFIER` also matches the first `IDENTIFIER` in `EXPORT_STAR_LIB`.
@@ -156,17 +160,66 @@ It will in turn underclassify in cases where the identifiers are renamed:
156160
})(exports);
157161
```
158162

159-
#### __esModule Detection
160-
161-
In addition, `__esModule` is detected as an export when set by `Object.defineProperty`:
163+
`Object.defineProperty` is detected for specifically value and getter forms returning an identifier or member expression:
162164

163165
```js
164-
// DETECTS: __esModule
165-
Object.defineProperty(exports, 'a', { value: 'a' });
166+
// DETECTS: a, b, c, d, __esModule
167+
Object.defineProperty(exports, 'a', {
168+
enumerable: true,
169+
get: function () {
170+
return q.p;
171+
}
172+
});
173+
Object.defineProperty(exports, 'b', {
174+
enumerable: true,
175+
get: function () {
176+
return q['p'];
177+
}
178+
});
179+
Object.defineProperty(exports, 'c', {
180+
enumerable: true,
181+
get () {
182+
return b;
183+
}
184+
});
185+
Object.defineProperty(exports, 'd', { value: 'd' });
166186
Object.defineProperty(exports, '__esModule', { value: true });
167187
```
168188

169-
No other named exports are detected for `defineProperty` calls in order not to trigger getters or non-enumerable properties unnecessarily.
189+
Alternative object definition structures or getter function bodies are not detected:
190+
191+
```js
192+
// DETECTS: NO EXPORTS
193+
Object.defineProperty(exports, 'a', {
194+
enumerable: false,
195+
get () {
196+
return p;
197+
}
198+
});
199+
Object.defineProperty(exports, 'b', {
200+
configurable: true,
201+
get () {
202+
return p;
203+
}
204+
});
205+
Object.defineProperty(exports, 'c', {
206+
get: () => p
207+
});
208+
Object.defineProperty(exports, 'd', {
209+
enumerable: true,
210+
get: function () {
211+
return dynamic();
212+
}
213+
});
214+
Object.defineProperty(exports, 'e', {
215+
enumerable: true,
216+
get () {
217+
return 'str';
218+
}
219+
});
220+
```
221+
222+
`Object.defineProperties` is also not supported.
170223

171224
#### Exports Object Assignment
172225

lexer.js

Lines changed: 103 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -260,30 +260,114 @@ function tryParseObjectDefineOrKeys (keys) {
260260
pos++;
261261
ch = commentWhitespace();
262262
if (ch === 100/*d*/ && source.startsWith('efineProperty', pos + 1)) {
263-
pos += 14;
264-
revertPos = pos - 1;
265-
ch = commentWhitespace();
266-
if (ch !== 40/*(*/) {
267-
pos = revertPos;
268-
return;
269-
}
270-
pos++;
271-
ch = commentWhitespace();
272-
if (readExportsOrModuleDotExports(ch)) {
263+
while (true) {
264+
pos += 14;
265+
revertPos = pos - 1;
266+
ch = commentWhitespace();
267+
if (ch !== 40/*(*/) break;
268+
pos++;
269+
ch = commentWhitespace();
270+
if (!readExportsOrModuleDotExports(ch)) break;
271+
ch = commentWhitespace();
272+
if (ch !== 44/*,*/) break;
273+
pos++;
274+
ch = commentWhitespace();
275+
if (ch !== 39/*'*/ && ch !== 34/*"*/) break;
276+
let quot = ch;
277+
const exportPos = ++pos;
278+
if (!identifier() || source.charCodeAt(pos) !== quot) break;
279+
const expt = source.slice(exportPos, pos);
280+
pos++;
281+
ch = commentWhitespace();
282+
if (ch !== 44/*,*/) break;
283+
pos++;
284+
ch = commentWhitespace();
285+
if (ch !== 123/*{*/) break;
286+
pos++;
273287
ch = commentWhitespace();
274-
if (ch === 44/*,*/) {
288+
if (ch === 101/*e*/) {
289+
if (!source.startsWith('numerable', pos + 1)) break;
290+
pos += 10;
291+
ch = commentWhitespace();
292+
if (ch !== 58/*:*/) break;
275293
pos++;
276294
ch = commentWhitespace();
277-
if (ch === 39/*'*/ || ch === 34/*"*/) {
278-
const exportPos = ++pos;
279-
if (identifier() && source.charCodeAt(pos) === ch) {
280-
// revert for "("
281-
const expt = source.slice(exportPos, pos);
282-
if (expt === '__esModule')
283-
addExport(expt);
284-
}
295+
if (ch !== 116/*t*/ || !source.startsWith('rue', pos + 1)) break;
296+
pos += 4;
297+
ch = commentWhitespace();
298+
if (ch !== 44) break;
299+
pos++;
300+
ch = commentWhitespace();
301+
}
302+
if (ch === 118/*v*/) {
303+
if (!source.startsWith('alue', pos + 1)) break;
304+
pos += 5;
305+
ch = commentWhitespace();
306+
if (ch !== 58/*:*/) break;
307+
pos++;
308+
addExport(expt);
309+
break;
310+
}
311+
else if (ch === 103/*g*/) {
312+
if (!source.startsWith('et', pos + 1)) break;
313+
pos += 3;
314+
ch = commentWhitespace();
315+
if (ch === 58/*:*/) {
316+
pos++;
317+
ch = commentWhitespace();
318+
if (ch !== 102/*f*/) break;
319+
if (!source.startsWith('unction', pos + 1)) break;
320+
pos += 8;
321+
ch = commentWhitespace();
322+
}
323+
if (ch !== 40/*(*/) break;
324+
pos++;
325+
ch = commentWhitespace();
326+
if (ch !== 41/*)*/) break;
327+
pos++;
328+
ch = commentWhitespace();
329+
if (ch !== 123/*{*/) break;
330+
pos++;
331+
ch = commentWhitespace();
332+
if (ch !== 114/*r*/) break;
333+
if (!source.startsWith('eturn', pos + 1)) break;
334+
pos += 6;
335+
ch = commentWhitespace();
336+
if (!identifier()) break;
337+
ch = commentWhitespace();
338+
if (ch === 46/*.*/) {
339+
pos++;
340+
commentWhitespace();
341+
if (!identifier()) break;
342+
ch = commentWhitespace();
343+
}
344+
else if (ch === 91/*[*/) {
345+
pos++;
346+
ch = commentWhitespace();
347+
if (ch === 39/*'*/) singleQuoteString();
348+
else if (ch === 34/*"*/) doubleQuoteString();
349+
else break;
350+
pos++;
351+
ch = commentWhitespace();
352+
if (ch !== 93/*]*/) break;
353+
pos++;
354+
ch = commentWhitespace();
355+
}
356+
if (ch === 59/*;*/) {
357+
pos++;
358+
ch = commentWhitespace();
285359
}
360+
if (ch !== 125/*}*/) break;
361+
pos++;
362+
ch = commentWhitespace();
363+
if (ch !== 125/*}*/) break;
364+
pos++;
365+
ch = commentWhitespace();
366+
if (ch !== 41/*)*/) break;
367+
addExport(expt);
368+
return;
286369
}
370+
break;
287371
}
288372
}
289373
else if (keys && ch === 107/*k*/ && source.startsWith('eys', pos + 1)) {

0 commit comments

Comments
 (0)