Skip to content

preProcessFile doesn't find other imports after template strings #30878

Closed
@diagramatics

Description

@diagramatics

TypeScript Version: 3.5.0-dev.20190411

Search Terms:
preProcessFile import

Code

const ts = require('typescript');
const output = ts.preProcessFile("`${foo}`; import './test';", true, true);
console.log(output.importedFiles);

Expected behavior:

./test should be listed in importedFiles as an entry to the array.

Actual behavior:

It doesn't. The array is empty.

Reversing the order works though.

const ts = require('typescript');
const output = ts.preProcessFile("import './test'; `${foo}`;", true, true);
console.log(output.importedFiles);

Note this also happens on dynamic imports too.

const ts = require("typescript");
const output = ts.preProcessFile("`${foo}`; import('./test');", true, true);
console.log(output.importedFiles);

Note that these repros are short for reproducability reasons. Yes, not doing static importing at the top is silly, but I found that from my use case of something similar to this:

const foo = `${someVariable}.${someOtherVariable}`;
import('./foo').then(m => m.callFunction());

You can drill down to having the issue also reproducible with static imports and dynamic imports with promise chaining or async, so anything import statement related?

Playground Link:

Replicated in CodeSandbox: https://codesandbox.io/embed/5zqln3nn5n.

Related Issues: None (?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    APIRelates to the public API for TypeScriptBugA bug in TypeScriptFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions