Browse Source

clean commas rule added

pull/22645/head
erdemcaygor 1 year ago
parent
commit
8612fb4450
  1. 71
      npm/ng-packs/packages/schematics/src/commands/change-theme/index.ts
  2. 7
      npm/ng-packs/packages/schematics/src/utils/ast.ts

71
npm/ng-packs/packages/schematics/src/commands/change-theme/index.ts

@ -13,6 +13,7 @@ import {
updateWorkspace,
WorkspaceDefinition,
getAppConfigPath,
removeEmptyElementsFromArrayLiteral,
} from '../../utils';
import { ThemeOptionsEnum } from './theme-options.enum';
import { findNodes, getDecoratorMetadata, getMetadataField } from '../../utils/angular/ast-utils';
@ -80,6 +81,7 @@ function updateAppModule(selectedProject: string, targetThemeName: ThemeOptionsE
insertImports(selectedProject, targetThemeName),
insertProviders(selectedProject, targetThemeName),
formatFile(appModulePath),
cleanNgModuleCommasRule(appModulePath, isStandalone),
]);
};
}
@ -361,3 +363,72 @@ export const formatFile = (filePath: string): Rule => {
return tree;
};
};
export function cleanNgModuleCommasRule(modulePath: string, isStandalone: boolean): Rule {
return (host: Tree) => {
const buffer = host.read(modulePath);
if (!buffer) throw new SchematicsException(`Cannot read ${modulePath}`);
const source = ts.createSourceFile(
modulePath,
buffer.toString('utf-8'),
ts.ScriptTarget.Latest,
true,
);
const recorder = host.beginUpdate(modulePath);
const printer = ts.createPrinter();
if (isStandalone) {
const varStatements = findNodes(source, ts.isVariableStatement);
for (const stmt of varStatements) {
const declList = stmt.declarationList;
for (const decl of declList.declarations) {
if (!decl.initializer || !ts.isObjectLiteralExpression(decl.initializer)) continue;
const obj = decl.initializer;
const providersProp = obj.properties.find(
prop =>
ts.isPropertyAssignment(prop) &&
ts.isIdentifier(prop.name) &&
prop.name.text === 'providers',
) as ts.PropertyAssignment;
if (!providersProp || !ts.isArrayLiteralExpression(providersProp.initializer)) continue;
const arrayLiteral = providersProp.initializer;
const cleanedArray = removeEmptyElementsFromArrayLiteral(arrayLiteral);
recorder.remove(arrayLiteral.getStart(), arrayLiteral.getWidth());
recorder.insertLeft(
arrayLiteral.getStart(),
printer.printNode(ts.EmitHint.Expression, cleanedArray, source),
);
}
}
} else {
const ngModuleNode = getDecoratorMetadata(source, 'NgModule', '@angular/core')[0];
if (!ngModuleNode) return host;
const metadataKeys = ['imports', 'providers'];
for (const key of metadataKeys) {
const metadataField = getMetadataField(ngModuleNode as ts.ObjectLiteralExpression, key);
if (!metadataField.length) continue;
const assignment = metadataField[0] as ts.PropertyAssignment;
const arrayLiteral = assignment.initializer as ts.ArrayLiteralExpression;
const cleanedArray = removeEmptyElementsFromArrayLiteral(arrayLiteral);
recorder.remove(arrayLiteral.getStart(), arrayLiteral.getWidth());
recorder.insertLeft(
arrayLiteral.getStart(),
printer.printNode(ts.EmitHint.Expression, cleanedArray, source),
);
}
}
host.commitUpdate(recorder);
return host;
};
}

7
npm/ng-packs/packages/schematics/src/utils/ast.ts

@ -35,3 +35,10 @@ export function isBooleanStringOrNumberLiteral(
node.kind === ts.SyntaxKind.FalseKeyword
);
}
export function removeEmptyElementsFromArrayLiteral(
array: ts.ArrayLiteralExpression,
): ts.ArrayLiteralExpression {
const cleaned = array.elements.filter(el => el.kind !== ts.SyntaxKind.OmittedExpression);
return ts.factory.updateArrayLiteralExpression(array, ts.factory.createNodeArray(cleaned));
}

Loading…
Cancel
Save