1

I am creating a angular shcematics project to propose suggestions to my angular project. I am trying to use the built in angular compiler to parse the code because libraries such as parse5 and htmlparser2 use standardized HTML, so it will provide suggestions such as adding values to template reference variables (e.g. #myInput=""), which will break the project. Now, when I run this code,

import { parseTemplate } from '@angular/compiler'

tree.visit(async (filePath: string, fileEntry) => {
if (fileEntry && filePath.endsWith('.html') && !filePath.includes('node_modules')) {
const parsedTemplate = parseTedmplate(fileEntry.content.toString('utf-8'), filePath)
}
})

I get the error:

[error] Error [ERR_INTERNAL_ASSRETION]: This is caused by either a bug in Node.js or incorrect usage of Node.js internals. ...
at assert  (node: internal/assert:14:11)
at ModuleLoader.getModuleJobForRequire (node:internal/modules/esm/loader:329:5)
at new ModuleJobSync (node:internal/modules/esm/module_job:313:34)

Have you guys faced this issue of trying to parse Angular with packages like parse5 and htmlparser2? Do you guys think I am taking the correct approach or have any suggestions? I'd love to hear your POV. Thanks

1 Answer 1

1

Angular Schematics is used to generate code as far as I know.

You are using the wrong toolkit for the wrong application.


I think ESLint does not parse the HTML, but looks at the Abstract syntax tree (AST), which is the structure of your code under the hood, which ESLint looks at and understands if there is a problem and throws an error, which VSCode displays, this looks very close to your requirement.


You are looking for custom ESlint or TSlint(deprecated) rules, which you can write specific for your application:

ESLint docs - Custom Rules

TSLint docs - Custom Rules

The best way to get inspiration is to look at an existing repo for custom eslint rules and use their own techniques to develop your requirement.

angular-eslint - github

tslint-angular - github

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.