@@ -12,26 +12,68 @@ import * as ts from 'typescript';
1212import { TypeScriptNodeEmitter } from './node_emitter' ;
1313import { GENERATED_FILES } from './util' ;
1414
15- const PREAMBLE = `/**
16- * @fileoverview This file was generated by the Angular template compiler.
17- * Do not edit.
18- * @suppress {suspiciousCode,uselessCode,missingProperties,missingOverride,checkTypes}
19- * tslint:disable
20- */` ;
15+ function getPreamble ( original : string ) {
16+ return `/**
17+ * @fileoverview This file was generated by the Angular template compiler. Do not edit.
18+ * ${ original }
19+ * @suppress {suspiciousCode,uselessCode,missingProperties,missingOverride,checkTypes}
20+ * tslint:disable
21+ */` ;
22+ }
2123
22- export function getAngularEmitterTransformFactory ( generatedFiles : Map < string , GeneratedFile > ) : ( ) =>
24+ /**
25+ * Returns a transformer that does two things for generated files (ngfactory etc):
26+ * - adds a fileoverview JSDoc comment containing Closure Compiler specific "suppress"ions in JSDoc.
27+ * The new comment will contain any fileoverview comment text from the original source file this
28+ * file was generated from.
29+ * - updates generated files that are not in the given map of generatedFiles to have an empty
30+ * list of statements as their body.
31+ */
32+ export function getAngularEmitterTransformFactory (
33+ generatedFiles : Map < string , GeneratedFile > , program : ts . Program ) : ( ) =>
2334 ( sourceFile : ts . SourceFile ) => ts . SourceFile {
2435 return function ( ) {
2536 const emitter = new TypeScriptNodeEmitter ( ) ;
2637 return function ( sourceFile : ts . SourceFile ) : ts . SourceFile {
2738 const g = generatedFiles . get ( sourceFile . fileName ) ;
39+ const orig = g && program . getSourceFile ( g . srcFileUrl ) ;
40+ let originalComment = '' ;
41+ if ( orig ) originalComment = getFileoverviewComment ( orig ) ;
42+ const preamble = getPreamble ( originalComment ) ;
2843 if ( g && g . stmts ) {
29- const [ newSourceFile ] = emitter . updateSourceFile ( sourceFile , g . stmts , PREAMBLE ) ;
44+ const orig = program . getSourceFile ( g . srcFileUrl ) ;
45+ let originalComment = '' ;
46+ if ( orig ) originalComment = getFileoverviewComment ( orig ) ;
47+ const [ newSourceFile ] = emitter . updateSourceFile ( sourceFile , g . stmts , preamble ) ;
3048 return newSourceFile ;
3149 } else if ( GENERATED_FILES . test ( sourceFile . fileName ) ) {
32- return ts . updateSourceFileNode ( sourceFile , [ ] ) ;
50+ // The file should be empty, but emitter.updateSourceFile would still add imports
51+ // and various minutiae.
52+ // Clear out the source file entirely, only including the preamble comment, so that
53+ // ngc produces an empty .js file.
54+ return ts . updateSourceFileNode (
55+ sourceFile , [ emitter . createCommentStatement ( sourceFile , preamble ) ] ) ;
3356 }
3457 return sourceFile ;
3558 } ;
3659 } ;
3760}
61+
62+ /**
63+ * Parses and returns the comment text (without start and end markers) of a \@fileoverview comment
64+ * in the given source file. Returns the empty string if no such comment can be found.
65+ */
66+ function getFileoverviewComment ( sourceFile : ts . SourceFile ) : string {
67+ const trivia = sourceFile . getFullText ( ) . substring ( 0 , sourceFile . getStart ( ) ) ;
68+ const leadingComments = ts . getLeadingCommentRanges ( trivia , 0 ) ;
69+ if ( ! leadingComments || leadingComments . length === 0 ) return '' ;
70+ const comment = leadingComments [ 0 ] ;
71+ if ( comment . kind !== ts . SyntaxKind . MultiLineCommentTrivia ) return '' ;
72+ // Only comments separated with a \n\n from the file contents are considered file-level comments
73+ // in TypeScript.
74+ if ( sourceFile . getFullText ( ) . substring ( comment . end , comment . end + 2 ) !== '\n\n' ) return '' ;
75+ const commentText = sourceFile . getFullText ( ) . substring ( comment . pos , comment . end ) ;
76+ // Closure Compiler ignores @suppress and similar if the comment contains @license.
77+ if ( commentText . indexOf ( '@license' ) !== - 1 ) return '' ;
78+ return commentText . replace ( / ^ \/ \* \* / , '' ) . replace ( / ? \* \/ $ / , '' ) ;
79+ }
0 commit comments