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 { join, resolve } from 'path';
import { import {
ArrayLiteralExpression, ArrayLiteralExpression,
createArrayLiteral,
createLiteral,
createNumericLiteral,
createObjectLiteral,
createPropertyAssignment,
Decorator, Decorator,
Expression, Expression,
factory,
Node, Node,
ObjectLiteralElementLike, ObjectLiteralElementLike,
ObjectLiteralExpression, ObjectLiteralExpression,
@ -102,9 +98,14 @@ export function convertValueToLiteral(val: any) {
return objectToObjectLiteral(val); return objectToObjectLiteral(val);
} }
if (typeof val === 'number') { 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 { function objectToObjectLiteral(obj: { [key: string]: any }): ObjectLiteralExpression {
const newProperties: ObjectLiteralElementLike[] = Object.keys(obj).map((key: string): ObjectLiteralElementLike => { 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 * FROM STENCIL
* Convert a js array into typescript AST * Convert a js array into typescript AST
* @param list array * @param list arrayÏ
* @returns Typescript Array Literal Expression * @returns Typescript Array Literal Expression
*/ */
function arrayToArrayLiteral(list: any[]): ArrayLiteralExpression { function arrayToArrayLiteral(list: any[]): ArrayLiteralExpression {
const newList: any[] = list.map(convertValueToLiteral); const newList: any[] = list.map(convertValueToLiteral);
return createArrayLiteral(newList); return factory.createArrayLiteralExpression(newList);
} }
export function getMethodsForDecorator(decoratorName: string) { 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'; import { getMethodsForDecorator } from '../helpers';
@ -41,11 +41,11 @@ function transformImports(file: SourceFile, ctx: TransformationContext, ngcBuild
decorators.forEach(d => (methods = getMethodsForDecorator(d).concat(methods))); 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); const methodNames = methodElements.map(el => el.escapedText);
importStatement.importClause.namedBindings.elements = [ importStatement.importClause.namedBindings.elements = [
createIdentifier('AwesomeCordovaNativePlugin'), factory.createIdentifier('AwesomeCordovaNativePlugin'),
...methodElements, ...methodElements,
...importStatement.importClause.namedBindings.elements.filter( ...importStatement.importClause.namedBindings.elements.filter(
el => keep.indexOf(el.name.text) !== -1 && methodNames.indexOf(el.name.text) === -1 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 { transformMethod } from './methods';
import { transformProperty } from './properties'; import { transformProperty } from './properties';
@ -17,7 +17,7 @@ export function transformMembers(cls: ClassDeclaration) {
propertyIndices.push(index); propertyIndices.push(index);
return member; return member;
case SyntaxKind.Constructor: case SyntaxKind.Constructor:
return createConstructor(undefined, undefined, member.parameters, member.body); return factory.createConstructorDeclaration(undefined, undefined, member.parameters, member.body);
default: default:
return member; // in case anything gets here by accident... return member; // in case anything gets here by accident...
} }

View File

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

View File

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

View File

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