Skip to content

Commit

Permalink
fix handling of imports
Browse files Browse the repository at this point in the history
Summary:
All imports are treated as "type" definitions regardless of whether or not they have a "type" kind. You can import classes and enums as values and use them in type locations. Additionally namespace imports can be used in a type location (eg `React.MixedElement`).

Because we don't have cross-file information, we cannot statically determine if an imported thing is a "type referencible" thing, so we have to just treat all imports as possible types.

This diff marks all import bindings as type definitions.

Reviewed By: evanyeung

Differential Revision: D33574248

fbshipit-source-id: b6dc10dc2afb8ad01e0dbfbe40d0dab25b0ec297
  • Loading branch information
bradzacher authored and facebook-github-bot committed Jan 14, 2022
1 parent 678b569 commit 8bebc9b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,7 @@ describe('Imports', () => {
{
name: 'RefType',
type: DefinitionType.ImportBinding,
referenceCount: 0,
referenceCount: 1,
},
{
name: 'foo',
Expand Down Expand Up @@ -1717,7 +1717,7 @@ describe('Imports', () => {
{
name: 'RefType',
type: DefinitionType.ImportBinding,
referenceCount: 0,
referenceCount: 1,
},
{
name: 'foo',
Expand Down Expand Up @@ -1835,7 +1835,7 @@ describe('Imports', () => {
{
name: 'RefType',
type: DefinitionType.ImportBinding,
referenceCount: 0,
referenceCount: 1,
},
{
name: 'foo',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,31 @@ class ImportBindingDefinition extends DefinitionBase<
decl.importKind === 'type' ||
decl.importKind === 'typeof'
) {
this.isTypeDefinition = true;
this.isVariableDefinition = false;
} else {
this.isTypeDefinition = false;
this.isVariableDefinition = true;
}
break;

case 'ImportDefaultSpecifier':
if (decl.importKind === 'type' || decl.importKind === 'typeof') {
this.isTypeDefinition = true;
this.isVariableDefinition = false;
} else {
this.isTypeDefinition = false;
this.isVariableDefinition = true;
}
break;

case 'ImportNamespaceSpecifier':
// not possible for a namespace import to be a type in flow
this.isTypeDefinition = false;
this.isVariableDefinition = true;
break;
}
}

+isTypeDefinition: boolean;
// all imports are treated as "type" definitions regardless of whether or
// not they have a "type" kind. You can import classes and enums as values
// and use them in type locations. Additionally namespace imports can be
// used in a type location (eg React.MixedElement).
+isTypeDefinition: boolean = true;
+isVariableDefinition: boolean;
}

Expand Down

0 comments on commit 8bebc9b

Please sign in to comment.