typescript#ClassDeclaration TypeScript Examples
The following examples show how to use
typescript#ClassDeclaration.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: mini-program-compiler.service.ts From angular-miniprogram with MIT License | 6 votes |
private getLibraryDirectiveMeta(
classDeclaration: ts.ClassDeclaration
): DirectiveMetaFromLibrary | undefined {
let listeners: string[] = [];
let properties: string[] = [];
const directiveName = classDeclaration.name!.getText();
const selector = createCssSelectorForTs(classDeclaration.getSourceFile());
const listenersNode = selector.queryOne(
`VariableDeclaration[name=${directiveName}_${LIBRARY_DIRECTIVE_LISTENERS_SUFFIX}]`
) as ts.VariableDeclaration;
if (listenersNode) {
listeners = literalResolve(listenersNode.type!.getText());
}
const propertiesNode = selector.queryOne(
`VariableDeclaration[name=${directiveName}_${LIBRARY_DIRECTIVE_PROPERTIES_SUFFIX}]`
) as ts.VariableDeclaration;
if (propertiesNode) {
properties = literalResolve(propertiesNode.type!.getText());
}
return {
isComponent: false,
listeners: listeners,
properties: properties,
};
}
Example #2
Source File: mini-program-compiler.service.ts From angular-miniprogram with MIT License | 6 votes |
private getLibraryComponentMeta(
classDeclaration: ts.ClassDeclaration
): ComponentMetaFromLibrary | undefined {
const directiveName = classDeclaration.name!.getText();
const selector = createCssSelectorForTs(classDeclaration.getSourceFile());
const exportPathNode = selector.queryOne(
`VariableDeclaration[name=${directiveName}_${LIBRARY_COMPONENT_OUTPUT_PATH_SUFFIX}]`
) as ts.VariableDeclaration;
if (!exportPathNode) {
return undefined;
}
const exportPath = exportPathNode.type!.getText();
return {
exportPath: literalResolve(exportPath),
...this.getLibraryDirectiveMeta(classDeclaration)!,
isComponent: true,
};
}
Example #3
Source File: mini-program-compiler.service.ts From angular-miniprogram with MIT License | 5 votes |
init() {
this.ngCompiler = this.ngTscProgram.compiler;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const traitCompiler: TraitCompiler = (this.ngCompiler as any).compilation
.traitCompiler;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const classes = (traitCompiler as any).classes as Map<
ts.ClassDeclaration,
ClassRecord
>;
for (const [classDeclaration, classRecord] of classes) {
const fileName = classDeclaration.getSourceFile().fileName;
const componentTraits = classRecord.traits.filter(
(trait) => trait.handler.name === 'ComponentDecoratorHandler'
);
if (componentTraits.length > 1) {
throw new Error('组件装饰器异常');
}
componentTraits.forEach((trait) => {
const meta: R3ComponentMetadata = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...(trait as any).analysis?.meta,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...(trait as any).resolution,
};
this.resolvedDataGroup.style.set(
path.normalize(fileName),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
((trait as any)?.analysis?.styleUrls || []).map(
(item: { url: string }) => this.resolveStyleUrl(fileName, item.url)
)
);
this.componentMap.set(
ts.getOriginalNode(classDeclaration) as ts.ClassDeclaration,
meta
);
});
const directiveTraits = classRecord.traits.filter(
(trait) => trait.handler.name === 'DirectiveDecoratorHandler'
);
if (directiveTraits.length > 1) {
throw new Error('指令装饰器异常');
}
directiveTraits.forEach((trait) => {
this.directiveMap.set(
ts.getOriginalNode(classDeclaration) as ts.ClassDeclaration,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(trait as any).analysis?.meta
);
});
}
}
Example #4
Source File: mini-program-compiler.service.ts From angular-miniprogram with MIT License | 5 votes |
async exportComponentBuildMetaMap() {
const { SelectorMatcher, CssSelector } = await angularCompilerPromise;
for (const [classDeclaration, meta] of this.componentMap) {
const fileName = path.normalize(
classDeclaration.getSourceFile().fileName
);
let directiveMatcher: SelectorMatcher | undefined;
if (meta.directives.length > 0) {
const matcher = new SelectorMatcher();
for (const directive of meta.directives) {
const selector = directive.selector;
const directiveClassDeclaration = ts.getOriginalNode(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(directive as any).ref.node
) as ts.ClassDeclaration;
const directiveMeta = this.directiveMap.get(
directiveClassDeclaration
);
const componentMeta = this.componentMap.get(
directiveClassDeclaration
);
let libraryMeta: MetaFromLibrary | undefined;
if (directive.isComponent) {
libraryMeta = this.getLibraryComponentMeta(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(directive as any).ref.node
);
}
if (!directive.isComponent && !directiveMeta) {
libraryMeta = this.getLibraryDirectiveMeta(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(directive as any).ref.node
);
}
matcher.addSelectables(CssSelector.parse(selector), {
directive,
directiveMeta,
componentMeta,
libraryMeta,
});
}
directiveMatcher = matcher;
}
const componentBuildMeta = this.buildComponentMeta(
directiveMatcher,
meta
);
this.resolvedDataGroup.outputContent.set(
path.normalize(fileName),
componentBuildMeta.content
);
this.resolvedDataGroup.useComponentPath.set(
path.normalize(fileName),
componentBuildMeta.useComponentPath
);
for (const key in componentBuildMeta.otherMetaGroup) {
if (
Object.prototype.hasOwnProperty.call(
componentBuildMeta.otherMetaGroup,
key
)
) {
const element = componentBuildMeta.otherMetaGroup[key];
this.resolvedDataGroup.otherMetaCollectionGroup[key] =
this.resolvedDataGroup.otherMetaCollectionGroup[key] ||
new MetaCollection();
this.resolvedDataGroup.otherMetaCollectionGroup[key].merge(element);
}
}
}
this.resolvedDataGroup.useComponentPath.forEach((value, key) => {
value.libraryPath = Array.from(new Set(value.libraryPath));
value.localPath = Array.from(new Set(value.localPath));
});
return this.resolvedDataGroup;
}
Example #5
Source File: mini-program-compiler.service.ts From angular-miniprogram with MIT License | 5 votes |
private componentMap = new Map<ClassDeclaration, R3ComponentMetadata>();
Example #6
Source File: mini-program-compiler.service.ts From angular-miniprogram with MIT License | 5 votes |
private directiveMap = new Map<ClassDeclaration, R3DirectiveMetadata>();