diff --git a/packages/compiler-cli/index.ts b/packages/compiler-cli/index.ts index 793470799c..24b78030e3 100644 --- a/packages/compiler-cli/index.ts +++ b/packages/compiler-cli/index.ts @@ -8,10 +8,6 @@ import {NodeJSFileSystem, setFileSystem} from './src/ngtsc/file_system'; export {AotCompilerHost, AotCompilerHost as StaticReflectorHost, StaticReflector, StaticSymbol} from '@angular/compiler'; -export {DiagnosticTemplateInfo, getExpressionScope, getTemplateExpressionDiagnostics} from './src/diagnostics/expression_diagnostics'; -export {AstType, ExpressionDiagnosticsContext} from './src/diagnostics/expression_type'; -export {BuiltinType, DeclarationKind, Definition, PipeInfo, Pipes, Signature, Span, Symbol, SymbolDeclaration, SymbolQuery, SymbolTable} from './src/diagnostics/symbols'; -export {getClassMembersFromDeclaration, getPipesTable, getSymbolQuery} from './src/diagnostics/typescript_symbols'; export {VERSION} from './src/version'; export * from './src/metadata'; diff --git a/packages/compiler-cli/src/language_services.ts b/packages/compiler-cli/src/language_services.ts index 4f04e048c4..a59c053dc0 100644 --- a/packages/compiler-cli/src/language_services.ts +++ b/packages/compiler-cli/src/language_services.ts @@ -14,10 +14,6 @@ Angular modules and Typescript as this will indirectly add a dependency to the language service. */ -export {DiagnosticTemplateInfo, ExpressionDiagnostic, getExpressionDiagnostics, getExpressionScope, getTemplateExpressionDiagnostics} from './diagnostics/expression_diagnostics'; -export {AstType, DiagnosticKind, ExpressionDiagnosticsContext, TypeDiagnostic} from './diagnostics/expression_type'; -export {BuiltinType, DeclarationKind, Definition, Location, PipeInfo, Pipes, Signature, Span, Symbol, SymbolDeclaration, SymbolQuery, SymbolTable} from './diagnostics/symbols'; -export {getClassFromStaticSymbol, getClassMembers, getClassMembersFromDeclaration, getPipesTable, getSymbolQuery} from './diagnostics/typescript_symbols'; export {MetadataCollector, ModuleMetadata} from './metadata'; export {CompilerOptions} from './transformers/api'; export {MetadataReaderCache, MetadataReaderHost, createMetadataReaderCache, readMetadata} from './transformers/metadata_reader'; diff --git a/packages/compiler-cli/test/diagnostics/BUILD.bazel b/packages/compiler-cli/test/diagnostics/BUILD.bazel index 65fab5a496..be2cece23f 100644 --- a/packages/compiler-cli/test/diagnostics/BUILD.bazel +++ b/packages/compiler-cli/test/diagnostics/BUILD.bazel @@ -1,28 +1,11 @@ load("//tools:defaults.bzl", "jasmine_node_test", "ts_library") -ts_library( - name = "mocks", - testonly = True, - srcs = [ - "mocks.ts", - ], - deps = [ - "//packages:types", - "//packages/compiler", - "//packages/compiler-cli", - "//packages/compiler-cli/test:test_utils", - "//packages/core", - "@npm//typescript", - ], -) - # check_types_spec ts_library( name = "check_types_lib", testonly = True, srcs = ["check_types_spec.ts"], deps = [ - ":mocks", "//packages/compiler-cli", "//packages/compiler-cli/test:test_utils", "@npm//typescript", @@ -49,69 +32,6 @@ jasmine_node_test( ], ) -# expression_diagnostics_spec -ts_library( - name = "expression_diagnostics_lib", - testonly = True, - srcs = ["expression_diagnostics_spec.ts"], - deps = [ - ":mocks", - "//packages/compiler", - "//packages/compiler-cli", - "//packages/compiler-cli/test:test_utils", - "//packages/language-service", - "@npm//typescript", - ], -) - -jasmine_node_test( - name = "expression_diagnostics", - bootstrap = ["angular/tools/testing/init_node_spec.js"], - data = [ - "//packages/common:npm_package", - "//packages/core:npm_package", - "//packages/forms:npm_package", - ], - tags = [ - # Disabled as these tests pertain to diagnostics in the old ngc compiler. The Ivy ngtsc - # compiler has its own tests for diagnostics. - "no-ivy-aot", - ], - deps = [ - ":expression_diagnostics_lib", - "//packages/core", - "//tools/testing:node", - ], -) - -# typescript_symbols_spec -ts_library( - name = "typescript_symbols_lib", - testonly = True, - srcs = ["typescript_symbols_spec.ts"], - deps = [ - ":mocks", - "//packages/compiler", - "//packages/compiler-cli", - "//packages/compiler-cli/test:test_utils", - "//packages/compiler/test:test_utils", - "//packages/language-service", - "@npm//typescript", - ], -) - -jasmine_node_test( - name = "typescript_symbols", - bootstrap = ["angular/tools/testing/init_node_spec.js"], - data = [ - ], - deps = [ - ":typescript_symbols_lib", - "//packages/core", - "//tools/testing:node", - ], -) - # typescript_version_spec ts_library( name = "typescript_version_lib", diff --git a/packages/language-service/src/completions.ts b/packages/language-service/src/completions.ts index cebaa6a23c..ccca2f9650 100644 --- a/packages/language-service/src/completions.ts +++ b/packages/language-service/src/completions.ts @@ -7,10 +7,10 @@ */ import {AST, AstPath, Attribute, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventAst, BoundTextAst, CssSelector, Element, ElementAst, ImplicitReceiver, NAMED_ENTITIES, Node as HtmlAst, NullTemplateVisitor, ParseSpan, PropertyRead, TagContentType, Text, findNode, getHtmlTagDefinition} from '@angular/compiler'; -import {getExpressionScope} from '@angular/compiler-cli/src/language_services'; import {$$, $_, isAsciiLetter, isDigit} from '@angular/compiler/src/chars'; import {AstResult} from './common'; +import {getExpressionScope} from './expression_diagnostics'; import {getExpressionCompletions} from './expressions'; import {attributeNames, elementNames, eventNames, propertyNames} from './html_info'; import {InlineTemplate} from './template'; diff --git a/packages/language-service/src/diagnostics.ts b/packages/language-service/src/diagnostics.ts index 6f613055db..d60b014068 100644 --- a/packages/language-service/src/diagnostics.ts +++ b/packages/language-service/src/diagnostics.ts @@ -7,15 +7,16 @@ */ import {NgAnalyzedModules} from '@angular/compiler'; -import {getTemplateExpressionDiagnostics} from '@angular/compiler-cli/src/language_services'; import * as path from 'path'; import * as ts from 'typescript'; import {AstResult} from './common'; +import {getTemplateExpressionDiagnostics} from './expression_diagnostics'; import * as ng from './types'; import {TypeScriptServiceHost} from './typescript_host'; import {findPropertyValueOfType, findTightestNode, offsetSpan, spanOf} from './utils'; + /** * Return diagnostic information for the parsed AST of the template. * @param ast contains HTML and template AST diff --git a/packages/compiler-cli/src/diagnostics/expression_diagnostics.ts b/packages/language-service/src/expression_diagnostics.ts similarity index 100% rename from packages/compiler-cli/src/diagnostics/expression_diagnostics.ts rename to packages/language-service/src/expression_diagnostics.ts diff --git a/packages/compiler-cli/src/diagnostics/expression_type.ts b/packages/language-service/src/expression_type.ts similarity index 100% rename from packages/compiler-cli/src/diagnostics/expression_type.ts rename to packages/language-service/src/expression_type.ts diff --git a/packages/language-service/src/expressions.ts b/packages/language-service/src/expressions.ts index 062fc203b1..26be536414 100644 --- a/packages/language-service/src/expressions.ts +++ b/packages/language-service/src/expressions.ts @@ -7,7 +7,7 @@ */ import {AST, ASTWithSource, AstPath as AstPathBase, NullAstVisitor, visitAstChildren} from '@angular/compiler'; -import {AstType} from '@angular/compiler-cli/src/language_services'; +import {AstType} from './expression_type'; import {BuiltinType, Span, Symbol, SymbolQuery, SymbolTable} from './types'; import {inSpan} from './utils'; diff --git a/packages/language-service/src/locate_symbol.ts b/packages/language-service/src/locate_symbol.ts index cd46f5ee81..499d3f95b1 100644 --- a/packages/language-service/src/locate_symbol.ts +++ b/packages/language-service/src/locate_symbol.ts @@ -7,9 +7,9 @@ */ import {AST, Attribute, BoundDirectivePropertyAst, BoundEventAst, CompileTypeSummary, CssSelector, DirectiveAst, ElementAst, SelectorMatcher, TemplateAstPath, findNode, tokenReference} from '@angular/compiler'; -import {getExpressionScope} from '@angular/compiler-cli/src/language_services'; import {AstResult} from './common'; +import {getExpressionScope} from './expression_diagnostics'; import {getExpressionSymbol} from './expressions'; import {Definition, DirectiveKind, Span, Symbol} from './types'; import {diagnosticInfoFromTemplateInfo, findTemplateAstAt, inSpan, offsetSpan, spanOf} from './utils'; diff --git a/packages/compiler-cli/src/diagnostics/symbols.ts b/packages/language-service/src/symbols.ts similarity index 100% rename from packages/compiler-cli/src/diagnostics/symbols.ts rename to packages/language-service/src/symbols.ts diff --git a/packages/language-service/src/template.ts b/packages/language-service/src/template.ts index d40a7227d1..8a52f7cb78 100644 --- a/packages/language-service/src/template.ts +++ b/packages/language-service/src/template.ts @@ -6,13 +6,14 @@ * found in the LICENSE file at https://angular.io/license */ -import {getClassMembersFromDeclaration, getPipesTable, getSymbolQuery} from '@angular/compiler-cli'; import * as ts from 'typescript'; import {isAstResult} from './common'; import {createGlobalSymbolTable} from './global_symbols'; import * as ng from './types'; import {TypeScriptServiceHost} from './typescript_host'; +import {getClassMembersFromDeclaration, getPipesTable, getSymbolQuery} from './typescript_symbols'; + /** * A base class to represent a template and which component class it is diff --git a/packages/language-service/src/types.ts b/packages/language-service/src/types.ts index a6972fbcc5..3b66681f51 100644 --- a/packages/language-service/src/types.ts +++ b/packages/language-service/src/types.ts @@ -7,9 +7,9 @@ */ import {CompileDirectiveMetadata, NgAnalyzedModules, StaticSymbol} from '@angular/compiler'; -import {BuiltinType, DeclarationKind, Definition, PipeInfo, Pipes, Signature, Span, Symbol, SymbolDeclaration, SymbolQuery, SymbolTable} from '@angular/compiler-cli/src/language_services'; import {AstResult} from './common'; +import {BuiltinType, DeclarationKind, Definition, PipeInfo, Pipes, Signature, Span, Symbol, SymbolDeclaration, SymbolQuery, SymbolTable} from './symbols'; export { BuiltinType, @@ -26,6 +26,7 @@ export { SymbolTable }; + /** * The information `LanguageService` needs from the `LanguageServiceHost` to describe the content of * a template and the language context the template is in. diff --git a/packages/compiler-cli/src/diagnostics/typescript_symbols.ts b/packages/language-service/src/typescript_symbols.ts similarity index 95% rename from packages/compiler-cli/src/diagnostics/typescript_symbols.ts rename to packages/language-service/src/typescript_symbols.ts index 99c63d9155..34f373c1d7 100644 --- a/packages/compiler-cli/src/diagnostics/typescript_symbols.ts +++ b/packages/language-service/src/typescript_symbols.ts @@ -11,7 +11,6 @@ import * as path from 'path'; import * as ts from 'typescript'; import {BuiltinType, DeclarationKind, Definition, Signature, Span, Symbol, SymbolDeclaration, SymbolQuery, SymbolTable} from './symbols'; -import {isVersionBetween} from './typescript_version'; // In TypeScript 2.1 these flags moved // These helpers work for both 2.0 and 2.1. @@ -396,33 +395,18 @@ class SignatureResultOverride implements Signature { get result(): Symbol { return this.resultType; } } -/** - * Indicates the lower bound TypeScript version supporting `SymbolTable` as an ES6 `Map`. - * For lower versions, `SymbolTable` is implemented as a dictionary - */ -const MIN_TS_VERSION_SUPPORTING_MAP = '2.2'; - -export const toSymbolTableFactory = (tsVersion: string) => (symbols: ts.Symbol[]) => { - if (isVersionBetween(tsVersion, MIN_TS_VERSION_SUPPORTING_MAP)) { - // ∀ Typescript version >= 2.2, `SymbolTable` is implemented as an ES6 `Map` - const result = new Map(); - for (const symbol of symbols) { - result.set(symbol.name, symbol); - } - // First, tell the compiler that `result` is of type `any`. Then, use a second type assertion - // to `ts.SymbolTable`. - // Otherwise, `Map` and `ts.SymbolTable` will be considered as incompatible - // types by the compiler - return (result); - } - - // ∀ Typescript version < 2.2, `SymbolTable` is implemented as a dictionary - const result: {[name: string]: ts.Symbol} = {}; +export function toSymbolTableFactory(symbols: ts.Symbol[]) { + // ∀ Typescript version >= 2.2, `SymbolTable` is implemented as an ES6 `Map` + const result = new Map(); for (const symbol of symbols) { - result[symbol.name] = symbol; + result.set(symbol.name, symbol); } + // First, tell the compiler that `result` is of type `any`. Then, use a second type assertion + // to `ts.SymbolTable`. + // Otherwise, `Map` and `ts.SymbolTable` will be considered as incompatible + // types by the compiler return (result); -}; +} function toSymbols(symbolTable: ts.SymbolTable | undefined): ts.Symbol[] { if (!symbolTable) return []; @@ -456,8 +440,7 @@ class SymbolTableWrapper implements SymbolTable { if (Array.isArray(symbols)) { this.symbols = symbols; - const toSymbolTable = toSymbolTableFactory(ts.version); - this.symbolTable = toSymbolTable(symbols); + this.symbolTable = toSymbolTableFactory(symbols); } else { this.symbols = toSymbols(symbols); this.symbolTable = symbols; diff --git a/packages/language-service/src/utils.ts b/packages/language-service/src/utils.ts index faa674a6f0..9863d09ef9 100644 --- a/packages/language-service/src/utils.ts +++ b/packages/language-service/src/utils.ts @@ -7,10 +7,10 @@ */ import {AstPath, CompileDirectiveSummary, CompileTypeMetadata, CssSelector, DirectiveAst, ElementAst, EmbeddedTemplateAst, ParseSourceSpan, RecursiveTemplateAstVisitor, TemplateAst, TemplateAstPath, identifierName, templateVisitAll} from '@angular/compiler'; -import {DiagnosticTemplateInfo} from '@angular/compiler-cli/src/language_services'; import * as ts from 'typescript'; import {AstResult, SelectorInfo} from './common'; +import {DiagnosticTemplateInfo} from './expression_diagnostics'; import {Span} from './types'; export interface SpanHolder { diff --git a/packages/language-service/test/BUILD.bazel b/packages/language-service/test/BUILD.bazel index 81cd6f4e5b..6ad9838fd1 100644 --- a/packages/language-service/test/BUILD.bazel +++ b/packages/language-service/test/BUILD.bazel @@ -3,15 +3,25 @@ load("//tools:defaults.bzl", "jasmine_node_test", "ts_library") ts_library( name = "test_lib", testonly = True, - srcs = glob( - include = ["**/*.ts"], - exclude = ["project/**/*"], - ), + srcs = [ + "completions_spec.ts", + "definitions_spec.ts", + "diagnostics_spec.ts", + "global_symbols_spec.ts", + "hover_spec.ts", + "html_info_spec.ts", + "language_service_spec.ts", + "reflector_host_spec.ts", + "template_references_spec.ts", + "template_spec.ts", + "test_utils.ts", + "ts_plugin_spec.ts", + "typescript_host_spec.ts", + "utils_spec.ts", + ], data = glob(["project/**/*"]), deps = [ - "//packages:types", "//packages/compiler", - "//packages/compiler-cli", "//packages/compiler-cli/test:test_utils", "//packages/language-service", "@npm//typescript", @@ -32,3 +42,37 @@ jasmine_node_test( ":test_lib", ], ) + +ts_library( + name = "diagnostics_lib", + testonly = True, + srcs = [ + "expression_diagnostics_spec.ts", + "mocks.ts", + "typescript_symbols_spec.ts", + ], + deps = [ + "//packages/compiler", + "//packages/compiler-cli/test:test_utils", + "//packages/core", + "//packages/language-service", + "@npm//typescript", + ], +) + +jasmine_node_test( + name = "diagnostics", + data = [ + "//packages/common:npm_package", + "//packages/core:npm_package", + "//packages/forms:npm_package", + ], + tags = [ + # Disabled as these tests pertain to diagnostics in the old ngc compiler. The Ivy ngtsc + # compiler has its own tests for diagnostics. + "no-ivy-aot", + ], + deps = [ + ":diagnostics_lib", + ], +) diff --git a/packages/compiler-cli/test/diagnostics/expression_diagnostics_spec.ts b/packages/language-service/test/expression_diagnostics_spec.ts similarity index 98% rename from packages/compiler-cli/test/diagnostics/expression_diagnostics_spec.ts rename to packages/language-service/test/expression_diagnostics_spec.ts index 9660ae74b0..4cd3c15a13 100644 --- a/packages/compiler-cli/test/diagnostics/expression_diagnostics_spec.ts +++ b/packages/language-service/test/expression_diagnostics_spec.ts @@ -7,11 +7,11 @@ */ import {StaticSymbol} from '@angular/compiler'; +import {Directory} from '@angular/compiler-cli/test/mocks'; import {ReflectorHost} from '@angular/language-service/src/reflector_host'; import * as ts from 'typescript'; -import {getTemplateExpressionDiagnostics} from '../../src/diagnostics/expression_diagnostics'; -import {Directory} from '../mocks'; +import {getTemplateExpressionDiagnostics} from '../src/expression_diagnostics'; import {DiagnosticContext, MockLanguageServiceHost, getDiagnosticTemplateInfo} from './mocks'; diff --git a/packages/language-service/test/global_symbols_spec.ts b/packages/language-service/test/global_symbols_spec.ts index 9dea6d8aba..f85241bb42 100644 --- a/packages/language-service/test/global_symbols_spec.ts +++ b/packages/language-service/test/global_symbols_spec.ts @@ -6,10 +6,10 @@ * found in the LICENSE file at https://angular.io/license */ -import {getSymbolQuery} from '@angular/compiler-cli'; import * as ts from 'typescript/lib/tsserverlibrary'; import {EMPTY_SYMBOL_TABLE, createGlobalSymbolTable} from '../src/global_symbols'; +import {getSymbolQuery} from '../src/typescript_symbols'; import {MockTypescriptHost} from './test_utils'; diff --git a/packages/compiler-cli/test/diagnostics/mocks.ts b/packages/language-service/test/mocks.ts similarity index 97% rename from packages/compiler-cli/test/diagnostics/mocks.ts rename to packages/language-service/test/mocks.ts index ab3ab7ac19..e58327e1db 100644 --- a/packages/compiler-cli/test/diagnostics/mocks.ts +++ b/packages/language-service/test/mocks.ts @@ -7,15 +7,15 @@ */ import {AotSummaryResolver, CompileMetadataResolver, CompilerConfig, DEFAULT_INTERPOLATION_CONFIG, DirectiveNormalizer, DirectiveResolver, DomElementSchemaRegistry, HtmlParser, I18NHtmlParser, InterpolationConfig, JitSummaryResolver, Lexer, NgAnalyzedModules, NgModuleResolver, ParseTreeResult, Parser, PipeResolver, ResourceLoader, StaticReflector, StaticSymbol, StaticSymbolCache, StaticSymbolResolver, StaticSymbolResolverHost, SummaryResolver, TemplateParser, analyzeNgModules, createOfflineCompileUrlResolver} from '@angular/compiler'; +import {Directory, MockAotContext} from '@angular/compiler-cli/test/mocks'; +import {setup} from '@angular/compiler-cli/test/test_support'; import {ViewEncapsulation, ɵConsole as Console} from '@angular/core'; import * as fs from 'fs'; import * as path from 'path'; import * as ts from 'typescript'; -import {DiagnosticTemplateInfo} from '../../src/diagnostics/expression_diagnostics'; -import {getClassMembers, getPipesTable, getSymbolQuery} from '../../src/diagnostics/typescript_symbols'; -import {Directory, MockAotContext} from '../mocks'; -import {setup} from '../test_support'; +import {DiagnosticTemplateInfo} from '../src/expression_diagnostics'; +import {getClassMembers, getPipesTable, getSymbolQuery} from '../src/typescript_symbols'; const realFiles = new Map(); diff --git a/packages/compiler-cli/test/diagnostics/typescript_symbols_spec.ts b/packages/language-service/test/typescript_symbols_spec.ts similarity index 87% rename from packages/compiler-cli/test/diagnostics/typescript_symbols_spec.ts rename to packages/language-service/test/typescript_symbols_spec.ts index 5157470dfd..ce661491b0 100644 --- a/packages/compiler-cli/test/diagnostics/typescript_symbols_spec.ts +++ b/packages/language-service/test/typescript_symbols_spec.ts @@ -6,12 +6,12 @@ * found in the LICENSE file at https://angular.io/license */ +import {Directory} from '@angular/compiler-cli/test/mocks'; import {ReflectorHost} from '@angular/language-service/src/reflector_host'; import * as ts from 'typescript'; -import {BuiltinType, Symbol, SymbolQuery, SymbolTable} from '../../src/diagnostics/symbols'; -import {getSymbolQuery, toSymbolTableFactory} from '../../src/diagnostics/typescript_symbols'; -import {Directory} from '../mocks'; +import {BuiltinType, Symbol, SymbolQuery, SymbolTable} from '../src/symbols'; +import {getSymbolQuery, toSymbolTableFactory} from '../src/typescript_symbols'; import {DiagnosticContext, MockLanguageServiceHost} from './mocks'; @@ -74,15 +74,10 @@ describe('symbol query', () => { }); describe('toSymbolTableFactory(tsVersion)', () => { - it('should return a Map for versions of TypeScript >= 2.2 and a dictionary otherwise', () => { + it('should return a Map for versions of TypeScript >= 2.2', () => { const a = { name: 'a' } as ts.Symbol; const b = { name: 'b' } as ts.Symbol; - - expect(toSymbolTableFactory('2.1')([a, b]) instanceof Map).toEqual(false); - expect(toSymbolTableFactory('2.4')([a, b]) instanceof Map).toEqual(true); - - // Check that for the lower bound version `2.2`, toSymbolTableFactory('2.2') returns a map - expect(toSymbolTableFactory('2.2')([a, b]) instanceof Map).toEqual(true); + expect(toSymbolTableFactory([a, b]) instanceof Map).toEqual(true); }); });