Skip to content

preProcessFile incorrectly identifies imports in comments after a template literal typeΒ #47597

@bradzacher

Description

@bradzacher

Bug Report

πŸ”Ž Search Terms

preProcessFile, ts.preProcessFile, template literal types

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried (tried v4.1.5, v4.5.4 and nightly).

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

/*
THIS IS CORRECTLY IGNORED
```
import * as react from 'react';
```
*/
type Foo = `${string}`;
/**
THIS IS NOT IGNORED
```
import {Get} from 'type-fest';
import * as lodash from 'lodash';
```
*/

Or, as a quick node example:

> ts.preProcessFile(`\
... type Foo = \`\${string}\`;
... /**
... \`\`\`
... import {Get} from 'type-fest';
... import * as lodash from 'lodash';
... \`\`\`
... */
... `)
{
  referencedFiles: [],
  typeReferenceDirectives: [],
  libReferenceDirectives: [],
  importedFiles: [
    { fileName: 'type-fest', pos: 50, end: 59 },
    { fileName: 'lodash', pos: 87, end: 93 }
  ],
  isLibFile: false,
  ambientExternalModules: undefined
}

πŸ™ Actual behavior

ts.preProcessFile parses the imports within comments after the template literal type.

πŸ™‚ Expected behavior

The imports within the comment are ignored correctly after the template literal type.


Might be related to #30878?
Original report: microsoft/TypeScript-Website#2243

This causes issues with the TS playground because it will over-fetch types based on the pre-process result. This can cause startup delays, esp with packages like lodash which have a huge numbers of files to fetch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: APIRelates to the public API for TypeScriptFix AvailableA PR has been opened for this issueHelp WantedYou can do this

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions