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

quotemark-rule: single/double options can be at any index in arg list #3114

Merged
merged 2 commits into from Aug 17, 2017

Conversation

aervin
Copy link
Contributor

@aervin aervin commented Aug 10, 2017

  • Addresses an existing issue: #3063
  • New feature, bugfix, or enhancement
    • Includes tests
  • Documentation update

Added a couple util functions to quotemark rule. Checks argument list for OPTION_SINGLE or OPTION_DOUBLE in such a way that argument order doesn't matter. Currently, isEnabled() will return false if no single/double option is specified. Maybe defaulting to double quotes makes more sense?

@@ -141,3 +133,21 @@ function walk(ctx: Lint.WalkContext<Options>) {
ts.forEachChild(node, cb);
});
}

function hasSingleDoublePreference(args: any[]): boolean {
for (const arg of args) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can simply return getQuotemarkPreference(args) !== undefined;

@@ -75,18 +73,12 @@ export class Rule extends Lint.Rules.AbstractRule {
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we remove this method, the rule will default to double quotes? I think that's more reasonable than just doing nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally agree and yes removing that call will set default to double quotes.

}

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}"`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's funny that this code would show the correct warning, but was never executed because the rule was disabled.

function getQuotemarkPreference(args: any[]): string | undefined {
for (const arg of args) {
if (arg === OPTION_SINGLE || arg === OPTION_DOUBLE) {
return (arg === OPTION_SINGLE) ? OPTION_SINGLE : OPTION_DOUBLE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just return arg;

@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2013 Palantir Technologies, Inc.
* Copyright 2017 Palantir Technologies, Inc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one should stay unchanged

@adidahiya adidahiya merged commit 61e40eb into palantir:master Aug 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants