Closed
Description
Bug Report
Starting with TypeScript 4.3, the ts.Node.getChildren()
API may return two copies of the same AST node.
🔎 Search Terms
getChildren
🕗 Version & Regression Information
Repros with TypeScript 4.3.2
and 4.4.0-dev.20210602
Does NOT repro with TypeScript 4.2.4
💻 Code
Here is a complete project that reproduces the problem: repro.zip
The code looks like this:
repro.js
const path = require("path");
const ts = require("typescript");
console.log('Start')
try {
const inputPath = path.join(__dirname, "input.ts");
const program = ts.createProgram([inputPath], {});
program.getSemanticDiagnostics();
const sourceFile = program.getSourceFile(inputPath);
const docNode = sourceFile.getChildren()[0].getChildren()[0].getChildren()[4].getChildren()[0].getChildren()[0].getChildren()[0];
for (const child of docNode.getChildren()) {
console.log(`[pos=${child.pos}, end=${child.end}] ` + child.getText())
}
debugger;
} catch (error) {
console.error(error.stack);
debugger;
}
It uses the compiler API to parse an input like this:
input.ts
export class TypeReferencesInAedoc {
/**
* Returns a value
* @param arg1 - The input parameter of type {@link TypeReferencesInAedoc}.
* @returns An object of type {@link TypeReferencesInAedoc}.
*/
public getValue(arg1: TypeReferencesInAedoc): TypeReferencesInAedoc {
return this;
}
}
🙁 Actual behavior
The printed output looks like this:
[pos=73, end=78] param
[pos=79, end=83] arg1
[pos=84, end=114] - The input parameter of type
[pos=114, end=143] {@link TypeReferencesInAedoc}
[pos=143, end=151] .
*
[pos=84, end=114] - The input parameter of type
[pos=114, end=143] {@link TypeReferencesInAedoc}
[pos=143, end=151] .
*
The duplicated pos/end nodes are actually the same object instances, which can be verified in the debugger:
docNode.getChildren[3] === docNode.getChildren[6]
true
🙂 Expected behavior
ts.Node.getChildren()
should not return duplicate nodes.