-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(typeQuery): add support for typeof of an imported module (#128)
* add support for typeof of an imported module * fix * make sure type query of module works for inferred variable type
- Loading branch information
Showing
13 changed files
with
392 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as ts from 'typescript'; | ||
|
||
export type PropertyLike = ts.PropertyDeclaration | ts.PropertySignature | ts.MethodSignature; | ||
|
||
export function isPropertyLike(prop: ts.Node): prop is PropertyLike { | ||
return prop.kind === ts.SyntaxKind.PropertyDeclaration || prop.kind === ts.SyntaxKind.PropertySignature || prop.kind === ts.SyntaxKind.MethodSignature; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as ts from 'typescript'; | ||
|
||
export type SignatureLike = ts.CallSignatureDeclaration | ts.ConstructSignatureDeclaration; | ||
|
||
export function isSignatureLike(prop: ts.Node): prop is SignatureLike { | ||
return prop.kind === ts.SyntaxKind.CallSignature || prop.kind === ts.SyntaxKind.ConstructSignature; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as ts from 'typescript'; | ||
import { TypescriptCreator } from '../../helper/creator'; | ||
import { Scope } from '../../scope/scope'; | ||
import { TypeChecker } from '../../typeChecker/typeChecker'; | ||
import { GetDescriptor } from '../descriptor'; | ||
import { TypescriptHelper } from '../helper/helper'; | ||
import { GetMockPropertiesFromDeclarations } from '../mock/mockProperties'; | ||
import { PropertyLike } from '../mock/propertyLike'; | ||
import { GetTypeQueryDescriptorFromDeclaration } from '../typeQuery/typeQuery'; | ||
|
||
export function GetModuleDescriptor(node: ts.NamedDeclaration, scope: Scope): ts.Expression { | ||
const typeChecker: ts.TypeChecker = TypeChecker(); | ||
|
||
const symbolAlias: ts.Symbol = typeChecker.getSymbolAtLocation(node.name); | ||
const symbol: ts.Symbol = typeChecker.getAliasedSymbol(symbolAlias); | ||
const externalModuleDeclaration: ts.NamedDeclaration = symbol.declarations[0]; | ||
|
||
if (ts.isSourceFile(externalModuleDeclaration) || ts.isModuleDeclaration(externalModuleDeclaration)) { | ||
const moduleExports: ts.Symbol[] = typeChecker.getExportsOfModule(symbol); | ||
|
||
const properties: PropertyLike[] = moduleExports.map((prop: ts.Symbol): PropertyLike => { | ||
const originalSymbol: ts.Symbol = TypescriptHelper.GetAliasedSymbolSafe(prop); | ||
const originalDeclaration: ts.NamedDeclaration = originalSymbol.declarations[0]; | ||
const declaration: ts.Declaration = prop.declarations[0]; | ||
if (ts.isExportAssignment(declaration)) { | ||
return TypescriptCreator.createProperty('default', ts.createTypeQueryNode(originalDeclaration.name as ts.Identifier)); | ||
} | ||
return TypescriptCreator.createProperty(originalDeclaration.name as ts.Identifier, ts.createTypeQueryNode(originalDeclaration.name as ts.Identifier)); | ||
}); | ||
|
||
return GetMockPropertiesFromDeclarations(properties, [], scope); | ||
} | ||
|
||
return GetTypeQueryDescriptorFromDeclaration(externalModuleDeclaration, scope); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.