Skip to content

Commit

Permalink
feat(ivy): implement Renderer.getSwitchableDeclarations (#25534)
Browse files Browse the repository at this point in the history
This supports the "ngcc ivy switch" specified in #25238.

PR Close #25534
  • Loading branch information
petebacondarwin authored and mhevery committed Aug 31, 2018
1 parent e73e864 commit 26cd9f5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
11 changes: 11 additions & 0 deletions packages/compiler-cli/src/ngcc/src/rendering/esm2015_renderer.ts
Expand Up @@ -7,6 +7,7 @@
*/
import * as ts from 'typescript';
import MagicString from 'magic-string';
import {POST_NGCC_MARKER, PRE_NGCC_MARKER} from '../host/ngcc_host';
import {AnalyzedClass} from '../analyzer';
import {Renderer} from './renderer';

Expand Down Expand Up @@ -70,4 +71,14 @@ export class Esm2015Renderer extends Renderer {
}
});
}

rewriteSwitchableDeclarations(outputText: MagicString, sourceFile: ts.SourceFile): void {
const declarations = this.host.getSwitchableDeclarations(sourceFile);
declarations.forEach(declaration => {
const start = declaration.initializer.getStart();
const end = declaration.initializer.getEnd();
const replacement = declaration.initializer.text.replace(PRE_NGCC_MARKER, POST_NGCC_MARKER);
outputText.overwrite(start, end, replacement);
});
}
}
8 changes: 7 additions & 1 deletion packages/compiler-cli/src/ngcc/src/rendering/renderer.ts
Expand Up @@ -89,9 +89,13 @@ export abstract class Renderer {
file.sourceFile);

this.addImports(outputText, importManager.getAllImports(file.sourceFile.fileName, null));
// QUESTION: do we need to remove contructor param metadata and property decorators?

// TODO: remove contructor param metadata and property decorators (we need info from the
// handlers to do this)
this.removeDecorators(outputText, decoratorsToRemove);

this.rewriteSwitchableDeclarations(outputText, file.sourceFile);

return this.renderSourceAndMap(file, input, outputText, targetPath);
}

Expand All @@ -102,6 +106,8 @@ export abstract class Renderer {
output: MagicString, analyzedClass: AnalyzedClass, definitions: string): void;
protected abstract removeDecorators(
output: MagicString, decoratorsToRemove: Map<ts.Node, ts.Node[]>): void;
protected abstract rewriteSwitchableDeclarations(
outputText: MagicString, sourceFile: ts.SourceFile): void;

/**
* Add the decorator nodes that are to be removed to a map
Expand Down
Expand Up @@ -46,6 +46,19 @@ export class C {}
C.decorators = [
{ type: Directive, args: [{ selector: '[c]' }] },
];
let compileNgModuleFactory = compileNgModuleFactory__PRE_NGCC__;
let badlyFormattedVariable = __PRE_NGCC__badlyFormattedVariable;
function compileNgModuleFactory__PRE_NGCC__(injector, options, moduleType) {
const compilerFactory = injector.get(CompilerFactory);
const compiler = compilerFactory.createCompiler([options]);
return compiler.compileModuleAsync(moduleType);
}
function compileNgModuleFactory__POST_NGCC__(injector, options, moduleType) {
ngDevMode && assertNgModuleType(moduleType);
return Promise.resolve(new R3NgModuleFactory(moduleType));
}
// Some other content`
};

Expand Down Expand Up @@ -82,6 +95,29 @@ export class A {}`);
});
});

describe('rewriteSwitchableDeclarations', () => {
it('should switch marked declaration initializers', () => {
const {renderer, program} = setup(PROGRAM);
const file = program.getSourceFile('some/file.js');
if (file === undefined) {
throw new Error(`Could not find source file`);
}
const output = new MagicString(PROGRAM.contents);
renderer.rewriteSwitchableDeclarations(output, file);
expect(output.toString())
.not.toContain(`let compileNgModuleFactory = compileNgModuleFactory__PRE_NGCC__;`);
expect(output.toString())
.toContain(`let badlyFormattedVariable = __PRE_NGCC__badlyFormattedVariable;`);
expect(output.toString())
.toContain(`let compileNgModuleFactory = compileNgModuleFactory__POST_NGCC__;`);
expect(output.toString())
.toContain(
`function compileNgModuleFactory__PRE_NGCC__(injector, options, moduleType) {`);
expect(output.toString())
.toContain(
`function compileNgModuleFactory__POST_NGCC__(injector, options, moduleType) {`);
});
});

describe('addDefinitions', () => {
it('should insert the definitions directly after the class declaration', () => {
Expand Down
Expand Up @@ -29,6 +29,9 @@ class TestRenderer extends Renderer {
removeDecorators(output: MagicString, decoratorsToRemove: Map<ts.Node, ts.Node[]>) {
output.prepend('\n// REMOVE DECORATORS\n');
}
rewriteSwitchableDeclarations(output: MagicString, sourceFile: ts.SourceFile): void {
output.prepend('\n// REWRITTEN DECLARATIONS\n');
}
}

function createTestRenderer() {
Expand Down Expand Up @@ -68,7 +71,7 @@ describe('Renderer', () => {
]
});
const RENDERED_CONTENTS =
`\n// REMOVE DECORATORS\n\n// ADD IMPORTS\n\n// ADD CONSTANTS\n\n// ADD DEFINITIONS\n` +
`\n// REWRITTEN DECLARATIONS\n\n// REMOVE DECORATORS\n\n// ADD IMPORTS\n\n// ADD CONSTANTS\n\n// ADD DEFINITIONS\n` +
INPUT_PROGRAM.contents;
const OUTPUT_PROGRAM_MAP = fromObject({
'version': 3,
Expand All @@ -78,14 +81,14 @@ describe('Renderer', () => {
'import { Directive } from \'@angular/core\';\nexport class A {\n foo(x) {\n return x;\n }\n}\nA.decorators = [\n { type: Directive, args: [{ selector: \'[a]\' }] }\n];\n'
],
'names': [],
'mappings': ';;;;;;;;AAAA;;;;;;;;;'
'mappings': ';;;;;;;;;;AAAA;;;;;;;;;'
});

const MERGED_OUTPUT_PROGRAM_MAP = fromObject({
'version': 3,
'sources': ['/file.ts'],
'names': [],
'mappings': ';;;;;;;;AAAA',
'mappings': ';;;;;;;;;;AAAA',
'file': '/output_file.js',
'sourcesContent': [
'import { Directive } from \'@angular/core\';\nexport class A {\n foo(x: string): string {\n return x;\n }\n static decorators = [\n { type: Directive, args: [{ selector: \'[a]\' }] }\n ];\n}'
Expand Down

0 comments on commit 26cd9f5

Please sign in to comment.