Skip to content

Fix typings that don't match runtime, without breaking changes - #117

Merged
7rulnik merged 2 commits into
mainfrom
fix/typings
Nov 19, 2026
Merged

7rulnik merged 2 commits into
mainfrom
fix/typings

Conversation

@7rulnik

@7rulnik 7rulnik commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Part of #111. Fixes every mismatch between source-map.d.ts and lib/ that can be fixed without breaking anyone's build. Only source-map.d.ts changes.

Fixed

SourceMapConsumer

  • The constructor takes a new RawSourceMapInput: version can be a number, sourcesContent can have null entries, and names can be left out (Sass does that). So new SourceMapConsumer(JSON.parse(map)) with "version": 3 compiles now, which is what Bug: RawSourceMap.version type should be number, not string #43 is about. It also covers the input half of Change sourcesContent type to allow null values #75.
  • The constructor also accepts index maps (new RawIndexMap and RawSection), JSON strings, and the second sourceMapUrl argument.
  • SourceMapConsumer.fromSourceMap is declared.
  • allGeneratedPositionsFor takes an optional column, as the README documents, and its results include lastColumn?: number | null. It's optional so that callers can still put plain { line, column } positions into the array.

SourceMapGenerator

  • skipValidation and ignoreInvalidMapping are typed (new SourceMapGeneratorOptions), for the constructor, fromSourceMap and SourceNode#toStringWithSourceMap.

SourceNode

  • The constructor accepts nulls and chunk arrays. The README's own new SourceNode(null, null, null, [a, b, c]) didn't compile.
  • add and prepend accept nested arrays and SourceNodes.
  • replaceRight accepts a RegExp (the README example uses /\s*$/) or a replacer function.

Not fixed: needs a breaking change

Tracked in #118 for the next major. These are return types. Making them accurate breaks code that uses the values as they're typed now. Each one has a comment saying what happens at runtime.

Testing

Checked outside the repo, nothing added to it:

  • A file that uses every old signature, with return types pinned by annotations, compiles against the old and the new typings on TypeScript 5.9 and 3.4.
  • So do callers that write into the allGeneratedPositionsFor result (push, concat, indexOf, unshift, splice), wrap add/prepend in void-returning callbacks, override methods with the old return types, or mix in a SourceNode or SourceMapConsumer typed by another copy of the package (the old typings, which are also what source-map 0.6 ships).
  • A file with the new usages is rejected by the old typings (26 errors), compiles against the new ones, and runs against lib/ with assertions, including the runtime behavior the new comments describe.
  • The typings of postcss 8.5.33, sass 1.108.2 and @vue/compiler-sfc 3.5.45 compile against both with skipLibCheck: false.
  • pnpm format:check passes, the tests pass.

SourceMapConsumer:
- The constructor accepts a numeric version, null sourcesContent entries
  and a missing names array (new RawSourceMapInput), index maps
  (RawIndexMap, RawSection), JSON strings, and the sourceMapUrl argument.
- Declare the static fromSourceMap.
- allGeneratedPositionsFor takes an optional column and returns lastColumn.

SourceMapGenerator: type the skipValidation and ignoreInvalidMapping
options (SourceMapGeneratorOptions), also for fromSourceMap and
SourceNode#toStringWithSourceMap.

SourceNode: the constructor accepts nulls and chunk arrays, add and
prepend accept nested arrays and SourceNodes and return the node,
replaceRight accepts a RegExp or a replacer function, and children, line,
column, source and name are declared.

Return types that are wrong only in being non-nullable, or a string
version, stay as they are: fixing them breaks callers. A comment on each
says what happens at runtime.

Part of #111
Refs #43
Refs #30
Refs #75
… callers

A required lastColumn broke writing plain positions into the result array (push, concat, indexOf, unshift, splice, reassigning). Make it optional.

Returning the node from add and prepend broke void-returning callbacks like (chunk: string): void => node.add(chunk). Keep void and note the runtime return value instead.

The new SourceNode members (children, line, column, source, name) made a SourceNode typed by another copy of the package, such as an older source-map-js or source-map 0.6, unassignable to this one. Drop them.
@7rulnik
7rulnik marked this pull request as ready for review November 19, 2026 12:02
@7rulnik
7rulnik merged commit 338d7ad into main Nov 19, 2026
6 checks passed
@7rulnik
7rulnik deleted the fix/typings branch November 19, 2026 12:02
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