From f0214621ee97df220754fcf2ad53a91945f890a2 Mon Sep 17 00:00:00 2001 From: Isabelle Weber Date: Thu, 6 Dec 2018 08:35:15 +1100 Subject: [PATCH 1/4] Add quotemark type validation to allow early return (#4310) --- src/rules/quotemarkRule.ts | 6 +++++- test/rules/quotemark/double/test.ts.fix | 1 + test/rules/quotemark/double/test.ts.lint | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/rules/quotemarkRule.ts b/src/rules/quotemarkRule.ts index 7193f54738c..a09d8353b3d 100644 --- a/src/rules/quotemarkRule.ts +++ b/src/rules/quotemarkRule.ts @@ -37,6 +37,10 @@ interface Options { avoidTemplate: boolean; } +function isQuoteMark(value: QUOTE_MARK | any): value is QUOTE_MARK { + return ["'", '"', "`"].indexOf(value) > -1; +} + export class Rule extends Lint.Rules.AbstractRule { /* tslint:disable:object-literal-sort-keys */ public static metadata: Lint.IRuleMetadata = { @@ -120,7 +124,7 @@ function walk(ctx: Lint.WalkContext) { : options.quoteMark; const actualQuoteMark = sourceFile.text[node.end - 1]; - if (actualQuoteMark === expectedQuoteMark) { + if (actualQuoteMark === expectedQuoteMark || !isQuoteMark(actualQuoteMark)) { return; } diff --git a/test/rules/quotemark/double/test.ts.fix b/test/rules/quotemark/double/test.ts.fix index 77531382b02..96dac17f919 100644 --- a/test/rules/quotemark/double/test.ts.fix +++ b/test/rules/quotemark/double/test.ts.fix @@ -4,6 +4,7 @@ var singleWithinDouble = "'singleWithinDouble'"; var doubleWithinSingle = "\"doubleWithinSingle\""; var tabNewlineWithinSingle = "tab\tNewline\nWithinSingle"; "escaped'quotemark"; +var unbalancedSingle = ('a) // "avoid-template" option is not set. `foo`; diff --git a/test/rules/quotemark/double/test.ts.lint b/test/rules/quotemark/double/test.ts.lint index 32fc8dd6f34..ee00375415b 100644 --- a/test/rules/quotemark/double/test.ts.lint +++ b/test/rules/quotemark/double/test.ts.lint @@ -8,6 +8,7 @@ var tabNewlineWithinSingle = 'tab\tNewline\nWithinSingle'; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [' should be "] 'escaped\'quotemark'; ~~~~~~~~~~~~~~~~~~~~ [' should be "] +var unbalancedSingle = ('a) // "avoid-template" option is not set. `foo`; From 04228ff8a751b088147f3e4a25c1e4a7a3ac03c7 Mon Sep 17 00:00:00 2001 From: Isabelle Weber Date: Thu, 6 Dec 2018 09:12:53 +1100 Subject: [PATCH 2/4] Fix no-unsafe-any linting error in quotemark rule --- src/rules/quotemarkRule.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rules/quotemarkRule.ts b/src/rules/quotemarkRule.ts index a09d8353b3d..63f77794566 100644 --- a/src/rules/quotemarkRule.ts +++ b/src/rules/quotemarkRule.ts @@ -37,7 +37,7 @@ interface Options { avoidTemplate: boolean; } -function isQuoteMark(value: QUOTE_MARK | any): value is QUOTE_MARK { +function isQuoteMark(value: QUOTE_MARK | string): value is QUOTE_MARK { return ["'", '"', "`"].indexOf(value) > -1; } From 551975a22b65bc09d1fa83930e74c5cce4f88479 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 16 Jun 2019 19:52:31 -0400 Subject: [PATCH 3/4] Separate invalid quote tests into new tests --- test/rules/quotemark/double/test.ts.fix | 1 - test/rules/quotemark/double/test.ts.lint | 1 - 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 +++++ 8 files changed, 20 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/test/rules/quotemark/double/test.ts.fix b/test/rules/quotemark/double/test.ts.fix index 96dac17f919..77531382b02 100644 --- a/test/rules/quotemark/double/test.ts.fix +++ b/test/rules/quotemark/double/test.ts.fix @@ -4,7 +4,6 @@ var singleWithinDouble = "'singleWithinDouble'"; var doubleWithinSingle = "\"doubleWithinSingle\""; var tabNewlineWithinSingle = "tab\tNewline\nWithinSingle"; "escaped'quotemark"; -var unbalancedSingle = ('a) // "avoid-template" option is not set. `foo`; diff --git a/test/rules/quotemark/double/test.ts.lint b/test/rules/quotemark/double/test.ts.lint index ee00375415b..32fc8dd6f34 100644 --- a/test/rules/quotemark/double/test.ts.lint +++ b/test/rules/quotemark/double/test.ts.lint @@ -8,7 +8,6 @@ var tabNewlineWithinSingle = 'tab\tNewline\nWithinSingle'; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [' should be "] 'escaped\'quotemark'; ~~~~~~~~~~~~~~~~~~~~ [' should be "] -var unbalancedSingle = ('a) // "avoid-template" option is not set. `foo`; 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"] + } +} From f27ff5a7100f7ed30c921755a89cabcb8073f731 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 16 Jun 2019 19:59:09 -0400 Subject: [PATCH 4/4] Fixed introduced build break --- src/rules/quotemarkRule.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rules/quotemarkRule.ts b/src/rules/quotemarkRule.ts index 45f63b43c09..ab6de0e9021 100644 --- a/src/rules/quotemarkRule.ts +++ b/src/rules/quotemarkRule.ts @@ -51,7 +51,7 @@ interface Options { avoidTemplate: boolean; } -function isQuoteMark(value: QUOTE_MARK | string): value is QUOTE_MARK { +function isQuoteMark(value: string): value is QUOTEMARK { return ["'", '"', "`"].indexOf(value) > -1; }