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

Commit

Permalink
quotemark-rule: single/double options can be at any index in arg list (
Browse files Browse the repository at this point in the history
  • Loading branch information
aervin_ authored and adidahiya committed Aug 17, 2017
1 parent c51e1bd commit 61e40eb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
23 changes: 10 additions & 13 deletions src/rules/quotemarkRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import { isNoSubstitutionTemplateLiteral, isSameLine, isStringLiteral } from "tsutils";
import * as ts from "typescript";

import { showWarningOnce } from "../error";
import * as Lint from "../index";

const OPTION_SINGLE = "single";
Expand Down Expand Up @@ -74,19 +72,9 @@ export class Rule extends Lint.Rules.AbstractRule {
return `${actual} should be ${expected}`;
}

public isEnabled(): boolean {
return super.isEnabled() && (this.ruleArguments[0] === OPTION_SINGLE || this.ruleArguments[0] === OPTION_DOUBLE);
}

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const args = this.ruleArguments;
if (args.length > 0) {
if (args[0] !== OPTION_SINGLE && args[0] !== OPTION_DOUBLE) {
showWarningOnce(`Warning: First argument to 'quotemark' rule should be "${OPTION_SINGLE}" or "${OPTION_DOUBLE}"`);
return [];
}
}
const quoteMark = args[0] === OPTION_SINGLE ? "'" : '"';
const quoteMark = getQuotemarkPreference(args) === OPTION_SINGLE ? "'" : '"';
return this.applyWithFunction(sourceFile, walk, {
avoidEscape: hasArg(OPTION_AVOID_ESCAPE),
avoidTemplate: hasArg(OPTION_AVOID_TEMPLATE),
Expand Down Expand Up @@ -141,3 +129,12 @@ function walk(ctx: Lint.WalkContext<Options>) {
ts.forEachChild(node, cb);
});
}

function getQuotemarkPreference(args: any[]): string | undefined {
for (const arg of args) {
if (arg === OPTION_SINGLE || arg === OPTION_DOUBLE) {
return arg as string;
}
}
return undefined;
}
2 changes: 1 addition & 1 deletion test/rules/quotemark/avoid-template/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rules": {
"quotemark": [true, "double", "avoid-escape", "avoid-template"]
"quotemark": [true, "avoid-escape", "double", "avoid-template"]
}
}
2 changes: 1 addition & 1 deletion test/rules/quotemark/jsx-double/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rules": {
"quotemark": [true, "single", "jsx-double"]
"quotemark": [true,"jsx-double", "single"]
}
}
2 changes: 1 addition & 1 deletion test/rules/quotemark/single-avoid-escape/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rules": {
"quotemark": [true, "single", "avoid-escape"]
"quotemark": [true, "avoid-escape", "single"]
}
}

0 comments on commit 61e40eb

Please sign in to comment.