MCPcopy Create free account
hub / github.com/angular/angular / EmitterVisitor

Class EmitterVisitor

packages/compiler-cli/preprocessor/src/output_ast_printer.ts:492–850  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

490}
491
492class EmitterVisitor extends AbstractEmitterVisitor {
493 suppressLeadingColon = false;
494
495 private readonly constVariableNames = new Set<string>();
496
497 constructor(
498 private printer: ExpressionPrinter,
499 forcePrintTypes = false,
500 ) {
501 super(true /* printComments */, printer.emitTypes || forcePrintTypes);
502 }
503
504 withSuppressedColon(fn: () => void): void {
505 const previous = this.suppressLeadingColon;
506 this.suppressLeadingColon = true;
507 try {
508 fn();
509 } finally {
510 this.suppressLeadingColon = previous;
511 }
512 }
513
514 override visitDeclareVarStmt(stmt: o.DeclareVarStmt, ctx: EmitterVisitorContext): void {
515 super.visitDeclareVarStmt(stmt, ctx);
516
517 if (stmt.hasModifier(o.StmtModifier.Final)) {
518 this.constVariableNames.add(stmt.name);
519 }
520 }
521
522 override visitBinaryOperatorExpr(ast: o.BinaryOperatorExpr, ctx: EmitterVisitorContext): void {
523 super.visitBinaryOperatorExpr(ast, ctx);
524
525 if (!this.printTypes) {
526 return;
527 }
528
529 // We have some internal users who had expressions like `!a > 1` or `'str' + something ?? 'foo'`
530 // which normally get flagged by TS as errors, however due to our aggressive parenthesizing
531 // in TCBs were not being flagged. This logic adds a `ts-ignore` if we detect cases like that,
532 // because we emit expressions in runtime code (more or less) as they were written.
533 if (binaryNeedsTsIgnore(ast) || (ast.isAssignment() && this.referencesConstVariable(ast.lhs))) {
534 ctx.addUniqueSingleLineComment(' @ts-ignore');
535 }
536 }
537
538 override visitUnaryOperatorExpr(ast: o.UnaryOperatorExpr, ctx: EmitterVisitorContext): void {
539 super.visitUnaryOperatorExpr(ast, ctx);
540
541 const isUpdate =
542 ast.operator === o.UnaryOperator.Increment || ast.operator === o.UnaryOperator.Decrement;
543
544 if (this.printTypes && isUpdate && this.referencesConstVariable(ast.expr)) {
545 ctx.addUniqueSingleLineComment(' @ts-ignore');
546 }
547 }
548
549 /** Whether `initialTarget` is a write to a `const` variable. */

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected