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 2 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
6 changes: 5 additions & 1 deletion src/rules/quotemarkRule.ts
Expand Up @@ -37,6 +37,10 @@ interface Options {
avoidTemplate: boolean;
}

function isQuoteMark(value: QUOTE_MARK | string): value is QUOTE_MARK {
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -120,7 +124,7 @@ function walk(ctx: Lint.WalkContext<Options>) {
: options.quoteMark;
const actualQuoteMark = sourceFile.text[node.end - 1];

if (actualQuoteMark === expectedQuoteMark) {
if (actualQuoteMark === expectedQuoteMark || !isQuoteMark(actualQuoteMark)) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions test/rules/quotemark/double/test.ts.fix
Expand Up @@ -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`;
1 change: 1 addition & 0 deletions test/rules/quotemark/double/test.ts.lint
Expand Up @@ -8,6 +8,7 @@ var tabNewlineWithinSingle = 'tab\tNewline\nWithinSingle';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [' should be "]
'escaped\'quotemark';
~~~~~~~~~~~~~~~~~~~~ [' should be "]
var unbalancedSingle = ('a)
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

// "avoid-template" option is not set.
`foo`;