microsoft / microsoft/TypeScript

SourceFile text does not include BOM

Aperta
#63,850 5 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@andrewbranch ci sta già lavorando.

Dal 7/7/2026.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

## Steps to reproduce

The `sourceFile.{text,getFullText(),getText()}` properties return a string that does not include the BOM from the original file, however the node positions are relative to the string with the BOM. This means the node positions and their text do not align.

```ts
import * as ts from "@typescript/native-preview/unstable/ast";
import { createVirtualFileSystem } from "@typescript/native-preview/unstable/fs";
import { API } from "@typescript/native-preview/unstable/sync";

const UTF8_BOM = "\uFEFF";
const INPUT = `${UTF8_BOM}let answer:number=42;`;

const fs = createVirtualFileSystem({
"/tsconfig.json": JSON.stringify({ files: ["/input.ts"] }),
"/input.ts": INPUT,
});
const api = new API({
cwd: "/",
fs,
});
const snap = api.updateSnapshot({ openProject: "/tsconfig.json" });
const ast = snap.getProject("/tsconfig.json")!.program.getSourceFile("/input.ts")!;

walk(ast);

function walk(node: ts.Node) {
const start = node.getFullStart();
const end = node.getEnd();
const src = node.getSourceFile().getText();
console.log({ start, end, text: src.slice(start, end), text2: node.getFullText() });
node.forEachChild(walk);
}
```

## Behavior with `typescript@6.0`

```ts
import * as ts from "typescript";

const UTF8_BOM = "\uFEFF";
const INPUT = `${UTF8_BOM}let answer:number=42;`;

const ast = ts.createSourceFile("input.ts", INPUT, ts.ScriptTarget.ESNext, /* setParentNodes */true);

walk(ast);

function walk(node: ts.Node) {
const start = node.getFullStart();
const end = node.getEnd();
const fullText = node.getSourceFile().getFullText();
console.log({ start, end, text: fullText.slice(start, end), text2: node.getFullText() });
node.forEachChild(walk);
}
```

Prints:

```
...
{ start: 4, end: 11, text: ' answer', text2: ' answer' }
{ start: 12, end: 18, text: 'number', text2: 'number' }
{ start: 19, end: 21, text: '42', text2: '42' }
{ start: 22, end: 22, text: '', text2: '' }
```

## Behavior with `tsgo`

```
...
{ start: 4, end: 11, text: 'answer:', text2: 'answer:' }
{ start: 12, end: 18, text: 'umber=', text2: 'umber=' }
{ start: 19, end: 21, text: '2;', text2: '2;' }
{ start: 22, end: 22, text: '', text2: '' }
```

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.