test(language-service): remove circular dependency (#40966)

This commit removes the circular dependency from buffer->util->project->buffer.

PR Close #40966
This commit is contained in:
Andrew Scott 2021-02-23 13:22:37 -08:00 committed by atscott
parent cf687fe8ab
commit d1535a1a77
4 changed files with 27 additions and 31 deletions

View File

@ -9,7 +9,7 @@
import {initMockFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing'; import {initMockFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
import * as ts from 'typescript/lib/tsserverlibrary'; import * as ts from 'typescript/lib/tsserverlibrary';
import {extractCursorInfo, LanguageServiceTestEnv, Project} from '../testing'; import {LanguageServiceTestEnv, Project} from '../testing';
function quickInfoSkeleton(): {[fileName: string]: string} { function quickInfoSkeleton(): {[fileName: string]: string} {
return { return {
@ -516,7 +516,7 @@ describe('quick info', () => {
{templateOverride, expectedSpanText, expectedDisplayString}: {templateOverride, expectedSpanText, expectedDisplayString}:
{templateOverride: string, expectedSpanText: string, expectedDisplayString: string}): {templateOverride: string, expectedSpanText: string, expectedDisplayString: string}):
ts.QuickInfo { ts.QuickInfo {
const {text} = extractCursorInfo(templateOverride); const text = templateOverride.replace('¦', '');
const template = project.openFile('app.html'); const template = project.openFile('app.html');
template.contents = text; template.contents = text;
env.expectNoSourceDiagnostics(); env.expectNoSourceDiagnostics();

View File

@ -8,7 +8,7 @@
import {initMockFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing'; import {initMockFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
import {extractCursorInfo, humanizeDocumentSpanLike, LanguageServiceTestEnv, Project} from '../testing'; import {humanizeDocumentSpanLike, LanguageServiceTestEnv, Project} from '../testing';
describe('type definitions', () => { describe('type definitions', () => {
let env: LanguageServiceTestEnv; let env: LanguageServiceTestEnv;
@ -42,7 +42,7 @@ describe('type definitions', () => {
function getTypeDefinitionsAndAssertBoundSpan( function getTypeDefinitionsAndAssertBoundSpan(
project: Project, {templateOverride}: {templateOverride: string}) { project: Project, {templateOverride}: {templateOverride: string}) {
const {text} = extractCursorInfo(templateOverride); const text = templateOverride.replace('¦', '');
const template = project.openFile('app.html'); const template = project.openFile('app.html');
template.contents = text; template.contents = text;
env.expectNoSourceDiagnostics(); env.expectNoSourceDiagnostics();

View File

@ -9,8 +9,6 @@
import * as ts from 'typescript/lib/tsserverlibrary'; import * as ts from 'typescript/lib/tsserverlibrary';
import {LanguageService} from '../../language_service'; import {LanguageService} from '../../language_service';
import {extractCursorInfo} from './util';
/** /**
* A file that is currently open in the `ts.Project`, with a cursor position. * A file that is currently open in the `ts.Project`, with a cursor position.
*/ */
@ -101,3 +99,19 @@ export class OpenBuffer {
return this.ngLS.getRenameInfo(this.scriptInfo.fileName, this._cursor); return this.ngLS.getRenameInfo(this.scriptInfo.fileName, this._cursor);
} }
} }
/**
* Given a text snippet which contains exactly one cursor symbol ('¦'), extract both the offset of
* that cursor within the text as well as the text snippet without the cursor.
*/
function extractCursorInfo(textWithCursor: string): {cursor: number, text: string} {
const cursor = textWithCursor.indexOf('¦');
if (cursor === -1 || textWithCursor.indexOf('¦', cursor + 1) !== -1) {
throw new Error(`Expected to find exactly one cursor symbol '¦'`);
}
return {
cursor,
text: textWithCursor.substr(0, cursor) + textWithCursor.substr(cursor + 1),
};
}

View File

@ -5,34 +5,9 @@
* Use of this source code is governed by an MIT-style license that can be * Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {absoluteFrom} from '@angular/compiler-cli/src/ngtsc/file_system';
import {TestFile} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
import {LanguageServiceTestEnv} from './env'; import {LanguageServiceTestEnv} from './env';
import {Project, ProjectFiles, TestableOptions} from './project'; import {Project, ProjectFiles, TestableOptions} from './project';
/**
* Given a text snippet which contains exactly one cursor symbol ('¦'), extract both the offset of
* that cursor within the text as well as the text snippet without the cursor.
*/
export function extractCursorInfo(textWithCursor: string): {cursor: number, text: string} {
const cursor = textWithCursor.indexOf('¦');
if (cursor === -1 || textWithCursor.indexOf('¦', cursor + 1) !== -1) {
throw new Error(`Expected to find exactly one cursor symbol '¦'`);
}
return {
cursor,
text: textWithCursor.substr(0, cursor) + textWithCursor.substr(cursor + 1),
};
}
function last<T>(array: T[]): T {
if (array.length === 0) {
throw new Error(`last() called on empty array`);
}
return array[array.length - 1];
}
/** /**
* Expect that a list of objects with a `fileName` property matches a set of expected files by only * Expect that a list of objects with a `fileName` property matches a set of expected files by only
* comparing the file names and not any path prefixes. * comparing the file names and not any path prefixes.
@ -120,3 +95,10 @@ type Stringy<T> = {
export function getText(contents: string, textSpan: ts.TextSpan) { export function getText(contents: string, textSpan: ts.TextSpan) {
return contents.substr(textSpan.start, textSpan.length); return contents.substr(textSpan.start, textSpan.length);
} }
function last<T>(array: T[]): T {
if (array.length === 0) {
throw new Error(`last() called on empty array`);
}
return array[array.length - 1];
}