Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,6 @@ dist
# Vite logs files
vite.config.js.timestamp-*
vite.config.ts.timestamp-*

# IDE files
.idea
36 changes: 15 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@
},
"peerDependencies": {
"prettier": "^3.0.0"
},
"dependencies": {
"angular-html-parser": "^10.6.1"
}
}
23 changes: 23 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Placeholder,
RootNode,
} from "./jte";
import {parseHtml} from "angular-html-parser";

const NOT_FOUND = -1;
const IGNORE_START = /^<!--\s*prettier-ignore-start\s*-->/;
Expand All @@ -30,6 +31,28 @@ export const parse: Parser<Node>["parse"] = (text) => {
const generatePlaceholder = placeholderGenerator(text);
root.content = parseFragment(text, root.nodes, generatePlaceholder);

// Validate HTML using the same parser prettier uses internally
const { errors } = parseHtml(root.content, {
canSelfClose: true,
allowHtmComponentClosingTags: true,
});

if (errors.length > 0) {
const error = errors[0];
const { msg, span: { start, end } } = error;
const line = start.line + 1;
const col = start.col;

const err = new SyntaxError(
`${msg} (${line}:${col + 1})`
);
(err as any).loc = {
start: { line, column: col + 1 },
end: { line: end.line + 1, column: end.col },
Comment thread
mfillon marked this conversation as resolved.
Outdated
};
throw err;
}

return root;
};

Expand Down
10 changes: 10 additions & 0 deletions test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ test("keeps broken directive text untouched", async () => {
.content,
).toEqual("<div>@for(var entry : entries </div>");
});
test("throws on invalid HTML nesting", () => {
expect(() =>
parse("<p><ul><li>item</li></ul></p>", {} as ParserOptions)
).toThrow(SyntaxError);
});
test("throws on malformed tag", () => {
expect(() =>
parse("<strong>Title</strong>\n</\n<ul><li>item</li></ul>", {} as ParserOptions)
).toThrow(SyntaxError);
});