Skip to content

fix: type EntityDecoderOptions.setXmlVersion's version as a number - #880

Merged
amitguptagwl merged 1 commit into
NaturalIntelligence:masterfrom
YevheniiKotyrlo:fix/entity-decoder-xml-version-type
Sep 29, 2026
Merged

amitguptagwl merged 1 commit into
NaturalIntelligence:masterfrom
YevheniiKotyrlo:fix/entity-decoder-xml-version-type

Conversation

@YevheniiKotyrlo

Copy link
Copy Markdown
Contributor

Purpose / Goal

EntityDecoderOptions.setXmlVersion is declared as taking a string, but the parser passes the number it read off <?xml version?>:

// src/xmlparser/OrderedObjParser.js
this.entityDecoder.setXmlVersion(Number(ver) || 1.0);
// src/fxp.d.ts, lib/fxp.d.cts, docs/v4, v5/2.XMLparseOptions.md
setXmlVersion: (version: string) => void;

No issue was raised for this, so here is input / expected / actual, for anyone passing their own entityDecoder (supported since 5.7.0):

Input

new XMLParser({
  entityDecoder: {
    setExternalEntities: () => {},
    addInputEntities: () => {},
    reset: () => {},
    decode: (text: string) => text,
    setXmlVersion: (version: number) => { /* … */ },
  },
}).parse('<?xml version="1.1"?><root>text</root>');

Actual (5.11.1): TS2322: Type '(version: number) => void' is not assignable to type '(version: string) => void'. A decoder written to the declaration instead compiles, but at runtime receives 1.1 as a number, so a version === '1.1' check never matches.

Expected: the number-typed decoder compiles. @nodable/entities, the default decoder, handles the version as a number too (this._ncrXmlVersion = version === 1.1 ? 1.1 : 1.0).

The declaration, its CommonJS copy and the documented type now say number.

Type

  • Bug Fix
  • Refactoring / Technology upgrade
  • New Feature

Tests

spec/typings/typings-test.ts gains two cases: a decoder whose setXmlVersion takes a number compiles, and one taking a string does not (((version: string) => void) extends EntityDecoderOptions['setXmlVersion'] must be false). I compiled the test against four declarations:

setXmlVersion parameter npm run test-types
number (this PR) passes
string (5.11.1) TS2322 on both cases
any TS2322 on the negative case
string | number TS2322 on the negative case

Verification

npm run test-types passes · jasmine 332 specs, 0 failures, 2 pending (same as master) · eslint output identical to master (the diff has no .js) · git diff --check clean.

node benchmark/XmlParser.mjs not run: the diff changes declarations, the typings test and docs, so the parser executes the same code before and after.

No existing issue or PR covers this. I searched the tracker for setXmlVersion, EntityDecoderOptions and entityDecoder, and the only hits were #819, #825 and #829, about which entities are decoded by default.

Related

nodable/val-parsers#6 fixes the matching declaration in @nodable/entities, where EntityDecoder is declared as a default export with no setXmlVersion at all.

Base

Cut from bb2ec6c (master), merges cleanly.

OrderedObjParser calls `this.entityDecoder.setXmlVersion(Number(ver) || 1.0)`,
and the default decoder from @nodable/entities compares `version === 1.1`, but
EntityDecoderOptions declared the parameter a string. A custom decoder written
for the number it receives failed to compile, while one written to the
declaration compared the number against "1.1" and never matched.

The declaration, its CommonJS copy and the documented type now say number, and
the typings test pins both halves: a number-taking decoder compiles, and a
string-taking one does not.
@amitguptagwl
amitguptagwl merged commit 513d0b3 into NaturalIntelligence:master Sep 29, 2026
14 checks passed
@amitguptagwl

Copy link
Copy Markdown
Member

Thanks

renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Oct 12, 2026
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Oct 12, 2026
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Oct 12, 2026
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Oct 12, 2026
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Oct 12, 2026
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Oct 12, 2026
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.

2 participants