Skip to content

feat: support Angular selectorless syntax #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/angular-html-parser/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"recommendations": [
"esbenp.prettier-vscode",
"editorconfig.editorconfig",
// "dbaeumer.vscode-eslint",
"streetsidesoftware.code-spell-checker"
]
}
12 changes: 12 additions & 0 deletions packages/angular-html-parser/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"[javascript][typescript][json][jsonc][markdown][yaml]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
// "[javascript][typescript]": {
// "editor.codeActionsOnSave": {
// "source.fixAll.eslint": "explicit",
// },
// },
"prettier.requireConfig": true
}
7 changes: 7 additions & 0 deletions packages/angular-html-parser/src/index.ts
Original file line number Diff line number Diff line change
@@ -49,6 +49,11 @@ export interface ParseOptions {
* tokenize angular let declaration syntax
*/
tokenizeAngularLetDeclaration?: boolean;

/**
* enable angular selectorless syntax
*/
enableAngularSelectorlessSyntax?: boolean;
}

export function parse(
@@ -62,6 +67,7 @@ export function parse(
getTagContentType,
tokenizeAngularBlocks = false,
tokenizeAngularLetDeclaration = false,
enableAngularSelectorlessSyntax = false,
} = options;
return getParser().parse(
input,
@@ -73,6 +79,7 @@ export function parse(
allowHtmComponentClosingTags,
tokenizeBlocks: tokenizeAngularBlocks,
tokenizeLet: tokenizeAngularLetDeclaration,
selectorlessEnabled: enableAngularSelectorlessSyntax,
},
isTagNameCaseSensitive,
getTagContentType,
86 changes: 85 additions & 1 deletion packages/angular-html-parser/test/index_spec.ts
Original file line number Diff line number Diff line change
@@ -68,7 +68,7 @@ describe("AST format", () => {
]);
});

it("should have `type` property when tokenizeBlocks is enabled", () => {
it("should support 'tokenizeAngularBlocks'", () => {
const input = `@if (user.isHuman) { <p>Hello human</p> }`;
const ast = parse(input, { tokenizeAngularBlocks: true });
expect(ast.rootNodes).toEqual([
@@ -95,4 +95,88 @@ describe("AST format", () => {
}),
]);
});

it("should support 'tokenizeAngularLetDeclaration'", () => {
const input = `@let foo = 'bar';`;
const ast = parse(input, { tokenizeAngularLetDeclaration: true });
expect(ast.rootNodes).toEqual([
expect.objectContaining({
name: "foo",
type: "letDeclaration",
value: "'bar'",
}),
]);
});

// https://github.com/angular/angular/pull/60724
it("should support 'enableAngularSelectorlessSyntax'", () => {
{
const ast = parse("<div @Dir></div>", {
enableAngularSelectorlessSyntax: true,
});
expect(ast.rootNodes).toEqual([
expect.objectContaining({
name: "div",
type: "element",
directives: [
expect.objectContaining({
name: "Dir",
type: "directive",
}),
],
}),
]);
}

{
const ast = parse("<MyComp>Hello</MyComp>", {
enableAngularSelectorlessSyntax: true,
});

expect(ast.rootNodes).toEqual([
expect.objectContaining({
fullName: "MyComp",
componentName: "MyComp",
type: "component",
}),
]);
}

{
const ast = parse("<MyComp/>", { enableAngularSelectorlessSyntax: true });
expect(ast.rootNodes).toEqual([
expect.objectContaining({
fullName: "MyComp",
componentName: "MyComp",
type: "component",
}),
]);
}

{
const ast = parse("<MyComp:button>Hello</MyComp:button>", {
enableAngularSelectorlessSyntax: true,
});
expect(ast.rootNodes).toEqual([
expect.objectContaining({
fullName: "MyComp:button",
componentName: "MyComp",
type: "component",
}),
]);
}

{
const ast = parse("<MyComp:svg:title>Hello</MyComp:svg:title>", {
enableAngularSelectorlessSyntax: true,
});
expect(ast.rootNodes).toEqual([
expect.objectContaining({
fullName: "MyComp:svg:title",
componentName: "MyComp",
type: "component",
}),
]);
}
});
});