Skip to content

updateImportDeclaration #31446

@anion155

Description

@anion155

TypeScript Version: 3.5.0-dev.20190517

Search Terms: updateImportDeclaration, createImportDeclaration

Code

// transformer.ts
function visit(node: ts.Node): ts.Node {
  if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
    const file = findFileInPaths(node.moduleSpecifier.text);  // transform specifier to filename according to baseUrl and paths compiller options.
    if (file) {
      return ts.updateImportDeclaration(
        node,
        node.decorators,
        node.modifiers,
        node.importClause,
        ts.createLiteral(file)
      );
    }
  }
}
//  tsconfig.json
{
  "compillerOptions": {
    "baseUrl": "./",
    "paths": { "@/*": [ "src/*" ] }
  }
}
// a.ts
import { B } from '@/b';
const b: B = 'blah';

// b.ts
export type B = 'blah' | 'vah';

Expected behavior: All imports to be updated accordingly to baseUrl and handled after. Especially when importClause elements used only as types, and not as values, import will be deleted from resulting js file.

// a.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var b = 'blah';

Actual behavior: All updated import declaration are go to resulting js file.

// a.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var b_1 = require("./b");
var b = 'blah';

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions