Skip to content

Commit faa81dc

Browse files
atscottjosephperrott
authored andcommitted
refactor(language-service): Extract common methods from VE and Ivy to new package (angular#39098)
Rather than having the Ivy implementation add the VE code to the deps list, create a new common package that both Ivy and VE depend on. This will make it more straightforward in the future to remove the VE code completely. PR Close angular#39098
1 parent e10b3e2 commit faa81dc

File tree

7 files changed

+78
-55
lines changed

7 files changed

+78
-55
lines changed

packages/language-service/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ts_library(
1919
"//packages/compiler",
2020
"//packages/compiler-cli",
2121
"//packages/core",
22+
"//packages/language-service/common",
2223
"@npm//@types/node",
2324
"@npm//typescript",
2425
],
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("//tools:defaults.bzl", "ts_library")
2+
3+
package(default_visibility = ["//packages/language-service:__subpackages__"])
4+
5+
ts_library(
6+
name = "common",
7+
srcs = glob(["*.ts"]),
8+
deps = [
9+
"@npm//typescript",
10+
],
11+
)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import * as ts from 'typescript';
10+
11+
// Reverse mappings of enum would generate strings
12+
export const ALIAS_NAME = ts.SymbolDisplayPartKind[ts.SymbolDisplayPartKind.aliasName];
13+
export const SYMBOL_INTERFACE = ts.SymbolDisplayPartKind[ts.SymbolDisplayPartKind.interfaceName];
14+
export const SYMBOL_PUNC = ts.SymbolDisplayPartKind[ts.SymbolDisplayPartKind.punctuation];
15+
export const SYMBOL_SPACE = ts.SymbolDisplayPartKind[ts.SymbolDisplayPartKind.space];
16+
export const SYMBOL_TEXT = ts.SymbolDisplayPartKind[ts.SymbolDisplayPartKind.text];
17+
18+
/**
19+
* Construct a QuickInfo object taking into account its container and type.
20+
* @param name Name of the QuickInfo target
21+
* @param kind component, directive, pipe, etc.
22+
* @param textSpan span of the target
23+
* @param containerName either the Symbol's container or the NgModule that contains the directive
24+
* @param type user-friendly name of the type
25+
* @param documentation docstring or comment
26+
*/
27+
export function createQuickInfo(
28+
name: string, kind: string, textSpan: ts.TextSpan, containerName?: string, type?: string,
29+
documentation?: ts.SymbolDisplayPart[]): ts.QuickInfo {
30+
const containerDisplayParts = containerName ?
31+
[
32+
{text: containerName, kind: SYMBOL_INTERFACE},
33+
{text: '.', kind: SYMBOL_PUNC},
34+
] :
35+
[];
36+
37+
const typeDisplayParts = type ?
38+
[
39+
{text: ':', kind: SYMBOL_PUNC},
40+
{text: ' ', kind: SYMBOL_SPACE},
41+
{text: type, kind: SYMBOL_INTERFACE},
42+
] :
43+
[];
44+
45+
return {
46+
kind: kind as ts.ScriptElementKind,
47+
kindModifiers: ts.ScriptElementKindModifier.none,
48+
textSpan: textSpan,
49+
displayParts: [
50+
{text: '(', kind: SYMBOL_PUNC},
51+
{text: kind, kind: SYMBOL_TEXT},
52+
{text: ')', kind: SYMBOL_PUNC},
53+
{text: ' ', kind: SYMBOL_SPACE},
54+
...containerDisplayParts,
55+
{text: name, kind: SYMBOL_INTERFACE},
56+
...typeDisplayParts,
57+
],
58+
documentation,
59+
};
60+
}

packages/language-service/ivy/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ ts_library(
1515
"//packages/compiler-cli/src/ngtsc/shims",
1616
"//packages/compiler-cli/src/ngtsc/typecheck",
1717
"//packages/compiler-cli/src/ngtsc/typecheck/api",
18-
# TODO(atscott): Pull functions/variables common to VE and Ivy into a new package
19-
"//packages/language-service",
18+
"//packages/language-service/common",
2019
"@npm//typescript",
2120
],
2221
)

packages/language-service/ivy/quick_info.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {NgCompiler} from '@angular/compiler-cli/src/ngtsc/core';
1010
import {DirectiveSymbol, DomBindingSymbol, ElementSymbol, ExpressionSymbol, InputBindingSymbol, OutputBindingSymbol, ReferenceSymbol, ShimLocation, Symbol, SymbolKind, VariableSymbol} from '@angular/compiler-cli/src/ngtsc/typecheck/api';
1111
import * as ts from 'typescript';
1212

13-
import {createQuickInfo, SYMBOL_PUNC, SYMBOL_SPACE, SYMBOL_TEXT} from '../src/hover';
13+
import {createQuickInfo, SYMBOL_PUNC, SYMBOL_SPACE, SYMBOL_TEXT} from '../common/quick_info';
1414

1515
import {findNodeAtPosition} from './hybrid_visitor';
1616
import {filterAliasImports, getDirectiveMatches, getDirectiveMatchesForAttribute, getTemplateInfoAtPosition, getTextSpanOfNode} from './utils';

packages/language-service/ivy/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as e from '@angular/compiler/src/expression_parser/ast'; // e for expr
1212
import * as t from '@angular/compiler/src/render3/r3_ast'; // t for template AST
1313
import * as ts from 'typescript';
1414

15-
import {ALIAS_NAME, SYMBOL_PUNC} from '../src/hover';
15+
import {ALIAS_NAME, SYMBOL_PUNC} from '../common/quick_info';
1616

1717
/**
1818
* Given a list of directives and a text to use as a selector, returns the directives which match

packages/language-service/src/hover.ts

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@
88

99
import {NgAnalyzedModules} from '@angular/compiler';
1010
import * as ts from 'typescript';
11+
12+
import {createQuickInfo} from '../common/quick_info';
13+
1114
import {locateSymbols} from './locate_symbol';
1215
import * as ng from './types';
1316
import {inSpan} from './utils';
1417

15-
// Reverse mappings of enum would generate strings
16-
export const SYMBOL_SPACE = ts.SymbolDisplayPartKind[ts.SymbolDisplayPartKind.space];
17-
export const SYMBOL_PUNC = ts.SymbolDisplayPartKind[ts.SymbolDisplayPartKind.punctuation];
18-
export const SYMBOL_TEXT = ts.SymbolDisplayPartKind[ts.SymbolDisplayPartKind.text];
19-
export const SYMBOL_INTERFACE = ts.SymbolDisplayPartKind[ts.SymbolDisplayPartKind.interfaceName];
20-
export const ALIAS_NAME = ts.SymbolDisplayPartKind[ts.SymbolDisplayPartKind.aliasName];
21-
2218
/**
2319
* Traverse the template AST and look for the symbol located at `position`, then
2420
* return the corresponding quick info.
@@ -71,47 +67,3 @@ export function getTsHover(
7167
}
7268
}
7369
}
74-
75-
/**
76-
* Construct a QuickInfo object taking into account its container and type.
77-
* @param name Name of the QuickInfo target
78-
* @param kind component, directive, pipe, etc.
79-
* @param textSpan span of the target
80-
* @param containerName either the Symbol's container or the NgModule that contains the directive
81-
* @param type user-friendly name of the type
82-
* @param documentation docstring or comment
83-
*/
84-
export function createQuickInfo(
85-
name: string, kind: string, textSpan: ts.TextSpan, containerName?: string, type?: string,
86-
documentation?: ts.SymbolDisplayPart[]): ts.QuickInfo {
87-
const containerDisplayParts = containerName ?
88-
[
89-
{text: containerName, kind: SYMBOL_INTERFACE},
90-
{text: '.', kind: SYMBOL_PUNC},
91-
] :
92-
[];
93-
94-
const typeDisplayParts = type ?
95-
[
96-
{text: ':', kind: SYMBOL_PUNC},
97-
{text: ' ', kind: SYMBOL_SPACE},
98-
{text: type, kind: SYMBOL_INTERFACE},
99-
] :
100-
[];
101-
102-
return {
103-
kind: kind as ts.ScriptElementKind,
104-
kindModifiers: ts.ScriptElementKindModifier.none,
105-
textSpan: textSpan,
106-
displayParts: [
107-
{text: '(', kind: SYMBOL_PUNC},
108-
{text: kind, kind: SYMBOL_TEXT},
109-
{text: ')', kind: SYMBOL_PUNC},
110-
{text: ' ', kind: SYMBOL_SPACE},
111-
...containerDisplayParts,
112-
{text: name, kind: SYMBOL_INTERFACE},
113-
...typeDisplayParts,
114-
],
115-
documentation,
116-
};
117-
}

0 commit comments

Comments
 (0)