Skip to content

Commit

Permalink
Add quotemark type validation to allow early return (palantir#4310) (p…
Browse files Browse the repository at this point in the history
…alantir#4344)

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

* Fix no-unsafe-any linting error in quotemark rule

* Separate invalid quote tests into new tests

* Fixed introduced build break
  • Loading branch information
isabelleweber authored and Josh Goldberg committed Jun 17, 2019
1 parent 9d6d49a commit db04231
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 2 deletions.
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"]
}
}

0 comments on commit db04231

Please sign in to comment.