From d1535a1a77d5fbfe192920f562026e3e184e95e3 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 23 Feb 2021 13:22:37 -0800 Subject: [PATCH] test(language-service): remove circular dependency (#40966) This commit removes the circular dependency from buffer->util->project->buffer. PR Close #40966 --- .../ivy/test/quick_info_spec.ts | 4 +-- .../ivy/test/type_definitions_spec.ts | 4 +-- .../ivy/testing/src/buffer.ts | 18 +++++++++-- .../language-service/ivy/testing/src/util.ts | 32 ++++--------------- 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/packages/language-service/ivy/test/quick_info_spec.ts b/packages/language-service/ivy/test/quick_info_spec.ts index 793cff68c2..41e8b4bf2b 100644 --- a/packages/language-service/ivy/test/quick_info_spec.ts +++ b/packages/language-service/ivy/test/quick_info_spec.ts @@ -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 { @@ -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(); diff --git a/packages/language-service/ivy/test/type_definitions_spec.ts b/packages/language-service/ivy/test/type_definitions_spec.ts index a911986082..ebe6ff11fa 100644 --- a/packages/language-service/ivy/test/type_definitions_spec.ts +++ b/packages/language-service/ivy/test/type_definitions_spec.ts @@ -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; @@ -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(); diff --git a/packages/language-service/ivy/testing/src/buffer.ts b/packages/language-service/ivy/testing/src/buffer.ts index aa7a49eb9e..da6d0a2b9f 100644 --- a/packages/language-service/ivy/testing/src/buffer.ts +++ b/packages/language-service/ivy/testing/src/buffer.ts @@ -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. */ @@ -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), + }; +} \ No newline at end of file diff --git a/packages/language-service/ivy/testing/src/util.ts b/packages/language-service/ivy/testing/src/util.ts index 780ad53853..b763c77f4d 100644 --- a/packages/language-service/ivy/testing/src/util.ts +++ b/packages/language-service/ivy/testing/src/util.ts @@ -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(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. @@ -120,3 +95,10 @@ type Stringy = { export function getText(contents: string, textSpan: ts.TextSpan) { return contents.substr(textSpan.start, textSpan.length); } + +function last(array: T[]): T { + if (array.length === 0) { + throw new Error(`last() called on empty array`); + } + return array[array.length - 1]; +} \ No newline at end of file