This commit adds the basic building blocks for linking partial declarations. In particular it provides a generic `FileLinker` class that delegates to a set of (not yet implemented) `PartialLinker` classes. The Babel plugin makes use of this `FileLinker` providing concrete classes for `AstHost` and `AstFactory` that work with Babel AST. It can be created with the following code: ```ts const plugin = createEs2015LinkerPlugin({ /* options */ }); ``` PR Close #39116
39 lines
1.4 KiB
TypeScript
39 lines
1.4 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google LLC 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 {ImportGenerator, NamedImport} from '../../src/ngtsc/translator';
|
|
import {FatalLinkerError} from './fatal_linker_error';
|
|
|
|
/**
|
|
* A class that is used to generate imports when translating from Angular Output AST to an AST to
|
|
* render, such as Babel.
|
|
*
|
|
* Note that, in the linker, there can only be imports from `@angular/core` and that these imports
|
|
* must be achieved by property access on an `ng` namespace identifer, which is passed in via the
|
|
* constructor.
|
|
*/
|
|
export class LinkerImportGenerator<TExpression> implements ImportGenerator<TExpression> {
|
|
constructor(private ngImport: TExpression) {}
|
|
|
|
generateNamespaceImport(moduleName: string): TExpression {
|
|
this.assertModuleName(moduleName);
|
|
return this.ngImport;
|
|
}
|
|
|
|
generateNamedImport(moduleName: string, originalSymbol: string): NamedImport<TExpression> {
|
|
this.assertModuleName(moduleName);
|
|
return {moduleImport: this.ngImport, symbol: originalSymbol};
|
|
}
|
|
|
|
private assertModuleName(moduleName: string): void {
|
|
if (moduleName !== '@angular/core') {
|
|
throw new FatalLinkerError(
|
|
this.ngImport, `Unable to import from anything other than '@angular/core'`);
|
|
}
|
|
}
|
|
}
|