Skip to content

Fix “noImplicitAny” errors in “x-parser.js”.#359

Merged
theengineear merged 1 commit intomainfrom
fix-no-implicit-any-in-parser
Mar 26, 2026
Merged

Fix “noImplicitAny” errors in “x-parser.js”.#359
theengineear merged 1 commit intomainfrom
fix-no-implicit-any-in-parser

Conversation

@theengineear
Copy link
Copy Markdown
Collaborator

With the change from TypeScript “5.x” to “6.x”, the “strict” config setting now defaults to “true”. We are simply working through the files one-by-one to be able to accept that new default change. It’s ultimately what we want, anyhow.

With the change from TypeScript “5.x” to “6.x”, the “strict” config
setting now defaults to “true”. We are simply working through the files
one-by-one to be able to accept that new default change. It’s ultimately
what we want, anyhow.
Comment thread x-parser.js
* parsing errors are allotted numbers #100-#199.
* @param {string | undefined} key
* @returns {string | undefined}
*/
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

☝️ — This was 99.9% just straight annotations. It turns out that this file was pretty well-suited for stricter typing… perhaps not a surprise.

Comment thread x-parser.js
let stringLength = 0;
let stringIndex = 0;
let nextStringIndex = 0;
let hasStringContext = false;
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

☝️ — Here is the bulk of the runtime “changes”. Rather than initializing to null… we simply initialize to a sensible, typed default. I.e., like '' or 0.

Note that we add a single flag called hasStringContext which we use in the error fork only. It allows us to not break our public contract with tools like eslint-plugin-x-element which look to see if start / end are specifically null — which indicates that no string context is available and a diagnostic needs to be printed at a more generic text span in an IDE (yes, we had a test covering that 😉).

Comment thread x-parser.js
break;
case XParser.#startTagClose:
if (XParser.#voidHtmlElements.has(tagName)) {
if (tagName !== null && XParser.#voidHtmlElements.has(tagName)) {
Copy link
Copy Markdown
Collaborator Author

@theengineear theengineear Mar 26, 2026

Choose a reason for hiding this comment

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

This runtime null check would be ugly to try and avoid since null cannot be a key for a string set. This single, strict null check only happens on a start-tag-close. I am not concerned that this is a performance issue and I cannot detect any sort of uptick from our performance tests.

Comment thread x-parser.js
break;
}
}
// Cast: nextStringIndex is guaranteed non-null here — it’s always
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This was a pleasant surprise. Because we now return never from our error functions (and because we initialize variables with types), TypeScript can fully infer this. Hooray.

Comment thread x-parser.js
const start = stringIndex;
const end = nextStringIndex;
const start = hasStringContext ? stringIndex : null;
const end = hasStringContext ? nextStringIndex : null;
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

☝️ — Again, we need to specifically set start / end to null if we don’t actually know where we are in the string. This is important for consumers looking to create text span diagnostics.

@theengineear
Copy link
Copy Markdown
Collaborator Author

FYI @klebba — working through these changes. Just getting a little better about annotating our internal types. So far, this is pretty straightforward as it’s just more commentary. I think we have tended to write our code with very strict, positional input parameters — so this isn’t much of a lift.

If we ever needed to do something drastic in the runtime, I would hesitate to do this stuff — but that’s not the pattern I’m seeing (so far).

Comment thread types/x-parser.d.ts
static #validTransition(string: any, stringIndex: any, value: any): any;
static #getErrorInfo(strings: any, stringsIndex: any, string: any, stringIndex: any): {
parsed: any;
/**
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Ideally, all this stuff would not end up in the emitted type declarations… but… I am hesitant to manually strip it (since it feels mostly harmless). I did leave commentary on microsoft/TypeScript#58145 about this. Maybe we can get TS to strip this for us one day.

@theengineear theengineear merged commit 04b0cc5 into main Mar 26, 2026
1 check passed
@theengineear theengineear deleted the fix-no-implicit-any-in-parser branch March 26, 2026 18:16
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