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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 2x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 9x 9x 35x 35x 35x 35x 44x 1x 1x 34x 34x 34x 1x 1x 1x 1x 1x 1x 51x 51x 51x 1x 1x 50x 50x 50x 51x 1x 1x 49x 49x 49x 49x 49x 49x 51x 49x 49x 49x 1x 1x 1x 1x 1x 1x 114x 114x 114x 114x 114x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x | import { SyntaxKind, type SourceFile, ts } from "ts-morph"; import { deleteFromDecoratorArgArray, getDecoratorArgument, insertIntoDecoratorArgArray, } from "./decorator-utils"; /** * Finds the NgModule class that declares a given component. * * For example, if the source file is a component called `MyComponent`, * this function will find the declaring NgModule class, for example `MyComponentModule`. * It does this by finding the NgModule that declares the component in its `declarations` array. * * @param sourceFile The component source file to find the NgModule class for. */ export function findNgModuleClassForComponent(sourceFile: SourceFile) { const componentClassName = sourceFile.getClasses()[0]?.getName(); if (!componentClassName) { return; } const sourceFiles = sourceFile.getProject().getSourceFiles(); const ngModuleClass = sourceFiles.find((f) => { const ngModuleDecorator = f.getClasses()[0]?.getDecorator("NgModule"); if (!ngModuleDecorator) { return false; } const declarationsProperty = getDecoratorArgument( ngModuleDecorator, "declarations", ); if (!declarationsProperty) { return false; } const declarationsArray = declarationsProperty.getInitializerIfKind( SyntaxKind.ArrayLiteralExpression, ); if (!declarationsArray) { return false; } const componentClass = declarationsArray.getElements().find((e) => { const identifier = e.compilerNode as ts.Identifier; return identifier.getText() === componentClassName; }); if (!componentClass) { return false; } return true; }); return ngModuleClass; } /** * Finds the Angular component class for a given template file. * @param templateFile The template file to find the component class for. */ export function findComponentTypescriptFileForTemplateFile( templateFile: SourceFile, ) { const templateFilePath = templateFile.getFilePath(); const templateFileName = templateFile.getBaseNameWithoutExtension(); const sourceFiles = templateFile.getProject().getSourceFiles(); const componentTypescriptFile = sourceFiles.find((f) => { const filePath = f.getFilePath(); const fileName = f.getBaseNameWithoutExtension(); if (filePath === templateFilePath) { return false; } if (fileName !== templateFileName) { return false; } if (!isAngularComponentClass(f)) { return false; } // TODO could make this even more accurate by // checking if the templateUrl matches the templateFile. return true; }); return componentTypescriptFile; } /** * Adds a new import to the imports array in the Component decorator. * @param sourceFile The source file to add the import to. * @param importName The name of the import to add. */ export function addImportToComponentDecorator( sourceFile: SourceFile, importName: string, ) { if (!isAngularComponentStandalone(sourceFile)) { console.warn( "[Ionic Dev] Cannot add import to component decorator. Component is not standalone.", ); return; } const componentDecorator = getAngularComponentDecorator(sourceFile)!; insertIntoDecoratorArgArray(componentDecorator, "imports", importName); sourceFile.formatText(); } /** * Adds a new import to the imports array in the NgModule decorator. * @param sourceFile The source file to add the import to. * @param importName The name of the import to add. */ export const addImportToNgModuleDecorator = ( sourceFile: SourceFile, importName: string, ) => { const ngModuleDecorator = getAngularNgModuleDecorator(sourceFile); if (ngModuleDecorator) { insertIntoDecoratorArgArray(ngModuleDecorator, "imports", importName); sourceFile.formatText(); } }; /** * Removes an import from the imports array in the NgModule decorator. * @param sourceFile The source file to remove the import from. * @param importName The name of the import to remove. */ export const removeImportFromNgModuleDecorator = ( sourceFile: SourceFile, importName: string, ) => { const ngModuleDecorator = getAngularNgModuleDecorator(sourceFile); if (ngModuleDecorator) { deleteFromDecoratorArgArray(ngModuleDecorator, "imports", importName); } }; /** * Checks if the source file is an Angular component using * the standalone: true option in the @Component decorator. * @param sourceFile The source file to check. */ export function isAngularComponentStandalone(sourceFile: SourceFile) { if (!isAngularComponentClass(sourceFile)) { return false; } const componentDecorator = getAngularComponentDecorator(sourceFile); if (!componentDecorator) { return false; } const standalonePropertyAssignment = getDecoratorArgument( componentDecorator, "standalone", ); if (!standalonePropertyAssignment) { return false; } const standalonePropertyValue = standalonePropertyAssignment.getInitializerIfKind(SyntaxKind.TrueKeyword); if (!standalonePropertyValue) { return false; } return true; } /** * Checks if the source file is an Angular component class. * @param sourceFile The source file to check. */ export function isAngularComponentClass(sourceFile: SourceFile) { const componentDecorator = getAngularComponentDecorator(sourceFile); if (!componentDecorator) { return false; } const importDeclaration = sourceFile.getImportDeclaration("@angular/core"); if (!importDeclaration) { return false; } const namedImports = importDeclaration.getNamedImports(); const componentImportSpecifier = namedImports.find( (n) => n.getName() === "Component", ); if (!componentImportSpecifier) { return false; } return true; } /** * Returns the Angular component decorator. * @param sourceFile The source file to check. */ export function getAngularComponentDecorator(sourceFile: SourceFile) { const componentDecorator = sourceFile .getClasses()[0] ?.getDecorator("Component"); return componentDecorator; } /** * Returns the Angular NgModule decorator. * @param sourceFile The source file to check. */ export function getAngularNgModuleDecorator(sourceFile: SourceFile) { const ngModuleDecorator = sourceFile .getClasses()[0] ?.getDecorator("NgModule"); return ngModuleDecorator; } |