Microsoft/TypeScript

createProgarm with @typescript/vfs fails for target ES2021

Open

#47,273 建立於 2021年12月30日

在 GitHub 查看
 (3 留言) (1 反應) (0 負責人)TypeScript (6,726 fork)batch import
BugDomain: ParserHelp Wanted

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

Bug Report

createProgram fails with internal error when using @typescript/vfs and setting compiler option target to ES2021. This issue only happens for ES2021, all other targets work fine.

🔎 Search Terms

ES2021, createProgram, @typescript/vfs, vfs

🕗 Version & Regression Information

  • This changed between versions 4.4.x and 4.5.x

Full version information:

  • typescript@4.6.0-dev.20211230
  • @typescript/vfs@1.3.5

⏯ Playground Link

Reproduction requires import of typescript and @typescript/vfs. Playground doesn't appear to support this.

💻 Code

import ts, { createProgram, ScriptTarget } from 'typescript';
import { createDefaultMapFromNodeModules, createSystem, createVirtualCompilerHost } from '@typescript/vfs';

const compilerOptions = { target: ScriptTarget.ES2021 };
const code = 'export default "foo"';

const fsMap = createDefaultMapFromNodeModules(compilerOptions, ts);
fsMap.set('index.tsx', code);
const host = createVirtualCompilerHost(createSystem(fsMap), compilerOptions, ts);

const program = createProgram({
  rootNames: [...fsMap.keys()],
  options: compilerOptions,
  host: host.compilerHost,
});

🙁 Actual behavior

Above snippet fails with following error:

/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:10460
        scan: while (pos >= 0 && pos < text.length) {
                                            ^
TypeError: Cannot read property 'length' of undefined
    at iterateCommentRanges (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:10460:45)
    at reduceEachLeadingCommentRange (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:10551:16)
    at Object.getLeadingCommentRanges (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:10566:16)
    at Object.getJSDocCommentRanges (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:15263:16)
    at addJSDocComment (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:30878:42)
    at parseSourceFileWorker (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:30851:34)
    at Object.parseSourceFile (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:30683:26)
    at Object.createSourceFile (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:30481:29)
    at Object.getSourceFile (/local/home/dppilche/typescript-vfs-bug/node_modules/@typescript/vfs/src/index.ts:504:16)
    at findSourceFileWorker (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:115369:29)

🙂 Expected behavior

createProgram does not crash.

貢獻者指南