From 90df7de54d1f84d51b0f28e6c47665d1fdb01f66 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 13 Mar 2019 16:46:06 +0100 Subject: [PATCH] refactor(core): better failure message for explicit-query timing tslint rule (#29258) Improves the failure message for the `explicit-query` timing TSLint rule that is used within Google. Currently it's not very clear what action developers need to take in order to resolve the lint failure manually. PR Close #29258 --- .../static-queries/google3/explicitQueryTimingRule.ts | 9 +++++++-- .../test/google3/explicit_query_timing_rule_spec.ts | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/core/schematics/migrations/static-queries/google3/explicitQueryTimingRule.ts b/packages/core/schematics/migrations/static-queries/google3/explicitQueryTimingRule.ts index ea3bfa9b1b..004343f6a7 100644 --- a/packages/core/schematics/migrations/static-queries/google3/explicitQueryTimingRule.ts +++ b/packages/core/schematics/migrations/static-queries/google3/explicitQueryTimingRule.ts @@ -14,6 +14,9 @@ import {NgQueryResolveVisitor} from '../angular/ng_query_visitor'; import {QueryTiming} from '../angular/query-definition'; import {getTransformedQueryCallExpr} from '../transform'; +const FAILURE_MESSAGE = 'Query does explicitly specify its timing. Read more here: ' + + 'https://github.com/angular/angular/pull/28810'; + /** * Rule that reports if an Angular "ViewChild" or "ContentChild" query is not explicitly * specifying its timing. The rule also provides TSLint automatic replacements that can @@ -55,10 +58,12 @@ export class Rule extends Rules.TypedRule { // updated call expression node. const fix = new Replacement(queryExpr.getStart(), queryExpr.getWidth(), newText); const timingStr = timing === QueryTiming.STATIC ? 'static' : 'dynamic'; + const failureMessage = `${FAILURE_MESSAGE}. Based on analysis of the query it can be ` + + `marked as "${timingStr}".`; failures.push(new RuleFailure( - sourceFile, queryExpr.getStart(), queryExpr.getWidth(), - `Query is not explicitly marked as "${timingStr}"`, this.ruleName, fix)); + sourceFile, queryExpr.getStart(), queryExpr.getWidth(), failureMessage, this.ruleName, + fix)); }); return failures; diff --git a/packages/core/schematics/test/google3/explicit_query_timing_rule_spec.ts b/packages/core/schematics/test/google3/explicit_query_timing_rule_spec.ts index 97b7c99466..fe456440d2 100644 --- a/packages/core/schematics/test/google3/explicit_query_timing_rule_spec.ts +++ b/packages/core/schematics/test/google3/explicit_query_timing_rule_spec.ts @@ -109,7 +109,7 @@ describe('Google3 explicitQueryTiming TSLint rule', () => { const failures = linter.getResult().failures; expect(failures.length).toBe(1); - expect(failures[0].getFailure()).toContain('Query is not explicitly marked as "static"'); + expect(failures[0].getFailure()).toMatch(/analysis of the query.*"static"/); }); it('should report non-explicit dynamic query definitions', () => { @@ -126,6 +126,6 @@ describe('Google3 explicitQueryTiming TSLint rule', () => { const failures = linter.getResult().failures; expect(failures.length).toBe(1); - expect(failures[0].getFailure()).toContain('Query is not explicitly marked as "dynamic"'); + expect(failures[0].getFailure()).toMatch(/analysis of the query.*"dynamic"/); }); });