Skip to content

fix: typing, build and packaging issues shared with project template - #283

Merged
char0n merged 11 commits into
mainfrom
fix/issue-269
Nov 26, 2026
Merged

char0n merged 11 commits into
mainfrom
fix/issue-269

Conversation

@char0n

@char0n char0n commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Fixes all items of #269, one commit per item, following the fixes in swaggerexpert/media-types.

# Item Commit
1 CommonJS typings (TS1479), incl. ./evaluate/realms/json and ./evaluate/realms/map-set as reported in the comment fix(types): provide CommonJS declarations for all entry points
2 new Grammar() (TS7009) fix(types): declare Grammar as class with construct signature
3 Unsound type predicates of testJSONPointer / testReferenceToken fix(types): return boolean from JSON Pointer and token test functions
4 Error names mangled in minified bundle fix(errors): keep error names in minified browser bundle
5 UMD bundles not loadable via require fix(build): make UMD bundles loadable via require
6 Fragile apg-lite path rewriting fix(build): resolve apg-lite path independently of cwd and platform
7 Trace.inferExpectations() wrong expectations fix(parse): infer expectations at furthest offset only
8 XMLTranslator malformed XML test(parse): cover escaping of XML special characters
9 test:watch ignores nested files chore(scripts): watch nested files in test:watch
10 NOTICE lacks apg-lite license docs(NOTICE): add apg-lite copyright and license
– CI: artifacts, typings, installed tarball on Node.js 12.20.0–26 ci: test built artifacts, typings and installed tarball

Differences from the media-types fixes

  • 1: scripts/build-types.cjs generates a .d.cts for every .d.ts (not only index.d.ts), rewriting relative .js imports to .cjs, so the realm declarations reference the CommonJS index declarations. @arethetypeswrong/cli on the packed tarball: 🟢 for node16 (CJS and ESM) and bundler on all entry points (was 👺 Masquerading as ESM).
  • 3: testArrayLocation, testArrayIndex and testArrayDash keep their predicates, as they narrow to literal types and are sound.
  • 7: no sentinel re-parse. The JSON Pointer grammar matches every code point (unescaped includes %x00-2E), so the \u0000 sentinel would be consumed. It is also not needed: every failing JSON Pointer ends with a failed terminal at the furthest offset (/ at start, 0/1 after ~). parse('/a~') now expects "0", "1" (was "0", "1", "/").
  • 8: already fixed by chore(deps): update dependencies #282: apg-lite 1.0.7+ escapes & < > " ' in toXml() (and replaces characters invalid in XML with U+FFFD, as its caveat comment says). This PR only adds a regression test.

Review follow-ups

  • fix(parse): infer no expectations after successful parse: parse('/a', { trace: true }).trace.inferExpectations() returned "\x00-." (regression of item 7, main returned []); " / \ are escaped and code points above 0xFF use \u{...}
  • fix(build): rewrite all relative specifiers in CommonJS declarations: also import() types, require(), side-effect imports; the build fails on extensionless relative specifiers and removes stale .d.cts
  • docs(NOTICE): mention apg-lite bundled in UMD browser bundles
  • test(artifacts): require UMD bundles on Node.js >= 14.6 only: bundles use private class methods
  • ci: verify tarball contains generated declarations: the .d.cts files and dist/package.json are gitignored

Not addressed: node10 resolution of the realm subpaths (pre-existing, same in 3.0.2) and dist/ not being in exports (bundles are meant for CDNs / loading by path).

Verification

  • npm test: 310 passing
  • npm run test:artifacts: CommonJS, ES module, realms, UMD via require and browser global; fails before the fix with ReferenceError: self is not defined
  • npm run test:types: tsc --strict for .cts / .mts consumers (node16) and bundler; reproduces TS1479 / TS7009 / never-narrowing before the fixes
  • Packed tarball smoke-tested locally on Node.js 12.20.0, 14, 16, 18, 24, 26

Closes #269

Comment thread test/parse/translators/XMLTranslator.js Dismissed
The require export condition reused ES module declarations, so CommonJS
consumers with node16/nodenext module resolution failed with TS1479.
Declarations are now copied to .d.cts at build time, with relative .js
specifiers rewritten to .cjs, and referenced via separate types under
import and require conditions.

Refs #269
Grammar was declared as function without construct signature, so
`new Grammar()` (as documented) failed with TS7009 under --strict.

Refs #269
testJSONPointer and testReferenceToken used type predicates targeting
aliases of string, which narrowed invalid input to never in the false
branch. testArrayLocation, testArrayIndex and testArrayDash keep their
predicates, as they narrow to literal types.

Refs #269
Error classes derived their names from this.constructor.name, which
Terser mangles in dist/*.min.js (e.g. JSONPointerKeyError became Z).
Names are now defined explicitly on the error classes.

Refs #269
webpack's default globalObject for web target is self, which is not
defined in Node.js, so requiring the bundles threw ReferenceError.
The bundles now use self or this as global object.

Adds smoke test of built artifacts (npm run test:artifacts).

Refs #269
The CommonJS build rewrote apg-lite imports relative to the current
working directory and used raw path.relative() output, producing wrong
paths when run from another directory, backslashes on Windows and bare
specifiers for files directly under src/.

Refs #269
inferExpectations collected failed terminals from offsets earlier than
the furthest one and considered TLS terminals only, so e.g. "/a~"
reported "0", "1", "/". Failed TLS, TBS and TRG terminals are now
recorded with their offsets and only those at the furthest offset are
reported, or none when the parser matched past them.

Refs #269
XMLTranslator produced malformed XML for JSON Pointers containing < or &
because apg-lite's toXml() did not escape texts. apg-lite fixed this in
1.0.7, and the minimum required version is ^1.0.8 since #282.

Refs #269
The npm-watch pattern {src,test}/*.js matched top-level files only, so
editing e.g. src/parse/index.js did not re-run tests.

Refs #269
The CommonJS build bundles apg-lite (cjs/apg-lite.cjs) and src/grammar.js
is generated by apg-js. The BSD-2-Clause license requires reproducing the
copyright notice and license in binary distributions.

Refs #269
Runs the artifact smoke test and type checks after the build, verifies
the tarball contains the generated declarations, and installs it into
a new project on every Node.js version from engines up.

Refs #269
@char0n char0n closed this Nov 26, 2026
@char0n
char0n deleted the fix/issue-269 branch November 26, 2026 10:09
@char0n
char0n restored the fix/issue-269 branch November 26, 2026 10:09
@char0n char0n reopened this Nov 26, 2026
@char0n
char0n merged commit 7ad0d42 into main Nov 26, 2026
15 checks passed
@char0n
char0n deleted the fix/issue-269 branch November 26, 2026 10:10
@char0n

char0n commented Nov 26, 2026

Copy link
Copy Markdown
Member Author

🎉 This PR is included in version 3.0.3 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Typing, build and packaging issues shared with project template

2 participants