Skip to content

Commit

Permalink
perf(typescript): upgrade to latest version of typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
uittorio committed Aug 8, 2021
1 parent c5aa380 commit 69e52e1
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 21 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"ts-node": "^10.1.0",
"tsconfig-paths": "^3.10.1",
"ttypescript": "1.5.12",
"typescript": "^4.2.3",
"typescript": "^4.3.5",
"webpack": "^5.49.0",
"webpack-cli": "^4.7.2",
"webpack-merge": "^5.8.0",
Expand Down
22 changes: 18 additions & 4 deletions src/transformer/descriptor/helper/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ export namespace TypescriptHelper {
}

export function GetDeclarationFromSymbol(symbol: ts.Symbol): ts.Declaration {
const declaration: ts.Declaration = GetFirstValidDeclaration(
symbol.declarations
);
const declarations: ts.Declaration[] | undefined = symbol.declarations;

if (!declarations) {
throw new Error(
`Failed to look up declarations for \`${symbol.getName()}'.`
);
}

const declaration: ts.Declaration = GetFirstValidDeclaration(declarations);

if (isImportExportDeclaration(declaration)) {
return GetDeclarationForImport(declaration);
Expand All @@ -52,7 +58,15 @@ export namespace TypescriptHelper {
export function GetConcreteDeclarationFromSymbol(
symbol: ts.Symbol
): ts.Declaration {
const declaration: ts.Declaration = symbol.declarations[0];
const declarations: ts.Declaration[] | undefined = symbol.declarations;

if (!declarations) {
throw new Error(
`Failed to look up declarations for \`${symbol.getName()}'.`
);
}

const declaration: ts.Declaration = declarations[0];

if (isImportExportDeclaration(declaration)) {
return GetConcreteDeclarationForImport(declaration);
Expand Down
2 changes: 1 addition & 1 deletion src/transformer/descriptor/mock/mockProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function GetMockPropertiesFromSymbol(
.filter((prop: ts.Symbol) => !!prop.declarations) // Dynamically generated properties (mapped types) do not have declarations
.map(
(prop: ts.Symbol) =>
prop.declarations.filter(
prop.declarations?.filter(
(declaration: ts.Declaration) => !core.ts.isSetAccessor(declaration)
)[0]
)
Expand Down
10 changes: 6 additions & 4 deletions src/transformer/descriptor/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
createTypeLiteralNode,
createTypeQueryNode,
} from '../../../typescriptFactory/typescriptFactory';
import GetDeclarationFromSymbol = TypescriptHelper.GetDeclarationFromSymbol;

type ExternalSource = ts.SourceFile | ts.ModuleDeclaration;

Expand All @@ -35,7 +36,8 @@ export function GetModuleDescriptor(
}

const symbol: ts.Symbol = typeChecker.getAliasedSymbol(symbolAlias);
const externalModuleDeclaration: ts.NamedDeclaration = symbol.declarations[0];
const externalModuleDeclaration: ts.NamedDeclaration =
GetDeclarationFromSymbol(symbol);

if (isExternalSource(externalModuleDeclaration)) {
return GetPropertiesFromSourceFileOrModuleDeclarationDescriptor(
Expand Down Expand Up @@ -83,12 +85,12 @@ export function GetPropertiesFromSourceFileOrModuleDeclaration(
const moduleExports: ts.Symbol[] = typeChecker.getExportsOfModule(symbol);

return moduleExports
.map((prop: ts.Symbol): ModuleExportsDeclarations => {
.map((prop: ts.Symbol): Partial<ModuleExportsDeclarations> => {
const originalSymbol: ts.Symbol =
TypescriptHelper.GetAliasedSymbolSafe(prop);
const originalDeclaration: ts.NamedDeclaration =
const originalDeclaration: ts.NamedDeclaration | undefined =
originalSymbol?.declarations?.[0];
const declaration: ts.Declaration = prop?.declarations?.[0];
const declaration: ts.Declaration | undefined = prop?.declarations?.[0];

return {
declaration,
Expand Down
3 changes: 2 additions & 1 deletion src/transformer/descriptor/typeParameter/typeParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
createVariableDeclarationList,
createVariableStatement,
} from '../../../typescriptFactory/typescriptFactory';
import GetDeclarationFromSymbol = TypescriptHelper.GetDeclarationFromSymbol;

export function GetTypeParameterDescriptor(
node: ts.TypeParameterDeclaration,
Expand All @@ -35,7 +36,7 @@ export function GetTypeParameterDescriptor(
? GetDescriptor(node.default, scope)
: GetNullDescriptor();

const declaration: ts.Declaration = type.symbol.declarations[0];
const declaration: ts.Declaration = GetDeclarationFromSymbol(type.symbol);
const typeDeclaration: ts.Declaration | undefined =
TypescriptHelper.GetTypeParameterOwnerMock(declaration);

Expand Down
10 changes: 8 additions & 2 deletions src/transformer/descriptor/typeQuery/typeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function GetTypeQueryDescriptor(
): ts.Expression {
const symbol: ts.Symbol | undefined = getTypeQuerySymbol(node);

if (!symbol?.declarations.length) {
if (!symbol?.declarations?.length) {
return GetUndefinedDescriptor();
}

Expand Down Expand Up @@ -131,7 +131,13 @@ function getTypeQuerySymbol(node: ts.TypeQueryNode): ts.Symbol | undefined {
function getTypeQueryDeclarationFromSymbol(
symbol: ts.Symbol
): ts.NamedDeclaration {
const declaration: ts.Declaration = symbol.declarations[0];
const declaration: ts.Declaration | undefined = symbol.declarations?.[0];

if (!declaration) {
throw new Error(
`Failed to look up declaration for \`${symbol.getName()}'.`
);
}

if (core.ts.isImportEqualsDeclaration(declaration)) {
return declaration;
Expand Down
4 changes: 2 additions & 2 deletions src/transformer/mock/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
createBinaryExpression,
createBlock,
createCall,
createEmptyStatement,
createOmittedExpression,
createExpressionStatement,
createForStatement,
createIdentifier,
Expand Down Expand Up @@ -71,7 +71,7 @@ export function storeRegisterMock(
Logger('RegisterMock').error(
'registerMock can be used only to mock type references.'
);
return createEmptyStatement();
return createOmittedExpression();
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/typescriptFactory/typescriptFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
ColonToken,
ConditionalExpression,
ElementAccessExpression,
EmptyStatement,
OmittedExpression,
Expression,
ExpressionStatement,
ForStatement,
Expand Down Expand Up @@ -463,8 +463,8 @@ export function createPostfix(
return core.ts.factory.createPostfixUnaryExpression(operand, operator);
}

export function createEmptyStatement(): EmptyStatement {
return core.ts.factory.createEmptyStatement();
export function createOmittedExpression(): OmittedExpression {
return core.ts.factory.createOmittedExpression();
}

export function createTypeReferenceNode(
Expand Down

0 comments on commit 69e52e1

Please sign in to comment.