Skip to content

Reference to the function expression identifier in it's body has different symbol #37131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ark120202 opened this issue Feb 29, 2020 · 1 comment
Labels
API Relates to the public API for TypeScript Bug A bug in TypeScript
Milestone

Comments

@ark120202
Copy link

TypeScript Version: 3.8.2, 3.9.0-dev.20200229

Search Terms:

Code

const ts = require('typescript');

const content = 'const x = function y() { y }';

const host = ts.createCompilerHost({});
const originalReadFile = host.readFile;
host.readFile = fileName => (fileName === 'main.ts' ? content : originalReadFile(fileName));

const program = ts.createProgram({ host, rootNames: ['main.ts'], options: { types: [] } });
program.getSemanticDiagnostics();
const typeChecker = program.getDiagnosticsProducingTypeChecker();
const sourceFile = program.getSourceFile('main.ts');

const fn = sourceFile.statements[0].declarationList.declarations[0].initializer;
const y = fn.name;
const yInBody = fn.body.statements[0].expression.expression;
const ySymbol = typeChecker.getSymbolAtLocation(y);
const yInBodySymbol = typeChecker.getSymbolAtLocation(yInBody);
console.log(ySymbol === yInBodySymbol);

Expected behavior:

y identifier in function name and y reference in it's body to have the same symbol

Actual behavior:

They have different symbols.

Few notes:

Playground Link:

Related Issues:

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript API Relates to the public API for TypeScript labels Mar 4, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 4, 2020
@RyanCavanaugh
Copy link
Member

This is sort of a bug and sort of not -- in this particular case we (probably?) don't need two different symbols. However, symbol identity is not an invariant of the API contract and you should use different means (e.g. checking declarations) to determine if two instances of the same identifier mean the "same" thing depending on your particular definition of sameness. The TS language service API provides functionality for "find all references" that handles most of this for you depending on your requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Relates to the public API for TypeScript Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants