Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 87636be

Browse files
committed
Use a tagged template string
1 parent e098cb7 commit 87636be

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/test/util-test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import * as assert from 'assert';
2-
import { isGlobalTSFile, isSymbolDescriptorMatch } from '../util';
2+
import { isGlobalTSFile, isSymbolDescriptorMatch, JSONPTR } from '../util';
33

44
describe('util', () => {
5+
describe('JSONPTR', () => {
6+
it('should escape JSON Pointer components', () => {
7+
const uri = 'file:///foo/~bar';
8+
const pointer = JSONPTR`/changes/${uri}/-`;
9+
assert.equal(pointer, '/changes/file:~1~1~1foo~1~0bar/-');
10+
});
11+
});
512
describe('isSymbolDescriptorMatch()', () => {
613
it('should return true for a matching query', () => {
714
const matches = isSymbolDescriptorMatch({

src/typescript-service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ import {
4949
import {
5050
convertStringtoSymbolKind,
5151
defInfoToSymbolDescriptor,
52-
encodeJsonPointerComponent,
5352
isLocalUri,
5453
isSymbolDescriptorMatch,
54+
JSONPTR,
5555
normalizeUri,
5656
path2uri,
5757
toUnixPath,
@@ -1055,10 +1055,10 @@ export class TypeScriptService {
10551055
// if file has no edit yet, initialize array
10561056
if (!editUris.has(uri)) {
10571057
editUris.add(uri);
1058-
return { op: 'add', path: `/changes/${encodeJsonPointerComponent(uri)}`, value: [edit] };
1058+
return { op: 'add', path: JSONPTR`/changes/${uri}`, value: [edit] };
10591059
}
10601060
// else append to array
1061-
return { op: 'add', path: `/changes/${encodeJsonPointerComponent(uri)}/-`, value: edit };
1061+
return { op: 'add', path: JSONPTR`/changes/${uri}/-`, value: edit };
10621062
})
10631063
.startWith({ op: 'add', path: '', value: { changes: {} } as WorkspaceEdit } as AddPatch);
10641064
}

src/util.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { PackageDescriptor, SymbolDescriptor } from './request-type';
88
let strict = false;
99

1010
/**
11-
* Encodes a JSON Pointer Component as per https://tools.ietf.org/html/rfc6901#section-3
11+
* Template string tag to escape JSON Pointer components as per https://tools.ietf.org/html/rfc6901#section-3
1212
*/
13-
export function encodeJsonPointerComponent(component: string): string {
14-
return component.replace(/~/g, '~0').replace(/\//g, '~1');
13+
export function JSONPTR(strings: TemplateStringsArray, ...toEscape: string[]): string {
14+
return strings.reduce((prev, curr, i) => prev + toEscape[i - 1].replace(/~/g, '~0').replace(/\//g, '~1') + curr);
1515
}
1616

1717
/**

0 commit comments

Comments
 (0)