From db042318491536b0c382c76d49de6d10de24d266 Mon Sep 17 00:00:00 2001 From: Isabelle Date: Mon, 17 Jun 2019 10:07:07 +1000 Subject: [PATCH] Add quotemark type validation to allow early return (#4310) (#4344) * Add quotemark type validation to allow early return (#4310) * Fix no-unsafe-any linting error in quotemark rule * Separate invalid quote tests into new tests * Fixed introduced build break --- src/rules/quotemarkRule.ts | 8 ++++++-- test/rules/quotemark/invalid-double/test.ts.fix | 2 ++ test/rules/quotemark/invalid-double/test.ts.lint | 3 +++ test/rules/quotemark/invalid-double/tslint.json | 5 +++++ test/rules/quotemark/invalid-single/test.ts.fix | 2 ++ test/rules/quotemark/invalid-single/test.ts.lint | 3 +++ test/rules/quotemark/invalid-single/tslint.json | 5 +++++ 7 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 test/rules/quotemark/invalid-double/test.ts.fix create mode 100644 test/rules/quotemark/invalid-double/test.ts.lint create mode 100644 test/rules/quotemark/invalid-double/tslint.json create mode 100644 test/rules/quotemark/invalid-single/test.ts.fix create mode 100644 test/rules/quotemark/invalid-single/test.ts.lint create mode 100644 test/rules/quotemark/invalid-single/tslint.json diff --git a/src/rules/quotemarkRule.ts b/src/rules/quotemarkRule.ts index d98b4b586c4..ab6de0e9021 100644 --- a/src/rules/quotemarkRule.ts +++ b/src/rules/quotemarkRule.ts @@ -51,6 +51,10 @@ interface Options { avoidTemplate: boolean; } +function isQuoteMark(value: string): value is QUOTEMARK { + return ["'", '"', "`"].indexOf(value) > -1; +} + export class Rule extends Lint.Rules.AbstractRule { /* tslint:disable:object-literal-sort-keys */ public static metadata: Lint.IRuleMetadata = { @@ -140,8 +144,8 @@ function walk(ctx: Lint.WalkContext) { return; } - // We already have the expected quotemark. Done. - if (actualQuotemark === expectedQuotemark) { + // We already have the expected quotemark, or the quotemark is invalid. Done. + if (actualQuotemark === expectedQuotemark || !isQuoteMark(actualQuotemark)) { return; } diff --git a/test/rules/quotemark/invalid-double/test.ts.fix b/test/rules/quotemark/invalid-double/test.ts.fix new file mode 100644 index 00000000000..1e5ab6769e1 --- /dev/null +++ b/test/rules/quotemark/invalid-double/test.ts.fix @@ -0,0 +1,2 @@ +var single = "single"; +var unbalancedSingleAfter = ('a) diff --git a/test/rules/quotemark/invalid-double/test.ts.lint b/test/rules/quotemark/invalid-double/test.ts.lint new file mode 100644 index 00000000000..8bf8e15d4f9 --- /dev/null +++ b/test/rules/quotemark/invalid-double/test.ts.lint @@ -0,0 +1,3 @@ +var single = 'single'; + ~~~~~~~~ [' should be "] +var unbalancedSingleAfter = ('a) diff --git a/test/rules/quotemark/invalid-double/tslint.json b/test/rules/quotemark/invalid-double/tslint.json new file mode 100644 index 00000000000..9318e0d53e4 --- /dev/null +++ b/test/rules/quotemark/invalid-double/tslint.json @@ -0,0 +1,5 @@ +{ + "rules": { + "quotemark": [true, "double"] + } +} diff --git a/test/rules/quotemark/invalid-single/test.ts.fix b/test/rules/quotemark/invalid-single/test.ts.fix new file mode 100644 index 00000000000..16e11a81288 --- /dev/null +++ b/test/rules/quotemark/invalid-single/test.ts.fix @@ -0,0 +1,2 @@ +var single = 'single'; +var unbalancedSingleAfter = ("a) diff --git a/test/rules/quotemark/invalid-single/test.ts.lint b/test/rules/quotemark/invalid-single/test.ts.lint new file mode 100644 index 00000000000..e5525bef1ce --- /dev/null +++ b/test/rules/quotemark/invalid-single/test.ts.lint @@ -0,0 +1,3 @@ +var single = "single"; + ~~~~~~~~ [" should be '] +var unbalancedSingleAfter = ("a) diff --git a/test/rules/quotemark/invalid-single/tslint.json b/test/rules/quotemark/invalid-single/tslint.json new file mode 100644 index 00000000000..4a5f880d8ab --- /dev/null +++ b/test/rules/quotemark/invalid-single/tslint.json @@ -0,0 +1,5 @@ +{ + "rules": { + "quotemark": [true, "single"] + } +}