Skip to content
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

Improve parser error messaging and factoring. #283

Merged
merged 1 commit into from
Mar 6, 2025

Conversation

theengineear
Copy link
Collaborator

Taking on some of the language tweaks from #248. Also switch-ing on the values (versus using a Map) so that these cases can be enforced by code coverage.

We likely have a couple messages which are impossible to get to. It’d be good to be forced to either remove them or find a way to prove that they are reachable via our test suite.

For now, I reduced the coverage target to keep this change set smaller, but I will address that in follow-up efforts.

Taking on some of the language tweaks from #248. Also `switch`-ing on
the values (versus using a `Map`) so that these cases can be enforced by
code coverage.

We likely have a couple messages which are impossible to get to. It’d be
good to be forced to either remove them or find a way to prove that they
are reachable via our test suite.

For now, I reduced the coverage target to keep this change set smaller,
but I will address that in follow-up efforts.
@@ -291,6 +291,49 @@ html`<div>${fragment}</div>`;
// Error: Unexpected child element count of zero for given DocumentFragment.
```

## Supported native tags
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As requested, added a section for #supported-native-tags in our TEMPLATES.md file (so that we can link to it from our error messaging).

order to reduce complexity and improve performance. The following tags are
supported:

```html
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought it would be nice to present this with html since that’s what it’s about. Open to different formatting options, but this felt like it would get the point across in a concise way.

@@ -7,7 +7,7 @@ import '../x-template.js';

// Set a high bar for code coverage!
coverage(new URL('../x-element.js', import.meta.url).href, 100);
coverage(new URL('../x-parser.js', import.meta.url).href, 100);
coverage(new URL('../x-parser.js', import.meta.url).href, 97); // TODO: Get back to 100%!
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix this later (pinky promise).

@@ -699,6 +699,42 @@ describe('odds and ends', () => {
});

describe('errors', () => {
it('throws when initial markup cannot be parsed', () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are a couple cases I was able to add to improve our coverage.

['#157', 'Forbidden declarative shadow root used (e.g., `<template shadowrootmode="open">`).'],
]);
static #getErrorMessage(key) {
switch (key) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you switch on this stuff, this will all get enforced by our code coverage targets. I had initially started trying to duplicate this mapping and enforce it in our tests… but that’s brittle. It’s really nice that this will just always be enforced moving forward.

static #getErrorMessage(key) {
switch (key) {
case '#100': return 'Could not parse template markup (at template start).';
case '#101': return 'Could not parse template markup (after text content).';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took on most of the language tweaks. Thanks @klebba!

@@ -519,15 +532,15 @@ export class XParser {
const { parsed, notParsed } = XParser.#getErrorInfo(strings, stringsIndex, string, stringIndex);
const valueForbidden = XParser.#forbiddenTransition(string, stringIndex, value);
const valueMalformed = XParser.#invalidTransition(string, stringIndex, value);
const errorMessagesKey = valueForbidden
? XParser.#valueForbiddenToErrorMessagesKey.get(valueForbidden)
const errorMessageKey = valueForbidden
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored some of the variable names to match the new setup. Unfortunately, that made this diff a little extra ugly.

@theengineear
Copy link
Collaborator Author

FYI @klebba — this change set doesn’t close down #248, but it should be a good step forward.

@theengineear theengineear merged commit 4334db0 into main Mar 6, 2025
1 check passed
@theengineear theengineear deleted the improve-parser-errors branch March 6, 2025 03:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant