Skip to content

Commit d2c2c18

Browse files
committed
💥 ReferenceTracker.iterateGlobalReferences() recognizes globalThis by default
1 parent c37508a commit d2c2c18

File tree

3 files changed

+114
-3
lines changed

3 files changed

+114
-3
lines changed

‎docs/api/scope-utils.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ This provides reference tracking for global variables, CommonJS modules, and ES
8888
:-----|:-----|:------------
8989
globalScope | Scope | The global scope.
9090
options.mode | `"strict"` or `"legacy"` | The mode which determines how the `tracker.iterateEsmReferences()` method scans CommonJS modules. If this is `"strict"`, the method binds CommonJS modules to the default export. Otherwise, the method binds CommonJS modules to both the default export and named exports. Optional. Default is `"strict"`.
91-
options.globalObjectNames | string[] | The name list of Global Object. Optional. Default is `["global", "self", "window"]`.
91+
options.globalObjectNames | string[] | The name list of Global Object. Optional. Default is `["global", "globalThis", "self", "window"]`.
9292

9393
## tracker.iterateGlobalReferences
9494

‎src/reference-tracker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ export class ReferenceTracker {
5656
* @param {Scope} globalScope The global scope.
5757
* @param {object} [options] The options.
5858
* @param {"legacy"|"strict"} [options.mode="strict"] The mode to determine the ImportDeclaration's behavior for CJS modules.
59-
* @param {string[]} [options.globalObjectNames=["global","self","window"]] The variable names for Global Object.
59+
* @param {string[]} [options.globalObjectNames=["global","globalThis","self","window"]] The variable names for Global Object.
6060
*/
6161
constructor(
6262
globalScope,
6363
{
6464
mode = "strict",
65-
globalObjectNames = ["global", "self", "window"],
65+
globalObjectNames = ["global", "globalThis", "self", "window"],
6666
} = {}
6767
) {
6868
this.variableStack = []

‎test/reference-tracker.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,117 @@ describe("The 'ReferenceTracker' class:", () => {
315315
},
316316
],
317317
},
318+
{
319+
description:
320+
"should iterate the member references of a given global variable, with 'global'.",
321+
code: [
322+
"/*global global */",
323+
"global.Object.a;",
324+
"global.Object.b; global.Object.b(); new global.Object.b();",
325+
"global.Object.c; global.Object.c(); new global.Object.c();",
326+
].join("\n"),
327+
traceMap: {
328+
Object: {
329+
a: { [READ]: 1 },
330+
b: { [CALL]: 2 },
331+
c: { [CONSTRUCT]: 3 },
332+
},
333+
},
334+
expected: [
335+
{
336+
node: { type: "MemberExpression" },
337+
path: ["Object", "a"],
338+
type: READ,
339+
info: 1,
340+
},
341+
{
342+
node: { type: "CallExpression" },
343+
path: ["Object", "b"],
344+
type: CALL,
345+
info: 2,
346+
},
347+
{
348+
node: { type: "NewExpression" },
349+
path: ["Object", "c"],
350+
type: CONSTRUCT,
351+
info: 3,
352+
},
353+
],
354+
},
355+
{
356+
description:
357+
"should iterate the member references of a given global variable, with 'globalThis'.",
358+
code: [
359+
"/*global globalThis */",
360+
"globalThis.Object.a;",
361+
"globalThis.Object.b; globalThis.Object.b(); new globalThis.Object.b();",
362+
"globalThis.Object.c; globalThis.Object.c(); new globalThis.Object.c();",
363+
].join("\n"),
364+
traceMap: {
365+
Object: {
366+
a: { [READ]: 1 },
367+
b: { [CALL]: 2 },
368+
c: { [CONSTRUCT]: 3 },
369+
},
370+
},
371+
expected: [
372+
{
373+
node: { type: "MemberExpression" },
374+
path: ["Object", "a"],
375+
type: READ,
376+
info: 1,
377+
},
378+
{
379+
node: { type: "CallExpression" },
380+
path: ["Object", "b"],
381+
type: CALL,
382+
info: 2,
383+
},
384+
{
385+
node: { type: "NewExpression" },
386+
path: ["Object", "c"],
387+
type: CONSTRUCT,
388+
info: 3,
389+
},
390+
],
391+
},
392+
{
393+
description:
394+
"should iterate the member references of a given global variable, with 'self'.",
395+
code: [
396+
"/*global self */",
397+
"self.Object.a;",
398+
"self.Object.b; self.Object.b(); new self.Object.b();",
399+
"self.Object.c; self.Object.c(); new self.Object.c();",
400+
].join("\n"),
401+
traceMap: {
402+
Object: {
403+
a: { [READ]: 1 },
404+
b: { [CALL]: 2 },
405+
c: { [CONSTRUCT]: 3 },
406+
},
407+
},
408+
expected: [
409+
{
410+
node: { type: "MemberExpression" },
411+
path: ["Object", "a"],
412+
type: READ,
413+
info: 1,
414+
},
415+
{
416+
node: { type: "CallExpression" },
417+
path: ["Object", "b"],
418+
type: CALL,
419+
info: 2,
420+
},
421+
{
422+
node: { type: "NewExpression" },
423+
path: ["Object", "c"],
424+
type: CONSTRUCT,
425+
info: 3,
426+
},
427+
],
428+
},
318429
{
319430
description:
320431
"should iterate the member references of a given global variable, with 'window'.",

0 commit comments

Comments
 (0)