Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 9x 9x 9x 9x 9x 17x 17x 9x 9x 9x 9x 9x 9x 9x | import ts from 'typescript';
import {
PropMetadata,
type ComponentMetadata,
} from '@fds-uif/generator-base';
import { createPropertyApi } from './blocks/createPropertyApi.js';
import { createLwcImportStatement } from './blocks/createLwcImportStatement.js';
import { compile } from './utils/compile.js';
function createComponentClass(metadata: ComponentMetadata) {
// 1. Component class name
const className = metadata.name;
// 2. Base extends
const baseClass = ts.factory.createIdentifier('LightningElement');
// 3. Create the heritage clause: extends LightningElement
const heritageClause = ts.factory.createHeritageClause(
ts.SyntaxKind.ExtendsKeyword,
[ts.factory.createExpressionWithTypeArguments(baseClass, [])]
);
// 4. Create the class body (empty for this basic example)
const classBody = [] as ts.ClassElement[];
// 5. Props to @api
metadata.props.forEach((prop: PropMetadata) => {
if (prop.source !== 'slot') {
const api = createPropertyApi(prop.name, prop.type, prop.default);
if (api) {
classBody.push(api);
}
}
});
// 6. Create the class declaration with export default
const classDeclaration = ts.factory.createClassDeclaration(
[
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword),
ts.factory.createModifier(ts.SyntaxKind.DefaultKeyword)
],
ts.factory.createIdentifier(className),
undefined, // Type parameters (none here)
[heritageClause], // Heritage clauses (extends)
classBody
);
return classDeclaration;
}
export function generateClass(metadata: ComponentMetadata): string {
// 1. LWC Import and Class
const lwcImportStatement = createLwcImportStatement(metadata);
const lwcComponentClass = createComponentClass(metadata);
// 2. Create a source file (AST root) containing the statement
const sourceFile = ts.factory.createSourceFile(
[lwcImportStatement, lwcComponentClass],
ts.factory.createToken(ts.SyntaxKind.EndOfFileToken),
ts.NodeFlags.None
);
// 3. Verify component compiles
//compile(sourceFile);
// 4. Use the printer to convert the AST to a string
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
try {
return printer.printFile(sourceFile);
} catch(error) {
console.log(error);
throw error;
}
} |