Skip to content

Commit

Permalink
Fixed finding a scope for compiler-level defined symbols like `global…
Browse files Browse the repository at this point in the history
…This` or `undefined`

Fixes #282
  • Loading branch information
timocov committed Dec 30, 2023
1 parent 79b373e commit 6a132dd
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/collisions-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ export class CollisionsResolver {
return null;
}

// we assume that all symbols for a given identifier will be in the same scope (i.e. defined in the same namespaces-chain)
// so we can use any declaration to find that scope as they all will have the same scope
const symbolScopePath = this.getNodeScope(getDeclarationsForSymbol(identifierSymbol)[0]);
const symbolScopePath = this.getSymbolScope(identifierSymbol);

// this scope defines where the current identifier is located
const currentIdentifierScope = this.getNodeScope(referencedIdentifier);
Expand Down Expand Up @@ -184,6 +182,20 @@ export class CollisionsResolver {
return identifierParts.join('.');
}

private getSymbolScope(identifierSymbol: ts.Symbol): ts.Symbol[] {
const identifierDeclarations = getDeclarationsForSymbol(identifierSymbol);

// not all symbols have declarations, e.g. `globalThis` or `undefined` (not type but value e.g. in `typeof undefined`)
// they are "fake" symbols that exist only at the compiler level (see checker.ts file in in the compiler or `globals.set()` calls)
if (identifierDeclarations.length === 0) {
return [];
}

// we assume that all symbols for a given identifier will be in the same scope (i.e. defined in the same namespaces-chain)
// so we can use any declaration to find that scope as they all will have the same scope
return this.getNodeScope(identifierDeclarations[0]);
}

/**
* Returns a node's scope where it is located in terms of namespaces/modules.
* E.g. A scope for `Opt` in `declare module foo { type Opt = number; }` is `[Symbol(foo)]`
Expand Down
5 changes: 5 additions & 0 deletions tests/e2e/test-cases/globalThis/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { TestCaseConfig } from '../test-case-config';

const config: TestCaseConfig = {};

export = config;
1 change: 1 addition & 0 deletions tests/e2e/test-cases/globalThis/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('../run-test-case').runTestCase(__dirname);
2 changes: 2 additions & 0 deletions tests/e2e/test-cases/globalThis/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export declare function getGlobal(): typeof globalThis;
export declare function getUndef(): typeof undefined;
4 changes: 4 additions & 0 deletions tests/e2e/test-cases/globalThis/output.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare function getGlobal(): typeof globalThis;
export declare function getUndef(): typeof undefined;

export {};

0 comments on commit 6a132dd

Please sign in to comment.