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

indent: support size and fix #2723

Merged
merged 3 commits into from May 11, 2017
Merged

Conversation

filipesilva
Copy link
Contributor

@filipesilva filipesilva commented May 9, 2017

PR checklist

Overview of change:

This PR adds indent size support to the indent rule (defaults to 4), and also allows auto fixing.

Is there anything you'd like reviewers to focus on?

Correctness of the auto-fix.

CHANGELOG.md entry:

[new-rule-option][new-fixer] indent support size and fix

@palantirtech
Copy link
Member

Thanks for your interest in palantir/tslint, @filipesilva! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request.

This PR adds indent size support to the `indent` rule, and also allows auto fixing.

Fix palantir#581
Fix palantir#2350

A second optional argument specifies indentation size, defaulting to 4:

* \`${OPTION_INDENT_SIZE_2.toString()}\` enforces 2 space indentation.
Copy link
Contributor

Choose a reason for hiding this comment

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

is .toString() really needed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that surprised me as well! I thought it wasn't necessary but TS is telling me it is:
image


constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
super(sourceFile, options);

if (this.hasOption(OPTION_INDENT_SIZE_2)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

the docs say it has to be the second option. so you can just use this.getOptions().ruleArguments[1] instead of this.hasOption(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sgtm, will also remove the hasOption change then.

} else if (this.hasOption(OPTION_INDENT_SIZE_4)) {
this.size = OPTION_INDENT_SIZE_4;
} else {
this.size = OPTION_INDENT_SIZE_DEFAULT;
Copy link
Contributor

Choose a reason for hiding this comment

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

how do I tell the rule not to mess with my indentation level if it defaults to 4?
I'd suggest not to fix anything if there is no indentSize specified.

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 agree, not fixing anything is a better default now that I think about it.


A second optional argument specifies indentation size:

* \`${OPTION_INDENT_SIZE_2.toString()}\` enforces 2 space indentation.
Copy link
Contributor

Choose a reason for hiding this comment

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

ts requires toString here because the dedent function takes an array of strings. We can probably make it take in (string | number)[] later

size = OPTION_INDENT_SIZE_2;
} else if (this.getOptions()[1] === OPTION_INDENT_SIZE_4) {
size = OPTION_INDENT_SIZE_4;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

probably shouldn't do fixes if 2nd arg is an invalid number or type

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

},
optionExamples: [[true, "spaces"]],
optionExamples: [
[true, "spaces", 4],
Copy link
Contributor

Choose a reason for hiding this comment

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

can replace "string" with const

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@filipesilva filipesilva force-pushed the indent-fix branch 3 times, most recently from 5dbe5fc to fbf4773 Compare May 11, 2017 09:04
@nchen63 nchen63 merged commit 26c0675 into palantir:master May 11, 2017
@nchen63
Copy link
Contributor

nchen63 commented May 11, 2017

@filipesilva thanks!

@filipesilva filipesilva deleted the indent-fix branch May 11, 2017 11:39
@filipesilva
Copy link
Contributor Author

Awesome! We this change in, we're thinking of using TSLint in Angular CLI to enable quotes/indentation auto fixing on file generation. It's something our users have been asking for a while.

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

5 participants