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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/rules/quotemarkRule.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var single = "single";
var unbalancedSingleAfter = ('a)
3 changes: 3 additions & 0 deletions test/rules/quotemark/invalid-double/test.ts.lint
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"quotemark": [true, "double"]
}
}
2 changes: 2 additions & 0 deletions test/rules/quotemark/invalid-single/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var single = 'single';
var unbalancedSingleAfter = ("a)
3 changes: 3 additions & 0 deletions test/rules/quotemark/invalid-single/test.ts.lint
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"quotemark": [true, "single"]
}
}