Skip to content

Commit a73a553

Browse files
committed
Assert that Extract* baselines are syntactically valid
1 parent 0d5d5cd commit a73a553

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/harness/unittests/extractTestHelpers.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,16 @@ namespace ts {
108108
it(`${caption} [${extension}]`, () => runBaseline(extension)));
109109

110110
function runBaseline(extension: Extension) {
111-
const f = {
112-
path: "/a" + extension,
113-
content: t.source
114-
};
115-
const host = projectSystem.createServerHost([f, projectSystem.libFile]);
116-
const projectService = projectSystem.createProjectService(host);
117-
projectService.openClientFile(f.path);
118-
const program = projectService.inferredProjects[0].getLanguageService().getProgram();
111+
const path = "/a" + extension;
112+
const program = makeProgram({ path, content: t.source });
119113

120-
// Don't bother generating JS baselines for inputs that aren't valid JS.
121-
const diags = program.getSyntacticDiagnostics();
122-
if (diags && diags.length) {
114+
if (hasSyntacticDiagnostics(program)) {
115+
// Don't bother generating JS baselines for inputs that aren't valid JS.
123116
assert.equal(Extension.Js, extension);
124117
return;
125118
}
126119

127-
const sourceFile = program.getSourceFile(f.path);
120+
const sourceFile = program.getSourceFile(path);
128121
const context: RefactorContext = {
129122
cancellationToken: { throwIfCancellationRequested() { }, isCancellationRequested() { return false; } },
130123
newLineCharacter,
@@ -150,10 +143,26 @@ namespace ts {
150143
const newText = textChanges.applyChanges(sourceFile.text, edits[0].textChanges);
151144
const newTextWithRename = newText.slice(0, renameLocation) + "/*RENAME*/" + newText.slice(renameLocation);
152145
data.push(newTextWithRename);
146+
147+
const diagProgram = makeProgram({ path, content: newText });
148+
assert.isFalse(hasSyntacticDiagnostics(diagProgram));
153149
}
154150
return data.join(newLineCharacter);
155151
});
156152
}
153+
154+
function makeProgram(f: {path: string, content: string }) {
155+
const host = projectSystem.createServerHost([f, projectSystem.libFile]);
156+
const projectService = projectSystem.createProjectService(host);
157+
projectService.openClientFile(f.path);
158+
const program = projectService.inferredProjects[0].getLanguageService().getProgram();
159+
return program;
160+
}
161+
162+
function hasSyntacticDiagnostics(program: Program) {
163+
const diags = program.getSyntacticDiagnostics();
164+
return length(diags) > 0;
165+
}
157166
}
158167

159168
export function testExtractSymbolFailed(caption: string, text: string, description: DiagnosticMessage) {

0 commit comments

Comments
 (0)