Skip to content

extremely odd interaction of scope, filename and preset-typescript #7592

@jeysal

Description

@jeysal

Bug Report

Input Code

Repro available: https://github.com/jeysal/babel-scope-filename-typescript-repro

const { transform } = require("@babel/core");

const plugin = ({ types: t }) => ({
  visitor: {
    CallExpression(path) {
      if (path.node.callee.name === "a") {
        path.scope
          .getProgramParent()
          .path.unshiftContainer(
            "body",
            t.importDeclaration(
              [t.importDefaultSpecifier(t.identifier("local"))],
              t.stringLiteral("source")
            )
          );

        path.scope.crawl();
        path.node.callee = t.identifier("local");
      }
    }
  }
});

console.log(
  transform(`a();`, {
    presets: ["@babel/preset-typescript"],
    plugins: [plugin]
  }).code
);

Babel/Babylon Configuration (.babelrc, package.json, cli command)

{
  "devDependencies": {
    "@babel/core": "^7.0.0-beta.46",
    "@babel/preset-typescript": "^7.0.0-beta.46"
  }
}

Expected Behavior

Generated code has import local from "source";

Current Behavior

local();

Context

Generated code misses the import.
Ways to fix this (ONE of the following is sufficient):

  • disable @babel/preset-typescript
  • crawl one line later (after adding a reference to the import)

Your Environment

software version(s)
Babel 7.0.0-beta.46
Babylon
node 8.11.1
yarn 1.6.0
Operating System Linux version 4.16.4-1-ARCH

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions