Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ jobs:
node-version: ${{ matrix.node-version }}
architecture: ${{ steps.calculate_architecture.outputs.result }}
cache: "npm"
- run: npm install
if: matrix.node-version == '10.x' || matrix.node-version == '12.x' || matrix.node-version == '14.x'
- run: npm ci
if: matrix.node-version == '10.x' || matrix.node-version == '12.x' || matrix.node-version == '14.x' || matrix.node-version == '16.x'
if: matrix.node-version != '10.x' && matrix.node-version != '12.x' && matrix.node-version != '14.x'
- run: npm run cover
- uses: codecov/codecov-action@v5
with:
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All methods should be considered as expensive as they may need to do computation

#### `source`

<!-- eslint-skip -->
```typescript
Source.prototype.source() -> String | Buffer
```
Expand All @@ -20,6 +21,7 @@ Returns the represented source code as string or Buffer (for binary Sources).

#### `buffer`

<!-- eslint-skip -->
```typescript
Source.prototype.buffer() -> Buffer
```
Expand All @@ -28,6 +30,7 @@ Returns the represented source code as Buffer. Strings are converted to utf-8.

#### `size`

<!-- eslint-skip -->
```typescript
Source.prototype.size() -> Number
```
Expand All @@ -36,6 +39,7 @@ Returns the size in bytes of the represented source code.

#### `map`

<!-- eslint-skip -->
```typescript
Source.prototype.map(options?: Object) -> Object | null
```
Expand All @@ -48,6 +52,7 @@ The `options` object can contain the following keys:

#### `sourceAndMap`

<!-- eslint-skip -->
```typescript
Source.prototype.sourceAndMap(options?: Object) -> {
source: String | Buffer,
Expand All @@ -61,6 +66,7 @@ See `map()` for `options`.

#### `updateHash`

<!-- eslint-skip -->
```typescript
Source.prototype.updateHash(hash: Hash) -> void
```
Expand All @@ -71,6 +77,7 @@ Updates the provided `Hash` object with the content of the represented source co

Represents source code without SourceMap.

<!-- eslint-skip -->
```typescript
new RawSource(sourceCode: String | Buffer)
```
Expand All @@ -79,6 +86,7 @@ new RawSource(sourceCode: String | Buffer)

Represents source code, which is a copy of the original file.

<!-- eslint-skip -->
```typescript
new OriginalSource(
sourceCode: String | Buffer,
Expand All @@ -95,6 +103,7 @@ OriginalSource tries to create column mappings if requested, by splitting the so

Represents source code with SourceMap, optionally having an additional SourceMap for the original source.

<!-- eslint-skip -->
```typescript
new SourceMapSource(
sourceCode: String | Buffer,
Expand All @@ -121,6 +130,7 @@ When original source matches generated source for a mapping it's assumed to be m
Decorates a `Source` and caches returned results of `map`, `source`, `buffer`, `size` and `sourceAndMap` in memory. `updateHash` is not cached.
It tries to reused cached results from other methods to avoid calculations, i. e. when `source` is already cached, calling `size` will get the size from the cached source, calling `sourceAndMap` will only call `map` on the wrapped Source.

<!-- eslint-skip -->
```typescript
new CachedSource(source: Source)
new CachedSource(source: Source | () => Source, cachedData?: CachedData)
Expand All @@ -146,6 +156,7 @@ Returns the original `Source` object or a function returning these.

Prefix every line of the decorated `Source` with a provided string.

<!-- eslint-skip -->
```typescript
new PrefixSource(
prefix: String,
Expand All @@ -157,6 +168,7 @@ new PrefixSource(

Concatenate multiple `Source`s or strings to a single source.

<!-- eslint-skip -->
```typescript
new ConcatSource(
...items?: Source | String
Expand All @@ -167,6 +179,7 @@ new ConcatSource(

#### `add`

<!-- eslint-skip -->
```typescript
ConcatSource.prototype.add(item: Source | String)
```
Expand All @@ -184,6 +197,7 @@ When original source matches generated source for a mapping it's assumed to be m

#### `replace`

<!-- eslint-skip -->
```typescript
ReplaceSource.prototype.replace(
start: Number,
Expand All @@ -198,6 +212,7 @@ Locations represents locations in the original source and are not influenced by

#### `insert`

<!-- eslint-skip -->
```typescript
ReplaceSource.prototype.insert(
pos: Number,
Expand All @@ -221,6 +236,7 @@ Converts a Source-like object into a real Source object.

#### static `from`

<!-- eslint-skip -->
```typescript
CompatSource.from(sourceLike: any | Source)
```
Expand Down
88 changes: 44 additions & 44 deletions lib/helpers/getFromStreamChunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ const createMappingsSerializer = require("./createMappingsSerializer");
/** @typedef {{ streamChunks: StreamChunksFunction }} SourceLikeWithStreamChunks */

/**
* @param {SourceLikeWithStreamChunks} inputSource input source
* @param {SourceLikeWithStreamChunks} source source
* @param {Options=} options options
* @returns {SourceAndMap} map
* @returns {RawSourceMap | null} map
*/
module.exports.getSourceAndMap = (inputSource, options) => {
let code = "";
module.exports.getMap = (source, options) => {
let mappings = "";
/** @type {(string | null)[]} */
const potentialSources = [];
Expand All @@ -29,8 +28,8 @@ module.exports.getSourceAndMap = (inputSource, options) => {
/** @type {(string | null)[]} */
const potentialNames = [];
const addMapping = createMappingsSerializer(options);
const { source } = inputSource.streamChunks(
{ ...options, finalSource: true },
source.streamChunks(
{ ...options, source: false, finalSource: true },
(
chunk,
generatedLine,
Expand All @@ -40,7 +39,6 @@ module.exports.getSourceAndMap = (inputSource, options) => {
originalColumn,
nameIndex,
) => {
if (chunk !== undefined) code += chunk;
mappings += addMapping(
generatedLine,
generatedColumn,
Expand Down Expand Up @@ -69,32 +67,29 @@ module.exports.getSourceAndMap = (inputSource, options) => {
potentialNames[nameIndex] = name;
},
);
return {
source: source !== undefined ? source : code,
map:
mappings.length > 0
? {
version: 3,
file: "x",
mappings,
// We handle broken sources as `null`, in spec this field should be string, but no information what we should do in such cases if we change type it will be breaking change
sources: /** @type {string[]} */ (potentialSources),
sourcesContent:
potentialSourcesContent.length > 0
? /** @type {string[]} */ (potentialSourcesContent)
: undefined,
names: /** @type {string[]} */ (potentialNames),
}
: null,
};
return mappings.length > 0
? {
version: 3,
file: "x",
mappings,
// We handle broken sources as `null`, in spec this field should be string, but no information what we should do in such cases if we change type it will be breaking change
sources: /** @type {string[]} */ (potentialSources),
sourcesContent:
potentialSourcesContent.length > 0
? /** @type {string[]} */ (potentialSourcesContent)
: undefined,
names: /** @type {string[]} */ (potentialNames),
}
: null;
};

/**
* @param {SourceLikeWithStreamChunks} source source
* @param {SourceLikeWithStreamChunks} inputSource input source
* @param {Options=} options options
* @returns {RawSourceMap | null} map
* @returns {SourceAndMap} map
*/
module.exports.getMap = (source, options) => {
module.exports.getSourceAndMap = (inputSource, options) => {
let code = "";
let mappings = "";
/** @type {(string | null)[]} */
const potentialSources = [];
Expand All @@ -103,8 +98,8 @@ module.exports.getMap = (source, options) => {
/** @type {(string | null)[]} */
const potentialNames = [];
const addMapping = createMappingsSerializer(options);
source.streamChunks(
{ ...options, source: false, finalSource: true },
const { source } = inputSource.streamChunks(
{ ...options, finalSource: true },
(
chunk,
generatedLine,
Expand All @@ -114,6 +109,7 @@ module.exports.getMap = (source, options) => {
originalColumn,
nameIndex,
) => {
if (chunk !== undefined) code += chunk;
mappings += addMapping(
generatedLine,
generatedColumn,
Expand Down Expand Up @@ -142,18 +138,22 @@ module.exports.getMap = (source, options) => {
potentialNames[nameIndex] = name;
},
);
return mappings.length > 0
? {
version: 3,
file: "x",
mappings,
// We handle broken sources as `null`, in spec this field should be string, but no information what we should do in such cases if we change type it will be breaking change
sources: /** @type {string[]} */ (potentialSources),
sourcesContent:
potentialSourcesContent.length > 0
? /** @type {string[]} */ (potentialSourcesContent)
: undefined,
names: /** @type {string[]} */ (potentialNames),
}
: null;
return {
source: source !== undefined ? source : code,
map:
mappings.length > 0
? {
version: 3,
file: "x",
mappings,
// We handle broken sources as `null`, in spec this field should be string, but no information what we should do in such cases if we change type it will be breaking change
sources: /** @type {string[]} */ (potentialSources),
sourcesContent:
potentialSourcesContent.length > 0
? /** @type {string[]} */ (potentialSourcesContent)
: undefined,
names: /** @type {string[]} */ (potentialNames),
}
: null,
};
};
4 changes: 2 additions & 2 deletions lib/helpers/stringBufferUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ function internString(str) {
module.exports = {
disableDualStringBufferCaching,
enableDualStringBufferCaching,
internString,
isDualStringBufferCachingEnabled,
enterStringInterningRange,
exitStringInterningRange,
internString,
isDualStringBufferCachingEnabled,
};
Loading