Skip to content

Commit

Permalink
💥 ReferenceTracker.iterateGlobalReferences() recognizes globalThis by…
Browse files Browse the repository at this point in the history
… default
  • Loading branch information
mysticatea committed Dec 26, 2019
1 parent c37508a commit d2c2c18
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/api/scope-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ This provides reference tracking for global variables, CommonJS modules, and ES
:-----|:-----|:------------
globalScope | Scope | The global scope.
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"`.
options.globalObjectNames | string[] | The name list of Global Object. Optional. Default is `["global", "self", "window"]`.
options.globalObjectNames | string[] | The name list of Global Object. Optional. Default is `["global", "globalThis", "self", "window"]`.

## tracker.iterateGlobalReferences

Expand Down
4 changes: 2 additions & 2 deletions src/reference-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ export class ReferenceTracker {
* @param {Scope} globalScope The global scope.
* @param {object} [options] The options.
* @param {"legacy"|"strict"} [options.mode="strict"] The mode to determine the ImportDeclaration's behavior for CJS modules.
* @param {string[]} [options.globalObjectNames=["global","self","window"]] The variable names for Global Object.
* @param {string[]} [options.globalObjectNames=["global","globalThis","self","window"]] The variable names for Global Object.
*/
constructor(
globalScope,
{
mode = "strict",
globalObjectNames = ["global", "self", "window"],
globalObjectNames = ["global", "globalThis", "self", "window"],
} = {}
) {
this.variableStack = []
Expand Down
111 changes: 111 additions & 0 deletions test/reference-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,117 @@ describe("The 'ReferenceTracker' class:", () => {
},
],
},
{
description:
"should iterate the member references of a given global variable, with 'global'.",
code: [
"/*global global */",
"global.Object.a;",
"global.Object.b; global.Object.b(); new global.Object.b();",
"global.Object.c; global.Object.c(); new global.Object.c();",
].join("\n"),
traceMap: {
Object: {
a: { [READ]: 1 },
b: { [CALL]: 2 },
c: { [CONSTRUCT]: 3 },
},
},
expected: [
{
node: { type: "MemberExpression" },
path: ["Object", "a"],
type: READ,
info: 1,
},
{
node: { type: "CallExpression" },
path: ["Object", "b"],
type: CALL,
info: 2,
},
{
node: { type: "NewExpression" },
path: ["Object", "c"],
type: CONSTRUCT,
info: 3,
},
],
},
{
description:
"should iterate the member references of a given global variable, with 'globalThis'.",
code: [
"/*global globalThis */",
"globalThis.Object.a;",
"globalThis.Object.b; globalThis.Object.b(); new globalThis.Object.b();",
"globalThis.Object.c; globalThis.Object.c(); new globalThis.Object.c();",
].join("\n"),
traceMap: {
Object: {
a: { [READ]: 1 },
b: { [CALL]: 2 },
c: { [CONSTRUCT]: 3 },
},
},
expected: [
{
node: { type: "MemberExpression" },
path: ["Object", "a"],
type: READ,
info: 1,
},
{
node: { type: "CallExpression" },
path: ["Object", "b"],
type: CALL,
info: 2,
},
{
node: { type: "NewExpression" },
path: ["Object", "c"],
type: CONSTRUCT,
info: 3,
},
],
},
{
description:
"should iterate the member references of a given global variable, with 'self'.",
code: [
"/*global self */",
"self.Object.a;",
"self.Object.b; self.Object.b(); new self.Object.b();",
"self.Object.c; self.Object.c(); new self.Object.c();",
].join("\n"),
traceMap: {
Object: {
a: { [READ]: 1 },
b: { [CALL]: 2 },
c: { [CONSTRUCT]: 3 },
},
},
expected: [
{
node: { type: "MemberExpression" },
path: ["Object", "a"],
type: READ,
info: 1,
},
{
node: { type: "CallExpression" },
path: ["Object", "b"],
type: CALL,
info: 2,
},
{
node: { type: "NewExpression" },
path: ["Object", "c"],
type: CONSTRUCT,
info: 3,
},
],
},
{
description:
"should iterate the member references of a given global variable, with 'window'.",
Expand Down

0 comments on commit d2c2c18

Please sign in to comment.