From 4ad323a4d661876dece47e9089e92e9832d618ad Mon Sep 17 00:00:00 2001 From: Ayaz Hafiz Date: Mon, 10 Jun 2019 09:19:35 -0700 Subject: [PATCH] feat(ivy): setup boilerplate for component indexing API (#30961) Set up the skeleton for a compiler API that indexes components and their templates on an independent indexing step. Part of #30959 PR Close #30961 --- packages/compiler-cli/BUILD.bazel | 1 + .../src/ngtsc/indexer/BUILD.bazel | 14 +++++++ .../compiler-cli/src/ngtsc/indexer/index.ts | 10 +++++ .../compiler-cli/src/ngtsc/indexer/src/api.ts | 41 +++++++++++++++++++ .../src/ngtsc/indexer/src/context.ts | 13 ++++++ .../src/ngtsc/indexer/src/transform.ts | 21 ++++++++++ packages/compiler-cli/src/ngtsc/program.ts | 5 +++ .../src/ngtsc/transform/BUILD.bazel | 1 + .../src/ngtsc/transform/src/api.ts | 8 ++++ .../src/ngtsc/transform/src/compilation.ts | 6 +++ 10 files changed, 120 insertions(+) create mode 100644 packages/compiler-cli/src/ngtsc/indexer/BUILD.bazel create mode 100644 packages/compiler-cli/src/ngtsc/indexer/index.ts create mode 100644 packages/compiler-cli/src/ngtsc/indexer/src/api.ts create mode 100644 packages/compiler-cli/src/ngtsc/indexer/src/context.ts create mode 100644 packages/compiler-cli/src/ngtsc/indexer/src/transform.ts diff --git a/packages/compiler-cli/BUILD.bazel b/packages/compiler-cli/BUILD.bazel index 2c1eab6262..ce8922eea7 100644 --- a/packages/compiler-cli/BUILD.bazel +++ b/packages/compiler-cli/BUILD.bazel @@ -29,6 +29,7 @@ ts_library( "//packages/compiler-cli/src/ngtsc/entry_point", "//packages/compiler-cli/src/ngtsc/imports", "//packages/compiler-cli/src/ngtsc/incremental", + "//packages/compiler-cli/src/ngtsc/indexer", "//packages/compiler-cli/src/ngtsc/metadata", "//packages/compiler-cli/src/ngtsc/partial_evaluator", "//packages/compiler-cli/src/ngtsc/path", diff --git a/packages/compiler-cli/src/ngtsc/indexer/BUILD.bazel b/packages/compiler-cli/src/ngtsc/indexer/BUILD.bazel new file mode 100644 index 0000000000..6151c77e8f --- /dev/null +++ b/packages/compiler-cli/src/ngtsc/indexer/BUILD.bazel @@ -0,0 +1,14 @@ +load("//tools:defaults.bzl", "ts_library") + +package(default_visibility = ["//visibility:public"]) + +ts_library( + name = "indexer", + srcs = ["index.ts"] + glob([ + "src/**/*.ts", + ]), + deps = [ + "//packages/compiler", + "@npm//typescript", + ], +) diff --git a/packages/compiler-cli/src/ngtsc/indexer/index.ts b/packages/compiler-cli/src/ngtsc/indexer/index.ts new file mode 100644 index 0000000000..ba903a8a61 --- /dev/null +++ b/packages/compiler-cli/src/ngtsc/indexer/index.ts @@ -0,0 +1,10 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * 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 + */ + +export * from './src/api'; +export {IndexingContext} from './src/context'; diff --git a/packages/compiler-cli/src/ngtsc/indexer/src/api.ts b/packages/compiler-cli/src/ngtsc/indexer/src/api.ts new file mode 100644 index 0000000000..75d51e6e65 --- /dev/null +++ b/packages/compiler-cli/src/ngtsc/indexer/src/api.ts @@ -0,0 +1,41 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * 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 {ParseSourceFile, ParseSpan} from '@angular/compiler'; +import * as ts from 'typescript'; + +/** + * Describes the kind of identifier found in a template. + */ +export enum IdentifierKind { +} + +/** + * Describes a semantically-interesting identifier in a template, such as an interpolated variable + * or selector. + */ +export interface TemplateIdentifier { + name: string; + span: ParseSpan; + kind: IdentifierKind; + file: ParseSourceFile; +} + +/** + * Describes an analyzed, indexed component and its template. + */ +export interface IndexedComponent { + name: string; + selector: string|null; + sourceFile: string; + content: string; + template: { + identifiers: Set, + usedComponents: Set, + }; +} diff --git a/packages/compiler-cli/src/ngtsc/indexer/src/context.ts b/packages/compiler-cli/src/ngtsc/indexer/src/context.ts new file mode 100644 index 0000000000..d571e01433 --- /dev/null +++ b/packages/compiler-cli/src/ngtsc/indexer/src/context.ts @@ -0,0 +1,13 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * 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 + */ + +/** + * Stores analysis information about components in a compilation for and provides methods for + * querying information about components to be used in indexing. + */ +export class IndexingContext {} diff --git a/packages/compiler-cli/src/ngtsc/indexer/src/transform.ts b/packages/compiler-cli/src/ngtsc/indexer/src/transform.ts new file mode 100644 index 0000000000..60161e2b89 --- /dev/null +++ b/packages/compiler-cli/src/ngtsc/indexer/src/transform.ts @@ -0,0 +1,21 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * 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 * as ts from 'typescript'; +import {IndexedComponent} from './api'; +import {IndexingContext} from './context'; + +/** + * Generates `IndexedComponent` entries from a `IndexingContext`, which has information + * about components discovered in the program registered in it. + * + * The context must be populated before `generateAnalysis` is called. + */ +export function generateAnalysis(context: IndexingContext): Map { + throw new Error('Method not implemented.'); +} diff --git a/packages/compiler-cli/src/ngtsc/program.ts b/packages/compiler-cli/src/ngtsc/program.ts index 0d768d3df6..1808d519a4 100644 --- a/packages/compiler-cli/src/ngtsc/program.ts +++ b/packages/compiler-cli/src/ngtsc/program.ts @@ -19,6 +19,7 @@ import {ErrorCode, ngErrorCode} from './diagnostics'; import {FlatIndexGenerator, ReferenceGraph, checkForPrivateExports, findFlatIndexEntryPoint} from './entry_point'; import {AbsoluteModuleStrategy, AliasGenerator, AliasStrategy, DefaultImportTracker, FileToModuleHost, FileToModuleStrategy, ImportRewriter, LocalIdentifierStrategy, LogicalProjectStrategy, ModuleResolver, NoopImportRewriter, R3SymbolsImportRewriter, Reference, ReferenceEmitter} from './imports'; import {IncrementalState} from './incremental'; +import {IndexedComponent} from './indexer'; import {CompoundMetadataReader, CompoundMetadataRegistry, DtsMetadataReader, LocalMetadataRegistry, MetadataReader} from './metadata'; import {PartialEvaluator} from './partial_evaluator'; import {AbsoluteFsPath, LogicalFileSystem} from './path'; @@ -264,6 +265,10 @@ export class NgtscProgram implements api.Program { return this.routeAnalyzer !.listLazyRoutes(entryRoute); } + getIndexedComponents(): Map { + throw new Error('Method not implemented.'); + } + getLibrarySummaries(): Map { throw new Error('Method not implemented.'); } diff --git a/packages/compiler-cli/src/ngtsc/transform/BUILD.bazel b/packages/compiler-cli/src/ngtsc/transform/BUILD.bazel index 42d58f5792..4a280a658a 100644 --- a/packages/compiler-cli/src/ngtsc/transform/BUILD.bazel +++ b/packages/compiler-cli/src/ngtsc/transform/BUILD.bazel @@ -12,6 +12,7 @@ ts_library( "//packages/compiler-cli/src/ngtsc/diagnostics", "//packages/compiler-cli/src/ngtsc/imports", "//packages/compiler-cli/src/ngtsc/incremental", + "//packages/compiler-cli/src/ngtsc/indexer", "//packages/compiler-cli/src/ngtsc/perf", "//packages/compiler-cli/src/ngtsc/reflection", "//packages/compiler-cli/src/ngtsc/scope", diff --git a/packages/compiler-cli/src/ngtsc/transform/src/api.ts b/packages/compiler-cli/src/ngtsc/transform/src/api.ts index 76a627818a..2b8967bba9 100644 --- a/packages/compiler-cli/src/ngtsc/transform/src/api.ts +++ b/packages/compiler-cli/src/ngtsc/transform/src/api.ts @@ -10,6 +10,7 @@ import {ConstantPool, Expression, Statement, Type} from '@angular/compiler'; import * as ts from 'typescript'; import {Reexport} from '../../imports'; +import {IndexingContext} from '../../indexer'; import {ClassDeclaration, Decorator} from '../../reflection'; import {TypeCheckContext} from '../../typecheck'; @@ -76,6 +77,13 @@ export interface DecoratorHandler { */ analyze(node: ClassDeclaration, metadata: M): AnalysisOutput; + /** + * Registers information about the decorator for the indexing phase in a + * `IndexingContext`, which stores information about components discovered in the + * program. + */ + index?(context: IndexingContext, node: ClassDeclaration, metadata: M): void; + /** * Perform resolution on the given decorator along with the result of analysis. * diff --git a/packages/compiler-cli/src/ngtsc/transform/src/compilation.ts b/packages/compiler-cli/src/ngtsc/transform/src/compilation.ts index cd9e298c4b..e32c0abe9e 100644 --- a/packages/compiler-cli/src/ngtsc/transform/src/compilation.ts +++ b/packages/compiler-cli/src/ngtsc/transform/src/compilation.ts @@ -12,6 +12,7 @@ import * as ts from 'typescript'; import {ErrorCode, FatalDiagnosticError} from '../../diagnostics'; import {ImportRewriter} from '../../imports'; import {IncrementalState} from '../../incremental'; +import {IndexingContext} from '../../indexer'; import {PerfRecorder} from '../../perf'; import {ClassDeclaration, ReflectionHost, isNamedClassDeclaration, reflectNameOfDeclaration} from '../../reflection'; import {LocalModuleScopeRegistry} from '../../scope'; @@ -250,6 +251,11 @@ export class IvyCompilation { } } + /** + * Feeds components discovered in the compilation to a context for indexing. + */ + index(context: IndexingContext) { throw new Error('Method not implemented.'); } + resolve(): void { const resolveSpan = this.perf.start('resolve'); this.ivyClasses.forEach((ivyClass, node) => {