Skip to content

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

Open
@ark120202

Description

@ark120202

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    APIRelates to the public API for TypeScriptBugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions