Skip to content

Commit 5c779b1

Browse files
authored
Allow singleline string writer to be recursively used (#18297)
* Allow singleline string writer to be recursively used * Add unit test exposing issue * Fix lints
1 parent 697c4d3 commit 5c779b1

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

Jakefile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ var harnessSources = harnessCoreSources.concat([
143143
"customTransforms.ts",
144144
"programMissingFiles.ts",
145145
"symbolWalker.ts",
146+
"languageService.ts",
146147
].map(function (f) {
147148
return path.join(unittestsDirectory, f);
148149
})).concat([

src/compiler/utilities.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ namespace ts {
3232
}
3333

3434
const stringWriter = createSingleLineStringWriter();
35-
let stringWriterAcquired = false;
3635

3736
function createSingleLineStringWriter(): StringSymbolWriter {
3837
let str = "";
@@ -62,15 +61,14 @@ namespace ts {
6261
}
6362

6463
export function usingSingleLineStringWriter(action: (writer: StringSymbolWriter) => void): string {
64+
const oldString = stringWriter.string();
6565
try {
66-
Debug.assert(!stringWriterAcquired);
67-
stringWriterAcquired = true;
6866
action(stringWriter);
6967
return stringWriter.string();
7068
}
7169
finally {
7270
stringWriter.clear();
73-
stringWriterAcquired = false;
71+
stringWriter.writeKeyword(oldString);
7472
}
7573
}
7674

src/harness/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
"./unittests/extractMethods.ts",
129129
"./unittests/textChanges.ts",
130130
"./unittests/telemetry.ts",
131+
"./unittests/languageService.ts",
131132
"./unittests/programMissingFiles.ts"
132133
]
133134
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/// <reference path="..\harness.ts" />
2+
3+
namespace ts {
4+
describe("languageService", () => {
5+
const files: {[index: string]: string} = {
6+
"foo.ts": `import Vue from "./vue";
7+
import Component from "./vue-class-component";
8+
import { vueTemplateHtml } from "./variables";
9+
10+
@Component({
11+
template: vueTemplateHtml,
12+
})
13+
class Carousel<T> extends Vue {
14+
}`,
15+
"variables.ts": `export const vueTemplateHtml = \`<div></div>\`;`,
16+
"vue.d.ts": `export namespace Vue { export type Config = { template: string }; }`,
17+
"vue-class-component.d.ts": `import Vue from "./vue";
18+
export function Component(x: Config): any;`
19+
};
20+
it("should be able to create a language service which can respond to deinition requests without throwing", () => {
21+
const languageService = ts.createLanguageService({
22+
getCompilationSettings() {
23+
return {};
24+
},
25+
getScriptFileNames() {
26+
return ["foo.ts", "variables.ts", "vue.d.ts", "vue-class-component.d.ts"];
27+
},
28+
getScriptVersion(_fileName) {
29+
return "";
30+
},
31+
getScriptSnapshot(fileName) {
32+
if (fileName === ".ts") {
33+
return ts.ScriptSnapshot.fromString("");
34+
}
35+
return ts.ScriptSnapshot.fromString(files[fileName] || "");
36+
},
37+
getCurrentDirectory: () => ".",
38+
getDefaultLibFileName(options) {
39+
return ts.getDefaultLibFilePath(options);
40+
},
41+
fileExists: noop as any,
42+
readFile: noop as any,
43+
readDirectory: noop as any,
44+
});
45+
const definitions = languageService.getDefinitionAtPosition("foo.ts", 160); // 160 is the latter `vueTemplateHtml` position
46+
expect(definitions).to.exist;
47+
});
48+
});
49+
}

0 commit comments

Comments
 (0)