"Fossies" - the Fresh Open Source Software Archive

Member "angular-16.2.6/packages/compiler/src/output/abstract_js_emitter.ts" (20 Sep 2023, 4404 Bytes) of package /linux/www/angular-16.2.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /**
    2  * @license
    3  * Copyright Google LLC All Rights Reserved.
    4  *
    5  * Use of this source code is governed by an MIT-style license that can be
    6  * found in the LICENSE file at https://angular.io/license
    7  */
    8 
    9 
   10 import {AbstractEmitterVisitor, EmitterVisitorContext, escapeIdentifier} from './abstract_emitter';
   11 import * as o from './output_ast';
   12 
   13 /**
   14  * In TypeScript, tagged template functions expect a "template object", which is an array of
   15  * "cooked" strings plus a `raw` property that contains an array of "raw" strings. This is
   16  * typically constructed with a function called `__makeTemplateObject(cooked, raw)`, but it may not
   17  * be available in all environments.
   18  *
   19  * This is a JavaScript polyfill that uses __makeTemplateObject when it's available, but otherwise
   20  * creates an inline helper with the same functionality.
   21  *
   22  * In the inline function, if `Object.defineProperty` is available we use that to attach the `raw`
   23  * array.
   24  */
   25 const makeTemplateObjectPolyfill =
   26     '(this&&this.__makeTemplateObject||function(e,t){return Object.defineProperty?Object.defineProperty(e,"raw",{value:t}):e.raw=t,e})';
   27 
   28 export abstract class AbstractJsEmitterVisitor extends AbstractEmitterVisitor {
   29   constructor() {
   30     super(false);
   31   }
   32 
   33   override visitWrappedNodeExpr(ast: o.WrappedNodeExpr<any>, ctx: EmitterVisitorContext): any {
   34     throw new Error('Cannot emit a WrappedNodeExpr in Javascript.');
   35   }
   36 
   37   override visitDeclareVarStmt(stmt: o.DeclareVarStmt, ctx: EmitterVisitorContext): any {
   38     ctx.print(stmt, `var ${stmt.name}`);
   39     if (stmt.value) {
   40       ctx.print(stmt, ' = ');
   41       stmt.value.visitExpression(this, ctx);
   42     }
   43     ctx.println(stmt, `;`);
   44     return null;
   45   }
   46   override visitTaggedTemplateExpr(ast: o.TaggedTemplateExpr, ctx: EmitterVisitorContext): any {
   47     // The following convoluted piece of code is effectively the downlevelled equivalent of
   48     // ```
   49     // tag`...`
   50     // ```
   51     // which is effectively like:
   52     // ```
   53     // tag(__makeTemplateObject(cooked, raw), expression1, expression2, ...);
   54     // ```
   55     const elements = ast.template.elements;
   56     ast.tag.visitExpression(this, ctx);
   57     ctx.print(ast, `(${makeTemplateObjectPolyfill}(`);
   58     ctx.print(ast, `[${elements.map(part => escapeIdentifier(part.text, false)).join(', ')}], `);
   59     ctx.print(ast, `[${elements.map(part => escapeIdentifier(part.rawText, false)).join(', ')}])`);
   60     ast.template.expressions.forEach(expression => {
   61       ctx.print(ast, ', ');
   62       expression.visitExpression(this, ctx);
   63     });
   64     ctx.print(ast, ')');
   65     return null;
   66   }
   67   override visitFunctionExpr(ast: o.FunctionExpr, ctx: EmitterVisitorContext): any {
   68     ctx.print(ast, `function${ast.name ? ' ' + ast.name : ''}(`);
   69     this._visitParams(ast.params, ctx);
   70     ctx.println(ast, `) {`);
   71     ctx.incIndent();
   72     this.visitAllStatements(ast.statements, ctx);
   73     ctx.decIndent();
   74     ctx.print(ast, `}`);
   75     return null;
   76   }
   77   override visitDeclareFunctionStmt(stmt: o.DeclareFunctionStmt, ctx: EmitterVisitorContext): any {
   78     ctx.print(stmt, `function ${stmt.name}(`);
   79     this._visitParams(stmt.params, ctx);
   80     ctx.println(stmt, `) {`);
   81     ctx.incIndent();
   82     this.visitAllStatements(stmt.statements, ctx);
   83     ctx.decIndent();
   84     ctx.println(stmt, `}`);
   85     return null;
   86   }
   87   override visitLocalizedString(ast: o.LocalizedString, ctx: EmitterVisitorContext): any {
   88     // The following convoluted piece of code is effectively the downlevelled equivalent of
   89     // ```
   90     // $localize `...`
   91     // ```
   92     // which is effectively like:
   93     // ```
   94     // $localize(__makeTemplateObject(cooked, raw), expression1, expression2, ...);
   95     // ```
   96     ctx.print(ast, `$localize(${makeTemplateObjectPolyfill}(`);
   97     const parts = [ast.serializeI18nHead()];
   98     for (let i = 1; i < ast.messageParts.length; i++) {
   99       parts.push(ast.serializeI18nTemplatePart(i));
  100     }
  101     ctx.print(ast, `[${parts.map(part => escapeIdentifier(part.cooked, false)).join(', ')}], `);
  102     ctx.print(ast, `[${parts.map(part => escapeIdentifier(part.raw, false)).join(', ')}])`);
  103     ast.expressions.forEach(expression => {
  104       ctx.print(ast, ', ');
  105       expression.visitExpression(this, ctx);
  106     });
  107     ctx.print(ast, ')');
  108     return null;
  109   }
  110 
  111   private _visitParams(params: o.FnParam[], ctx: EmitterVisitorContext): void {
  112     this.visitAllObjects(param => ctx.print(null, param.name), params, ctx, ',');
  113   }
  114 }