Skip to content

Commit

Permalink
fix: accept duplicated import/variable in different module (#13527)
Browse files Browse the repository at this point in the history
Co-authored-by: Hu谩ng J霉nli脿ng <jlhwung@gmail.com>
  • Loading branch information
colinaaa and JLHwung committed Jul 6, 2021
1 parent fce35af commit a5a63e3
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/babel-traverse/src/scope/index.ts
Expand Up @@ -202,6 +202,9 @@ const collectorVisitor: Visitor<CollectVisitorState> = {
// delegate block scope handling to the `BlockScoped` method
if (path.isBlockScoped()) return;

// delegate import handing to the `ImportDeclaration` method
if (path.isImportDeclaration()) return;

// this will be hit again once we traverse into it after this iteration
if (path.isExportDeclaration()) return;

Expand All @@ -211,6 +214,13 @@ const collectorVisitor: Visitor<CollectVisitorState> = {
parent.registerDeclaration(path);
},

ImportDeclaration(path) {
// import may only appear in the top level or inside a module/namespace (for TS/flow)
const parent = path.scope.getBlockParent();

parent.registerDeclaration(path);
},

ReferencedIdentifier(path, state) {
state.references.push(path);
},
Expand Down
@@ -0,0 +1,18 @@
// @flow

type T = string;
declare module 'test' {
import type { JSONSchema7 } from 'json-schema';
declare var a: number;
declare function foo(a: JSONSchema7): string;
declare export function concatPath(dirA: string, dirB: T): string;
declare export class A {}
}

declare module 'test/submodule' {
import type { JSONSchema7 } from 'json-schema';
declare var a: number;
declare function foo(a: JSONSchema7): string;
declare export function concatPath(dirA: string, dirB: string): string;
declare export class A {}
}
@@ -0,0 +1,3 @@
{
"plugins": ["syntax-flow"]
}
@@ -0,0 +1,16 @@
// @flow
type T = string;
declare module 'test' {
import type { JSONSchema7 } from 'json-schema';
declare var a: number;
declare function foo(a: JSONSchema7): string;
declare export function concatPath(dirA: string, dirB: T): string;
declare export class A {}
}
declare module 'test/submodule' {
import type { JSONSchema7 } from 'json-schema';
declare var a: number;
declare function foo(a: JSONSchema7): string;
declare export function concatPath(dirA: string, dirB: string): string;
declare export class A {}
}
@@ -0,0 +1,16 @@
type T = number;
declare module 'test' {
import type { JSONSchema7 } from 'json-schema';
import { bar } from "baz";
export { fooBar } from "baz";
let foo: JSONSchema7;
// can reference type outsider module
let baz: T;
}

declare module 'test/submodule' {
import type { JSONSchema7 } from 'json-schema';
import { bar } from "baz";
export { fooBar } from "baz";
let foo: JSONSchema7;
}
@@ -0,0 +1,4 @@
{
"plugins": ["syntax-typescript"],
"sourceType": "module"
}
@@ -0,0 +1,15 @@
type T = number;
declare module 'test' {
import type { JSONSchema7 } from 'json-schema';
import { bar } from "baz";
export { fooBar } from "baz";
let foo: JSONSchema7; // can reference type outsider module

let baz: T;
}
declare module 'test/submodule' {
import type { JSONSchema7 } from 'json-schema';
import { bar } from "baz";
export { fooBar } from "baz";
let foo: JSONSchema7;
}

0 comments on commit a5a63e3

Please sign in to comment.