Skip to content

Commit

Permalink
Fix bug with memo after insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Apr 15, 2022
1 parent c65976a commit dd3d0ff
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
original location in the source file through a source map.

You may already be familiar with the [`source-map`][source-map] package's `SourceMapConsumer`. This
provides the same `originalPositionFor` API, without requires WASM.
provides the same `originalPositionFor` and `generatedPositionFor` API, without requires WASM.

## Installation

Expand All @@ -17,7 +17,7 @@ npm install @jridgewell/trace-mapping
## Usage

```typescript
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping';
import { TraceMap, originalPositionFor, generatedPositionFor } from '@jridgewell/trace-mapping';

const tracer = new TraceMap({
version: 3,
Expand All @@ -34,6 +34,12 @@ assert.deepEqual(traced, {
column: 4,
name: 'foo',
});

const generated = generatedPositionFor(tracer, { source: 'input.js', line: 42, column: 4 });
assert.deepEqual(generated, {
line: 1,
column: 5,
});
```

We also provide a lower level API to get the actual segment that matches our line and column. Unlike
Expand Down
2 changes: 1 addition & 1 deletion src/by-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function buildBySources(
memoizedBinarySearch(originalLine, sourceColumn, memo, sourceLine),
);

insert(originalLine, index + 1, [sourceColumn, i, seg[COLUMN]]);
insert(originalLine, (memo.lastIndex = index + 1), [sourceColumn, i, seg[COLUMN]]);
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/trace-mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ describe('TraceMap', () => {
test('generatedPositionFor', (t) => {
const tracer = new TraceMap(map);

t.deepEqual(generatedPositionFor(tracer, { source: 'input.js', line: 4, column: 3 }), {
line: 5,
column: 3,
});

t.deepEqual(generatedPositionFor(tracer, { source: 'input.js', line: 1, column: 0 }), {
line: 1,
column: 0,
Expand Down

0 comments on commit dd3d0ff

Please sign in to comment.