refactor(build): remove deprecated typescript functions

This commit is contained in:
Daniel Sogl 2021-09-27 22:04:25 +02:00
parent 9e38c6b922
commit 66f5bbaa4e
6 changed files with 55 additions and 80 deletions

View File

@ -3,13 +3,9 @@ import { camelCase, clone } from 'lodash';
import { join, resolve } from 'path';
import {
ArrayLiteralExpression,
createArrayLiteral,
createLiteral,
createNumericLiteral,
createObjectLiteral,
createPropertyAssignment,
Decorator,
Expression,
factory,
Node,
ObjectLiteralElementLike,
ObjectLiteralExpression,
@ -102,9 +98,14 @@ export function convertValueToLiteral(val: any) {
return objectToObjectLiteral(val);
}
if (typeof val === 'number') {
return createNumericLiteral(String(val));
return factory.createNumericLiteral(val);
}
if (typeof val === 'string') {
return factory.createStringLiteral(val);
}
if (typeof val === 'boolean') {
return val ? factory.createTrue() : factory.createFalse();
}
return createLiteral(val);
}
/**
@ -115,21 +116,24 @@ export function convertValueToLiteral(val: any) {
*/
function objectToObjectLiteral(obj: { [key: string]: any }): ObjectLiteralExpression {
const newProperties: ObjectLiteralElementLike[] = Object.keys(obj).map((key: string): ObjectLiteralElementLike => {
return createPropertyAssignment(createLiteral(key), convertValueToLiteral(obj[key]) as Expression);
return factory.createPropertyAssignment(
factory.createStringLiteral(key),
convertValueToLiteral(obj[key]) as Expression
);
});
return createObjectLiteral(newProperties);
return factory.createObjectLiteralExpression(newProperties);
}
/**
* FROM STENCIL
* Convert a js array into typescript AST
* @param list array
* @param list arrayÏ
* @returns Typescript Array Literal Expression
*/
function arrayToArrayLiteral(list: any[]): ArrayLiteralExpression {
const newList: any[] = list.map(convertValueToLiteral);
return createArrayLiteral(newList);
return factory.createArrayLiteralExpression(newList);
}
export function getMethodsForDecorator(decoratorName: string) {

View File

@ -1,4 +1,4 @@
import { createIdentifier, SourceFile, SyntaxKind, TransformationContext } from 'typescript';
import { factory, SourceFile, SyntaxKind, TransformationContext } from 'typescript';
import { getMethodsForDecorator } from '../helpers';
@ -41,11 +41,11 @@ function transformImports(file: SourceFile, ctx: TransformationContext, ngcBuild
decorators.forEach(d => (methods = getMethodsForDecorator(d).concat(methods)));
const methodElements = methods.map(m => createIdentifier(m));
const methodElements = methods.map(m => factory.createIdentifier(m));
const methodNames = methodElements.map(el => el.escapedText);
importStatement.importClause.namedBindings.elements = [
createIdentifier('AwesomeCordovaNativePlugin'),
factory.createIdentifier('AwesomeCordovaNativePlugin'),
...methodElements,
...importStatement.importClause.namedBindings.elements.filter(
el => keep.indexOf(el.name.text) !== -1 && methodNames.indexOf(el.name.text) === -1

View File

@ -1,4 +1,4 @@
import { ClassDeclaration, createConstructor, SyntaxKind } from 'typescript';
import { ClassDeclaration, factory, SyntaxKind } from 'typescript';
import { transformMethod } from './methods';
import { transformProperty } from './properties';
@ -17,7 +17,7 @@ export function transformMembers(cls: ClassDeclaration) {
propertyIndices.push(index);
return member;
case SyntaxKind.Constructor:
return createConstructor(undefined, undefined, member.parameters, member.body);
return factory.createConstructorDeclaration(undefined, undefined, member.parameters, member.body);
default:
return member; // in case anything gets here by accident...
}

View File

@ -1,19 +1,4 @@
import {
createBinary,
createBlock,
createCall,
createIdentifier,
createIf,
createImmediatelyInvokedArrowFunction,
createLiteral,
createMethod,
createReturn,
createThis,
createTrue,
Expression,
MethodDeclaration,
SyntaxKind,
} from 'typescript';
import { Expression, factory, MethodDeclaration, SyntaxKind } from 'typescript';
import { Logger } from '../../logger';
import {
@ -32,7 +17,7 @@ export function transformMethod(method: MethodDeclaration) {
decoratorArgs = getDecoratorArgs(decorator);
try {
return createMethod(
return factory.createMethodDeclaration(
undefined,
undefined,
undefined,
@ -41,7 +26,7 @@ export function transformMethod(method: MethodDeclaration) {
method.typeParameters,
method.parameters,
method.type,
createBlock([createReturn(getMethodBlock(method, decoratorName, decoratorArgs))])
factory.createBlock([factory.createReturnStatement(getMethodBlock(method, decoratorName, decoratorArgs))])
);
} catch (e) {
Logger.error('Error transforming method: ' + (method.name as any).text);
@ -56,23 +41,23 @@ function getMethodBlock(method: MethodDeclaration, decoratorName: string, decora
case 'CordovaCheck':
case 'InstanceCheck':
// TODO remove function wrapper
return createImmediatelyInvokedArrowFunction([
createIf(
createBinary(
createCall(createIdentifier(decoratorMethod), undefined, [createThis()]),
return factory.createImmediatelyInvokedArrowFunction([
factory.createIfStatement(
factory.createBinaryExpression(
factory.createCallExpression(factory.createIdentifier(decoratorMethod), undefined, [factory.createThis()]),
SyntaxKind.EqualsEqualsEqualsToken,
createTrue()
factory.createTrue()
),
method.body
),
]);
default:
return createCall(createIdentifier(decoratorMethod), undefined, [
createThis(),
createLiteral(decoratorArgs?.methodName || (method.name as any).text),
return factory.createCallExpression(factory.createIdentifier(decoratorMethod), undefined, [
factory.createThis(),
factory.createStringLiteral(decoratorArgs?.methodName || (method.name as any).text),
convertValueToLiteral(decoratorArgs),
createIdentifier('arguments'),
factory.createIdentifier('arguments'),
]);
}
}

View File

@ -1,8 +1,6 @@
import {
createClassDeclaration,
createIdentifier,
createProperty,
createToken,
Decorator,
factory,
SourceFile,
SyntaxKind,
TransformationContext,
@ -18,7 +16,7 @@ function transformClass(cls: any, ngcBuild?: boolean) {
Logger.profile('transformClass: ' + cls.name.text);
const pluginStatics = [];
const dec: any = getDecorator(cls);
const dec: Decorator = getDecorator(cls);
if (dec) {
const pluginDecoratorArgs = getDecoratorArgs(dec);
@ -26,10 +24,10 @@ function transformClass(cls: any, ngcBuild?: boolean) {
// add plugin decorator args as static properties of the plugin's class
for (const prop in pluginDecoratorArgs) {
pluginStatics.push(
createProperty(
factory.createPropertyDeclaration(
undefined,
[createToken(SyntaxKind.StaticKeyword)],
createIdentifier(prop),
[factory.createToken(SyntaxKind.StaticKeyword)],
factory.createIdentifier(prop),
undefined,
undefined,
convertValueToLiteral(pluginDecoratorArgs[prop])
@ -38,11 +36,11 @@ function transformClass(cls: any, ngcBuild?: boolean) {
}
}
cls = createClassDeclaration(
cls = factory.createClassDeclaration(
ngcBuild && cls.decorators && cls.decorators.length
? cls.decorators.filter(d => getDecoratorName(d) === 'Injectable')
: undefined, // remove Plugin and Injectable decorators
[createToken(SyntaxKind.ExportKeyword)],
[factory.createToken(SyntaxKind.ExportKeyword)],
cls.name,
cls.typeParameters,
cls.heritageClauses,

View File

@ -1,16 +1,4 @@
import {
createBlock,
createCall,
createGetAccessor,
createIdentifier,
createLiteral,
createParameter,
createReturn,
createSetAccessor,
createStatement,
createThis,
PropertyDeclaration,
} from 'typescript';
import { factory, PropertyDeclaration } from 'typescript';
import { getDecorator, getDecoratorName } from '../helpers';
@ -34,33 +22,33 @@ export function transformProperty(members: any[], index: number) {
return property;
}
const getter = createGetAccessor(
const getter = factory.createGetAccessorDeclaration(
undefined,
undefined,
property.name,
undefined,
property.type,
createBlock([
createReturn(
createCall(createIdentifier(type + 'PropertyGet'), undefined, [
createThis(),
createLiteral((property.name as any).text),
factory.createBlock([
factory.createReturnStatement(
factory.createCallExpression(factory.createIdentifier(type + 'PropertyGet'), undefined, [
factory.createThis(),
factory.createStringLiteral((property.name as any).text),
])
),
])
);
const setter = createSetAccessor(
const setter = factory.createSetAccessorDeclaration(
undefined,
undefined,
property.name,
[createParameter(undefined, undefined, undefined, 'value', undefined, property.type)],
createBlock([
createStatement(
createCall(createIdentifier(type + 'PropertySet'), undefined, [
createThis(),
createLiteral((property.name as any).text),
createIdentifier('value'),
[factory.createParameterDeclaration(undefined, undefined, undefined, 'value', undefined, property.type)],
factory.createBlock([
factory.createExpressionStatement(
factory.createCallExpression(factory.createIdentifier(type + 'PropertySet'), undefined, [
factory.createThis(),
factory.createStringLiteral((property.name as any).text),
factory.createIdentifier('value'),
])
),
])