Skip to content

Commit 6509a5d

Browse files
committed
fix merge
1 parent bc88d51 commit 6509a5d

File tree

9 files changed

+45
-55
lines changed

9 files changed

+45
-55
lines changed

packages/cspell-grammar/src/mappers/appendMappedText.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ describe('appendMappedText', () => {
1818
});
1919

2020
function mt(text: string, map: number[] | undefined): MappedText {
21-
return { text, map };
21+
return { text, offsetMap: map };
2222
}

packages/cspell-grammar/src/mappers/appendMappedText.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
11
import assert from 'node:assert';
22

3+
import type { ParsedText } from '@cspell/cspell-types';
4+
35
import type { MappedText } from './types.js';
46

7+
export function appendParsedText(a: ParsedText, b: ParsedText): ParsedText {
8+
const adjacent = a.range[1] === b.range[0];
9+
10+
if (adjacent && !a.map && !b.map) {
11+
return {
12+
text: a.text + b.text,
13+
range: [a.range[0], b.range[1]],
14+
};
15+
}
16+
17+
const aMap = a.map || [a.range[1] - a.range[0], a.text.length];
18+
const bMap = b.map || [b.range[1] - b.range[0], b.text.length];
19+
20+
const inject = adjacent ? [] : [b.range[0] - a.range[1], 0];
21+
const map = [...aMap, ...inject, ...bMap];
22+
return {
23+
...a,
24+
...b,
25+
text: a.text + b.text,
26+
range: [a.range[0], b.range[1]],
27+
map,
28+
};
29+
}
30+
531
export function appendMappedText(a: MappedText, b: MappedText): MappedText {
6-
if (!a.map && !b.map) {
32+
if (!a.offsetMap && !b.offsetMap) {
733
return { text: a.text + b.text };
834
}
935
const aLen = a.text.length;
1036
const bLen = b.text.length;
11-
const aMap = [0, 0, ...(a.map || [0, 0, aLen, aLen])];
12-
const bMap = [0, 0, ...(b.map || [0, 0, bLen, bLen])];
37+
const aMap = [0, 0, ...(a.offsetMap || [0, 0, aLen, aLen])];
38+
const bMap = [0, 0, ...(b.offsetMap || [0, 0, bLen, bLen])];
1339

1440
assert(aMap[aMap.length - 1] === aLen);
1541
assert(bMap[bMap.length - 1] === bLen);
1642
assert((aMap.length & 1) === 0);
1743
assert((bMap.length & 1) === 0);
1844
return {
1945
text: a.text + b.text,
20-
map: joinMaps(aMap, bMap),
46+
offsetMap: joinMaps(aMap, bMap),
2147
};
2248
}
2349

packages/cspell-grammar/src/mappers/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ export interface MappedText {
1212
* where the `[0, 0]` is unnecessary.
1313
*
1414
*/
15-
map?: number[] | undefined;
15+
offsetMap?: number[] | undefined;
16+
17+
map?: symbol;
1618
}

packages/cspell-grammar/src/mappers/typescript.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('mappers typescript', () => {
6767
${'hello\\u{020}there'} | ${[5, 5, 12, 6, 17, 11]}
6868
`('mapRawString map $# [$text]', ({ text, expected }) => {
6969
const r = mapRawString(text);
70-
expect(r.map).toEqual(expected);
70+
expect(r.offsetMap).toEqual(expected);
7171
});
7272
});
7373

packages/cspell-grammar/src/mappers/typescript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,6 @@ export function mapRawString(text: string): MappedText {
147147

148148
return {
149149
text: t,
150-
map,
150+
offsetMap: map,
151151
};
152152
}

packages/cspell-grammar/src/parsers/typescript/TypeScriptParser.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { opFlatten, opMap, pipe } from '@cspell/cspell-pipe/sync';
44
import type { ParsedText, ParseResult, Scope, ScopeChain } from '@cspell/cspell-types/Parser';
55

66
import { grammar } from '../../grammars/typescript.js';
7-
import { appendMappedText } from '../../mappers/appendMappedText.js';
7+
import { appendParsedText } from '../../mappers/appendMappedText.js';
88
import { mapRawString } from '../../mappers/typescript.js';
99
import { compileGrammar } from '../../parser/grammar.js';
1010
import { createParser } from '../../parser/parser.js';
@@ -25,7 +25,7 @@ function* transform(texts: ParseResult['parsedTexts']): ParseResult['parsedTexts
2525
yield {
2626
text: mapped.text,
2727
scope: scope?.parent,
28-
map: absMapToRelMap(mapped.map),
28+
map: absMapToRelMap(mapped.offsetMap),
2929
range: parsed.range,
3030
};
3131
continue;
@@ -60,12 +60,10 @@ function* mergeStringResults(results: Iterable<ParsedText>): Iterable<ParsedText
6060
}
6161

6262
function mergeParsedText(a: ParsedText, b: ParsedText): ParsedText {
63-
const abT = appendMappedText(a, b);
63+
const abT = appendParsedText(a, b);
6464
const ab: ParsedText = {
65-
text: abT.text,
65+
...abT,
6666
scope: a.scope,
67-
range: [a.range[0], b.range[1]],
68-
map: absMapToRelMap(abT.map),
6967
delegate: a.delegate,
7068
};
7169

packages/cspell-lib/fixtures/docValidator/parser/sample.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ export async function loadFile(filename: string): Promise<string> {
1616

1717
export const messages = ['This is the first message.', 'This is the \x73econd message.'];
1818

19-
export const messagesWithErrors = ['This message has some \x73errors.'];
19+
export const messagesWithErrors = ['This message has some \x73errors. \u0073issues are bad.'];

packages/cspell-lib/src/lib/Transform/TextMap.test.ts

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { MappedText, SourceMap } from '@cspell/cspell-types';
22
import { describe, expect, test } from 'vitest';
33

4-
import { mergeSourceMaps } from './SourceMap.js';
54
import {
65
calRangeInSrc,
76
doesIntersect,
@@ -16,7 +15,7 @@ describe('TextMap', () => {
1615
${'fine café'} | ${[200, 209]} | ${undefined} | ${[205, 209]} | ${tm('café', [205, 209])}
1716
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[205, 212]} | ${tm('café', [205, 212], [3, 3, 4, 1])}
1817
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[200, 212]} | ${tm('fine café', [200, 212], [8, 8, 4, 1])}
19-
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[200, 213]} | ${tm('fine café ', [200, 213], [8, 8, 4, 1, 7, 7])}
18+
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[200, 213]} | ${tm('fine café ', [200, 213], [8, 8, 4, 1])}
2019
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1, 7, 7]} | ${[200, 219]} | ${tm('fine café coffee', [200, 219], [8, 8, 4, 1, 7, 7])}
2120
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1]} | ${[200, 219]} | ${tm('fine café coffee', [200, 219], [8, 8, 4, 1])}
2221
${'fine café coffee'} | ${[200, 219]} | ${[8, 8, 4, 1]} | ${[205, 212]} | ${tm('café', [205, 212], [3, 3, 4, 1])}
@@ -145,41 +144,6 @@ describe('TextMap', () => {
145144

146145
// cspell:dictionaries html-symbol-entities unicode-escapes
147146

148-
describe('mergeSourceMaps', () => {
149-
test('mergeSourceMaps', () => {
150-
/**
151-
* orig | len | intermediate | len | final | len |
152-
* ---- | --- | ------------ | --- | ----- | --- |
153-
* 0-3 | 3 | 0-3 | 3 | 0-3 | 3 |
154-
* 3-7 | 4 | 3-7 | 4 | 3-4 | 1 |
155-
* 7-8 | 1 | 7-8 | 1 | 4-5 | 1 |
156-
* 8-12 | 4 | 8-9 | 1 | 5-6 | 1 |
157-
* 12-19| 7 | 9-16 | 7 | 6-13 | 7 |
158-
* 19-22| 3 | 16-16 | 0 | 13-13 | 0 |
159-
* 22-24| 2 | 16-18 | 2 | 13-15 | 2 |
160-
*/
161-
const map1 = [8, 8, 4, 1, 7, 7, 3, 0, 2, 2];
162-
const map2 = [3, 3, 4, 1];
163-
const expected = [3, 3, 4, 1, 1, 1, 4, 1, 7, 7, 3, 0, 2, 2];
164-
const r = mergeSourceMaps(map1, map2);
165-
expect(r).toEqual(expected);
166-
});
167-
168-
test.each`
169-
map1 | map2 | expected
170-
${[]} | ${[]} | ${undefined}
171-
${[]} | ${undefined} | ${undefined}
172-
${undefined} | ${[]} | ${undefined}
173-
${undefined} | ${undefined} | ${undefined}
174-
${[1, 8]} | ${[]} | ${[1, 8]}
175-
${[]} | ${[0, 0, 3, 3]} | ${[0, 0, 3, 3]}
176-
${[0, 0, 8, 8]} | ${[0, 0, 3, 3]} | ${[0, 0, 3, 3, 5, 5]}
177-
`('mergeSourceMaps $map1, $map2', ({ map1, map2, expected }) => {
178-
const r = mergeSourceMaps(map1, map2);
179-
expect(r).toEqual(expected);
180-
});
181-
});
182-
183147
function tm(text: string, range: [number, number], map?: number[]): MappedText {
184148
return map ? { text, range, map } : { text, range };
185149
}

packages/cspell-lib/src/lib/textValidation/docValidator.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ describe('docValidator', () => {
137137
expect(dVal.prepTime).toBeGreaterThan(0);
138138
});
139139

140-
// cspell:ignore kount naame colector Reciever reciever recievers serrors dockblock
140+
// cspell:ignore kount naame colector Reciever reciever recievers serrors sissues
141141
test.each`
142142
filename | maxDuplicateProblems | expectedIssues | expectedRawIssues
143143
${fix('sample-with-errors.ts')} | ${undefined} | ${['dockblock', 'Helllo']} | ${undefined}
144144
${fix('sample-with-many-errors.ts')} | ${undefined} | ${['reciever', 'naame', 'naame', 'naame', 'reciever', 'Reciever', 'naame', 'Reciever', 'naame', 'kount', 'Reciever', 'kount', 'colector', 'recievers', 'Reciever', 'recievers', 'recievers']} | ${undefined}
145145
${fix('sample-with-many-errors.ts')} | ${1} | ${['reciever', 'naame', 'Reciever', 'kount', 'colector', 'recievers']} | ${undefined}
146-
${fix('parser/sample.ts')} | ${1} | ${['serrors']} | ${['\\x73errors']}
146+
${fix('parser/sample.ts')} | ${1} | ${['serrors', 'sissues']} | ${['\\x73errors', '\\u0073issues']}
147147
${fix('sample-with-directives-errors.ts')} | ${1} | ${['disable-prev', 'ignored', 'world', 'enable-line']} | ${undefined}
148148
${tFix('issues/issue-4811/#local/README.md')} | ${undefined} | ${[]} | ${undefined}
149149
${tFix('issues/issue-4811/#local/version@2.md')} | ${undefined} | ${['marrkdown']} | ${undefined /* cspell:disable-line */}
@@ -255,7 +255,7 @@ describe('docValidator', () => {
255255
${fix('sample-with-errors.ts')} | ${undefined} | ${['dockblock', 'Helllo']} | ${undefined}
256256
${fix('sample-with-many-errors.ts')} | ${undefined} | ${['reciever', 'naame', 'naame', 'naame', 'reciever', 'Reciever', 'naame', 'Reciever', 'naame', 'kount', 'Reciever', 'kount', 'colector', 'recievers', 'Reciever', 'recievers', 'recievers']} | ${undefined}
257257
${fix('sample-with-many-errors.ts')} | ${1} | ${['reciever', 'naame', 'Reciever', 'kount', 'colector', 'recievers']} | ${undefined}
258-
${fix('parser/sample.ts')} | ${1} | ${['serrors']} | ${['\\x73errors']}
258+
${fix('parser/sample.ts')} | ${1} | ${['serrors', 'sissues']} | ${['\\x73errors', '\\u0073issues']}
259259
${fix('sample-with-directives-errors.ts')} | ${1} | ${['disable-prev', 'ignored', 'world', 'enable-line']} | ${undefined}
260260
${fix('sample-with-local-words.md')} | ${undefined} | ${[]} | ${undefined}
261261
`(

0 commit comments

Comments
 (0)