fix(compiler-cli): merge @fileoverview comments. (#20870)

Previously, this code would unconditionally add a @fileoverview
comment to generated files, and only if the contained any code at all.

However often existing fileoverview comments should be copied from the
file the generated file was originally based off of. This allows users
to e.g. include Closure Compiler directives in their original
`component.ts` file, which will then automaticallly also apply to code
generated from it.

This special cases `@license` comments, as Closure disregards directives
in comments containing `@license`.

PR Close #20870
This commit is contained in:
Martin Probst
2017-12-07 17:52:16 +01:00
committed by Jason Aden
parent add3589451
commit 8c52088346
5 changed files with 157 additions and 27 deletions

View File

@ -249,6 +249,90 @@ describe('ngc transformer command-line', () => {
.toBe(true);
});
describe('comments', () => {
function compileAndRead(contents: string) {
writeConfig(`{
"extends": "./tsconfig-base.json",
"files": ["mymodule.ts"],
"angularCompilerOptions": {"allowEmptyCodegenFiles": true}
}`);
write('mymodule.ts', contents);
const exitCode = main(['-p', basePath], errorSpy);
expect(exitCode).toEqual(0);
const modPath = path.resolve(outDir, 'mymodule.ngfactory.js');
expect(fs.existsSync(modPath)).toBe(true);
return fs.readFileSync(modPath, {encoding: 'UTF-8'});
}
it('should be added', () => {
const contents = compileAndRead(`
import {CommonModule} from '@angular/common';
import {NgModule} from '@angular/core';
@NgModule({
imports: [CommonModule]
})
export class MyModule {}
`);
expect(contents).toContain('@fileoverview');
expect(contents).toContain('generated by the Angular template compiler');
expect(contents).toContain('@suppress {suspiciousCode');
});
it('should be merged with existing fileoverview comments', () => {
const contents = compileAndRead(`/** Hello world. */
import {CommonModule} from '@angular/common';
import {NgModule} from '@angular/core';
@NgModule({
imports: [CommonModule]
})
export class MyModule {}
`);
expect(contents).toContain('Hello world.');
});
it('should only pick file comments', () => {
const contents = compileAndRead(`
/** Comment on class. */
class MyClass {
}
`);
expect(contents).toContain('@fileoverview');
expect(contents).not.toContain('Comment on class.');
});
it('should not be merged with @license comments', () => {
const contents = compileAndRead(`/** @license Some license. */
import {CommonModule} from '@angular/common';
import {NgModule} from '@angular/core';
@NgModule({
imports: [CommonModule]
})
export class MyModule {}
`);
expect(contents).toContain('@fileoverview');
expect(contents).not.toContain('@license');
});
it('should be included in empty files', () => {
const contents = compileAndRead(`/** My comment. */
import {Inject, Injectable, Optional} from '@angular/core';
@Injectable()
export class NotAnAngularComponent {}
`);
expect(contents).toContain('My comment');
});
});
it('should compile with an explicit tsconfig reference', () => {
writeConfig(`{
"extends": "./tsconfig-base.json",

View File

@ -437,7 +437,8 @@ describe('ng program', () => {
sf => sf.fileName === path.join(testSupport.basePath, checks.originalFileName)))
.toBe(true);
if (checks.shouldBeEmpty) {
expect(writeData !.data).toBe('');
// The file should only contain comments (the preamble comment added by ngc).
expect(writeData !.data).toMatch(/^(\s*\/\*([^*]|\*[^/])*\*\/\s*)?$/);
} else {
expect(writeData !.data).not.toBe('');
}