Skip to content

Typings of sourcesContent does not match implementation #234

@AriPerkkio

Description

@AriPerkkio

Typings of the sourcesContent is string[]:

sourcesContent: string[];

sourcesContent: string[];

The implementation may return { sourcesContent: [null] } when includeContent = false (default value) is used:

sourcesContent: options.includeContent ? [this.original] : [null],


Minimal reproduction

import MagicString from "magic-string";

const s = new MagicString('Hello world');

const map = s.generateMap({});
const [firstSourcesContent] = map.sourcesContent;

console.log(firstSourcesContent.charAt(0));
//          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// TypeError: Cannot read properties of null (reading 'charAt')
//     at <anonymous> (/x/y/repro/index.ts:14:33)
// This should not raise tsc errors
map.sourcesContent[0] = null
// ^^^^^^^^^^^^^^^^^^
// Type 'null' is not assignable to type 'string'.ts(2322)

Based on the standard the implementation works correctly:

Proposed Format

[...]
6. "sourcesContent": [null, null],
[...]
Line 6: An optional list of source content, useful when the “source” can’t be hosted. The contents are listed in the same order as the sources in line 5. “null” may be used if some original sources should be retrieved by name.


Related downstream bug:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions