2021-09-28 03:04:28 +08:00
|
|
|
import {
|
2021-09-28 04:04:25 +08:00
|
|
|
Decorator,
|
|
|
|
factory,
|
2021-09-28 03:04:28 +08:00
|
|
|
SourceFile,
|
|
|
|
SyntaxKind,
|
|
|
|
TransformationContext,
|
|
|
|
TransformerFactory,
|
|
|
|
visitEachChild,
|
|
|
|
} from 'typescript';
|
2018-03-23 17:57:54 +08:00
|
|
|
|
2017-12-29 12:15:34 +08:00
|
|
|
import { Logger } from '../../logger';
|
2020-05-16 20:40:49 +08:00
|
|
|
import { convertValueToLiteral, getDecorator, getDecoratorArgs, getDecoratorName } from '../helpers';
|
2017-12-28 20:28:44 +08:00
|
|
|
import { transformMembers } from './members';
|
|
|
|
|
|
|
|
function transformClass(cls: any, ngcBuild?: boolean) {
|
2017-12-29 12:43:29 +08:00
|
|
|
Logger.profile('transformClass: ' + cls.name.text);
|
2017-12-28 20:28:44 +08:00
|
|
|
|
2018-03-23 17:57:54 +08:00
|
|
|
const pluginStatics = [];
|
2021-09-28 04:04:25 +08:00
|
|
|
const dec: Decorator = getDecorator(cls);
|
2017-12-28 20:28:44 +08:00
|
|
|
|
2018-03-23 17:57:54 +08:00
|
|
|
if (dec) {
|
|
|
|
const pluginDecoratorArgs = getDecoratorArgs(dec);
|
2017-12-28 20:28:44 +08:00
|
|
|
|
2018-03-23 17:57:54 +08:00
|
|
|
// add plugin decorator args as static properties of the plugin's class
|
|
|
|
for (const prop in pluginDecoratorArgs) {
|
|
|
|
pluginStatics.push(
|
2021-09-28 04:04:25 +08:00
|
|
|
factory.createPropertyDeclaration(
|
2017-12-28 20:28:44 +08:00
|
|
|
undefined,
|
2021-09-28 04:04:25 +08:00
|
|
|
[factory.createToken(SyntaxKind.StaticKeyword)],
|
|
|
|
factory.createIdentifier(prop),
|
2017-12-28 20:28:44 +08:00
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
convertValueToLiteral(pluginDecoratorArgs[prop])
|
2018-03-23 17:57:54 +08:00
|
|
|
)
|
|
|
|
);
|
2017-12-28 20:28:44 +08:00
|
|
|
}
|
2018-03-23 17:57:54 +08:00
|
|
|
}
|
2017-12-28 20:28:44 +08:00
|
|
|
|
2021-09-28 04:04:25 +08:00
|
|
|
cls = factory.createClassDeclaration(
|
2018-03-23 17:57:54 +08:00
|
|
|
ngcBuild && cls.decorators && cls.decorators.length
|
2021-09-28 04:09:05 +08:00
|
|
|
? cls.decorators.filter((d) => getDecoratorName(d) === 'Injectable')
|
2018-03-23 17:57:54 +08:00
|
|
|
: undefined, // remove Plugin and Injectable decorators
|
2021-09-28 04:04:25 +08:00
|
|
|
[factory.createToken(SyntaxKind.ExportKeyword)],
|
2018-03-23 17:57:54 +08:00
|
|
|
cls.name,
|
|
|
|
cls.typeParameters,
|
|
|
|
cls.heritageClauses,
|
|
|
|
[...transformMembers(cls), ...pluginStatics]
|
|
|
|
);
|
|
|
|
|
2018-06-23 00:24:09 +08:00
|
|
|
Logger.profile('transformClass: ' + cls.name.text);
|
2018-03-23 17:57:54 +08:00
|
|
|
return cls;
|
2017-12-28 20:28:44 +08:00
|
|
|
}
|
|
|
|
|
2021-09-28 03:04:28 +08:00
|
|
|
function transformClasses(file: SourceFile, ctx: TransformationContext, ngcBuild?: boolean) {
|
2017-12-29 12:43:29 +08:00
|
|
|
Logger.silly('Transforming file: ' + file.fileName);
|
2021-09-28 03:04:28 +08:00
|
|
|
return visitEachChild(
|
2018-03-23 17:57:54 +08:00
|
|
|
file,
|
2021-09-28 04:09:05 +08:00
|
|
|
(node) => {
|
2020-05-16 20:40:49 +08:00
|
|
|
if (
|
2021-09-28 03:04:28 +08:00
|
|
|
node.kind !== SyntaxKind.ClassDeclaration ||
|
2021-09-28 04:09:05 +08:00
|
|
|
(node.modifiers && node.modifiers.find((v) => v.kind === SyntaxKind.DeclareKeyword))
|
2020-05-16 20:40:49 +08:00
|
|
|
) {
|
2018-03-23 17:57:54 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
return transformClass(node, ngcBuild);
|
|
|
|
},
|
|
|
|
ctx
|
|
|
|
);
|
2017-12-28 20:28:44 +08:00
|
|
|
}
|
|
|
|
|
2021-09-28 03:04:28 +08:00
|
|
|
export function pluginClassTransformer(ngcBuild?: boolean): TransformerFactory<SourceFile> {
|
|
|
|
return (ctx: TransformationContext) => {
|
2021-09-28 04:09:05 +08:00
|
|
|
return (tsSourceFile) => {
|
2021-09-28 03:04:28 +08:00
|
|
|
if (tsSourceFile.fileName.indexOf('src/@awesome-cordova-plugins/plugins') > -1) {
|
2017-12-28 20:28:44 +08:00
|
|
|
return transformClasses(tsSourceFile, ctx, ngcBuild);
|
2021-09-28 03:04:28 +08:00
|
|
|
}
|
2017-12-28 20:28:44 +08:00
|
|
|
return tsSourceFile;
|
2017-12-29 12:43:29 +08:00
|
|
|
};
|
2017-12-28 20:28:44 +08:00
|
|
|
};
|
|
|
|
}
|