Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: accept duplicated import/variable in different module #13527

Merged
merged 3 commits into from Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,17 @@
// @flow

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: string): 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,15 @@
// @flow
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: string): 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,13 @@
declare module 'test' {
import type { JSONSchema7 } from 'json-schema';
import { bar } from "baz";
export { fooBar } from "baz";
let foo: JSONSchema7;
}

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,12 @@
declare module 'test' {
import type { JSONSchema7 } from 'json-schema';
import { bar } from "baz";
export { fooBar } from "baz";
let foo: JSONSchema7;
}
declare module 'test/submodule' {
import type { JSONSchema7 } from 'json-schema';
import { bar } from "baz";
export { fooBar } from "baz";
let foo: JSONSchema7;
}
14 changes: 12 additions & 2 deletions packages/babel-types/src/validators/isBlockScoped.ts
@@ -1,10 +1,20 @@
import { isClassDeclaration, isFunctionDeclaration } from "./generated";
import {
isClassDeclaration,
isFunctionDeclaration,
isImportDeclaration,
} from "./generated";
import isLet from "./isLet";
import type * as t from "..";

/**
* Check if the input `node` is block scoped.
*/
export default function isBlockScoped(node: t.Node): boolean {
return isFunctionDeclaration(node) || isClassDeclaration(node) || isLet(node);
return (
isFunctionDeclaration(node) ||
isClassDeclaration(node) ||
isLet(node) ||
// import declaration can be block scoped when it's in TS/flow module declaration
isImportDeclaration(node)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it is not block scoped as ImportDeclarations may only appear in the top level or inside a module/namespace (for TS). Adding it here may be a breaking change for someone using the validator.
Furthermore excluding ImportDeclarations from

Declaration(path) {
// delegate block scope handling to the `BlockScoped` method
if (path.isBlockScoped()) return;
// this will be hit again once we traverse into it after this iteration
if (path.isExportDeclaration()) return;
// we've ran into a declaration!
const parent =
path.scope.getFunctionParent() || path.scope.getProgramParent();
parent.registerDeclaration(path);
},

results in excluding those nodes from the traversal and not running registerDeclaration on them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think excluding ImportDeclarations is well enough, since it will also cause breaking changes.

The main problem here is that the scope of declared module should be isolated with other modules' scopes and the program's scope. But unfortunately it's not now.

So maybe we could change the implementation of getProgramParent to make it returns not only on Program, but also ModuleDeclaration. This will make scopes of modules isolated with each other and the program.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, excluding ImportDeclaration is definitely not the right way here.
You will be happy to know that there is already a method called getBlockParent that gets the upper block. However I would suggest you to give a look at how the BlockScoped visitor is handling block scoped declarations, because we may have to replicate a similar behavior for ImportDeclaration.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it returns not only on Program, but also ModuleDeclaration.

I don't think we can mark ModuleDeclaration as program because variables within module declaration can reference bindings defined in program:

type T = number;
declare module 'test' {
  declare var a: T;
}

Since ambient declarations are always top level. We can check if node.parent is ModuleDeclaration and then register declarations there, otherwise we fallback to

const parent =
path.scope.getFunctionParent() || path.scope.getProgramParent();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a different approach.

Take all ImportDeclaration into a separated visitor, and use getBlockParent to find parent scope.

I think it is more conducive to maintenance。

);
}