Skip to content

Commit

Permalink
test(language-service): remove circular dependency (#40966)
Browse files Browse the repository at this point in the history
This commit removes the circular dependency from buffer->util->project->buffer.

PR Close #40966
  • Loading branch information
atscott committed Feb 24, 2021
1 parent c0b364c commit 3fe759e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
4 changes: 2 additions & 2 deletions packages/language-service/ivy/test/quick_info_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {initMockFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';

import * as ts from 'typescript/lib/tsserverlibrary';
import {extractCursorInfo, LanguageServiceTestEnv, Project} from '../testing';
import {LanguageServiceTestEnv, Project} from '../testing';

function quickInfoSkeleton(): {[fileName: string]: string} {
return {
Expand Down Expand Up @@ -516,7 +516,7 @@ describe('quick info', () => {
{templateOverride, expectedSpanText, expectedDisplayString}:
{templateOverride: string, expectedSpanText: string, expectedDisplayString: string}):
ts.QuickInfo {
const {text} = extractCursorInfo(templateOverride);
const text = templateOverride.replace('¦', '');
const template = project.openFile('app.html');
template.contents = text;
env.expectNoSourceDiagnostics();
Expand Down
4 changes: 2 additions & 2 deletions packages/language-service/ivy/test/type_definitions_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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', () => {
let env: LanguageServiceTestEnv;
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('type definitions', () => {

function getTypeDefinitionsAndAssertBoundSpan(
project: Project, {templateOverride}: {templateOverride: string}) {
const {text} = extractCursorInfo(templateOverride);
const text = templateOverride.replace('¦', '');
const template = project.openFile('app.html');
template.contents = text;
env.expectNoSourceDiagnostics();
Expand Down
18 changes: 16 additions & 2 deletions packages/language-service/ivy/testing/src/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import * as ts from 'typescript/lib/tsserverlibrary';
import {LanguageService} from '../../language_service';

import {extractCursorInfo} from './util';

/**
* A file that is currently open in the `ts.Project`, with a cursor position.
*/
Expand Down Expand Up @@ -101,3 +99,19 @@ export class OpenBuffer {
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),
};
}
32 changes: 7 additions & 25 deletions packages/language-service/ivy/testing/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,9 @@
* 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
*/
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 {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
* comparing the file names and not any path prefixes.
Expand Down Expand Up @@ -120,3 +95,10 @@ type Stringy<T> = {
export function getText(contents: string, textSpan: ts.TextSpan) {
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];
}

0 comments on commit 3fe759e

Please sign in to comment.