diff --git a/package-lock.json b/package-lock.json index dd23690..0e653d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "2.1.2", "license": "Apache-2.0", "dependencies": { + "@jridgewell/set-array": "1.0.0", "@jridgewell/trace-mapping": "^0.3.0" }, "devDependencies": { @@ -1026,6 +1027,14 @@ "node": ">=6.0.0" } }, + "node_modules/@jridgewell/set-array": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@jridgewell/set-array/-/set-array-1.0.0.tgz", + "integrity": "sha512-LcqVnHCjOAj8BTCtjpwYZCMTn4yArusbdObCVRUYvBHhrR5fVLVyENG+UVWM4T4H/ufv7NiBLdprllxWs/5PaQ==", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/@jridgewell/sourcemap-codec": { "version": "1.4.10", "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.10.tgz", @@ -6790,6 +6799,11 @@ "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.0.4.tgz", "integrity": "sha512-cz8HFjOFfUBtvN+NXYSFMHYRdxZMaEl0XypVrhzxBgadKIXhIkRd8aMeHhmF56Sl7SuS8OnUpQ73/k9LE4VnLg==" }, + "@jridgewell/set-array": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@jridgewell/set-array/-/set-array-1.0.0.tgz", + "integrity": "sha512-LcqVnHCjOAj8BTCtjpwYZCMTn4yArusbdObCVRUYvBHhrR5fVLVyENG+UVWM4T4H/ufv7NiBLdprllxWs/5PaQ==" + }, "@jridgewell/sourcemap-codec": { "version": "1.4.10", "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.10.tgz", diff --git a/package.json b/package.json index 0914a04..e51667b 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "typescript": "4.5.5" }, "dependencies": { + "@jridgewell/set-array": "^1.0.0", "@jridgewell/trace-mapping": "^0.3.0" } } diff --git a/src/fast-string-array.ts b/src/fast-string-array.ts deleted file mode 100644 index 04bf285..0000000 --- a/src/fast-string-array.ts +++ /dev/null @@ -1,38 +0,0 @@ -export interface FastStringArray { - indexes: { [key: string]: number }; - array: readonly string[]; -} - -/** - * FastStringArray acts like a `Set` (allowing only one occurrence of a string - * `key`), but provides the index of the `key` in the backing array. - * - * This is designed to allow synchronizing a second array with the contents of - * the backing array, like how `sourcesContent[i]` is the source content - * associated with `source[i]`, and there are never duplicates. - */ -export function FastStringArray(): FastStringArray { - return { - indexes: { __proto__: null } as any, - array: [], - }; -} - -/** - * Puts `key` into the backing array, if it is not already present. Returns - * the index of the `key` in the backing array. - */ -export function put(strarr: FastStringArray, key: string): number { - const { array, indexes } = strarr; - // The key may or may not be present. If it is present, it's a number. - let index = indexes[key] as number | undefined; - - // If it's not yet present, we need to insert it and track the index in the - // indexes. - if (index === undefined) { - index = indexes[key] = array.length; - (array as string[]).push(key); - } - - return index; -} diff --git a/src/source-map-tree.ts b/src/source-map-tree.ts index c815ade..d029976 100644 --- a/src/source-map-tree.ts +++ b/src/source-map-tree.ts @@ -1,4 +1,4 @@ -import { FastStringArray, put } from './fast-string-array'; +import { SetArray, put } from '@jridgewell/set-array'; import { presortedDecodedMap, traceSegment, decodedMappings } from '@jridgewell/trace-mapping'; import type { TraceMap } from '@jridgewell/trace-mapping'; @@ -62,8 +62,8 @@ export function OriginalSource(source: string, content: string | null): Original */ export function traceMappings(tree: Sources): TraceMap { const mappings: SourceMapSegment[][] = []; - const names = FastStringArray(); - const sources = FastStringArray(); + const names = new SetArray(); + const sources = new SetArray(); const sourcesContent: (string | null)[] = []; const { sources: rootSources, map } = tree; const rootNames = map.names; diff --git a/test/unit/fast-array-string.ts b/test/unit/fast-array-string.ts deleted file mode 100644 index a7fce35..0000000 --- a/test/unit/fast-array-string.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { FastStringArray, put } from '../../src/fast-string-array'; - -describe('FastStringArray', () => { - describe('put()', () => { - test('puts string in if not present', () => { - const array = FastStringArray(); - - put(array, 'test'); - expect(array.array).toEqual(['test']); - put(array, 'test'); - expect(array.array).toEqual(['test']); - - put(array, 'foo'); - expect(array.array).toEqual(['test', 'foo']); - put(array, 'bar'); - expect(array.array).toEqual(['test', 'foo', 'bar']); - - put(array, 'bar'); - expect(array.array).toEqual(['test', 'foo', 'bar']); - put(array, 'foo'); - expect(array.array).toEqual(['test', 'foo', 'bar']); - }); - - test('returns index of string in array', () => { - const array = FastStringArray(); - - expect(put(array, 'test')).toBe(0); - expect(put(array, 'foo')).toBe(1); - expect(put(array, 'bar')).toBe(2); - }); - - test('returns original index of string in array', () => { - const array = FastStringArray(); - - put(array, 'test'); - put(array, 'foo'); - put(array, 'bar'); - - expect(put(array, 'test')).toBe(0); - expect(put(array, 'foo')).toBe(1); - expect(put(array, 'bar')).toBe(2); - }); - - test('handles empty string', () => { - const array = FastStringArray(); - - expect(put(array, '')).toBe(0); - expect(put(array, '')).toBe(0); - expect(array.array).toEqual(['']); - }); - }); -});