@@ -4,36 +4,46 @@ import { Project, SyntaxKind } from "ts-morph";
44 * @typedef {import("ts-morph").StringLiteral | import("ts-morph").NoSubstitutionTemplateLiteral } StyleLiteral
55 */
66
7- export class TypescriptParser {
8- /**
9- * Creates an instance of TypescriptParser.
10- *
11- * @param { import("postcss").Input } input - The PostCSS input containing the source CSS and its originating file.
12- */
13- constructor ( input ) {
14- const project = new Project ( {
15- useInMemoryFileSystem : true ,
16- } ) ;
7+ /**
8+ * Parses TypeScript source to extract style literals from component decorators.
9+ *
10+ * @param { import("postcss").Input } input - The PostCSS input containing the source CSS and its originating file.
11+ * @returns { StyleLiteral[] } An array of style literals extracted from the components within the source file.
12+ */
13+ export function parseTypescript ( input ) {
14+ const project = new Project ( {
15+ useInMemoryFileSystem : true ,
16+ } ) ;
1717
18- this . sourceFile = project . createSourceFile ( input . from , input . css ) ;
19- }
18+ const sourceFile = project . createSourceFile ( input . from , input . css ) ;
19+ return extractStyleLiterals ( sourceFile ) ;
20+ }
2021
21- /**
22- * Parses the TypeScript source file to extract style literals from component decorators.
23- *
24- * @returns {StyleLiteral[] } An array of style literals extracted from the components within the source file.
25- */
26- parse ( ) {
27- return this . sourceFile
28- . getClasses ( )
29- . flatMap ( ( cls ) => cls . getDecorators ( ) )
30- . filter ( isComponentDecorator )
31- . flatMap ( ( decorator ) => decorator . getArguments ( ) )
32- . filter ( isObjectLiteral )
33- . flatMap ( getStylesProperty )
34- . flatMap ( getStyleValues )
35- . map ( castToStyleLiteral ) ;
36- }
22+ /**
23+ * Extracts style literals from a source file.
24+ *
25+ * @param {import("ts-morph").SourceFile } sourceFile - The TypeScript source file to parse.
26+ * @returns {StyleLiteral[] } An array of extracted style literals.
27+ */
28+ function extractStyleLiterals ( sourceFile ) {
29+ return sourceFile
30+ . getClasses ( )
31+ . flatMap ( getComponentDecorators )
32+ . flatMap ( getDecoratorArguments )
33+ . filter ( isObjectLiteral )
34+ . flatMap ( getStylesProperty )
35+ . flatMap ( getStyleValues )
36+ . map ( castToStyleLiteral ) ;
37+ }
38+
39+ /**
40+ * Retrieves component decorators from a class.
41+ *
42+ * @param {import("ts-morph").ClassDeclaration } cls - The class declaration.
43+ * @returns {import("ts-morph").Decorator[] } An array of Component decorators.
44+ */
45+ function getComponentDecorators ( cls ) {
46+ return cls . getDecorators ( ) . filter ( isComponentDecorator ) ;
3747}
3848
3949/**
@@ -46,6 +56,16 @@ function isComponentDecorator(decorator) {
4656 return decorator . getName ( ) === "Component" ;
4757}
4858
59+ /**
60+ * Retrieves arguments from a decorator.
61+ *
62+ * @param {import("ts-morph").Decorator } decorator - The decorator.
63+ * @returns {import("ts-morph").Node[] } An array of decorator arguments.
64+ */
65+ function getDecoratorArguments ( decorator ) {
66+ return decorator . getArguments ( ) ;
67+ }
68+
4969/**
5070 * Checks if the provided expression is an object literal.
5171 *
0 commit comments