diff --git a/docs/api/scope-utils.md b/docs/api/scope-utils.md index 3b46364..88be37d 100644 --- a/docs/api/scope-utils.md +++ b/docs/api/scope-utils.md @@ -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 diff --git a/src/reference-tracker.js b/src/reference-tracker.js index ec5a193..548388b 100644 --- a/src/reference-tracker.js +++ b/src/reference-tracker.js @@ -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 = [] diff --git a/test/reference-tracker.js b/test/reference-tracker.js index eb7166e..fe95ee0 100644 --- a/test/reference-tracker.js +++ b/test/reference-tracker.js @@ -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'.",