Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Add quotemark type validation to allow early return (#4310) #4344

Merged
merged 5 commits into from Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/rules/quotemarkRule.ts
Expand Up @@ -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 = {
Expand Down Expand Up @@ -140,8 +144,8 @@ function walk(ctx: Lint.WalkContext<Options>) {
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;
}

Expand Down
2 changes: 2 additions & 0 deletions test/rules/quotemark/invalid-double/test.ts.fix
@@ -0,0 +1,2 @@
var single = "single";
var unbalancedSingleAfter = ('a)
3 changes: 3 additions & 0 deletions test/rules/quotemark/invalid-double/test.ts.lint
@@ -0,0 +1,3 @@
var single = 'single';
~~~~~~~~ [' should be "]
var unbalancedSingleAfter = ('a)
5 changes: 5 additions & 0 deletions test/rules/quotemark/invalid-double/tslint.json
@@ -0,0 +1,5 @@
{
"rules": {
"quotemark": [true, "double"]
}
}
2 changes: 2 additions & 0 deletions test/rules/quotemark/invalid-single/test.ts.fix
@@ -0,0 +1,2 @@
var single = 'single';
var unbalancedSingleAfter = ("a)
3 changes: 3 additions & 0 deletions test/rules/quotemark/invalid-single/test.ts.lint
@@ -0,0 +1,3 @@
var single = "single";
~~~~~~~~ [" should be ']
var unbalancedSingleAfter = ("a)
5 changes: 5 additions & 0 deletions test/rules/quotemark/invalid-single/tslint.json
@@ -0,0 +1,5 @@
{
"rules": {
"quotemark": [true, "single"]
}
}