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

Add a fixer for the 'curly' rule #3262

Merged
merged 4 commits into from Oct 2, 2017
Merged

Add a fixer for the 'curly' rule #3262

merged 4 commits into from Oct 2, 2017

Conversation

calebegg
Copy link
Contributor

No description provided.

];
} else {
let positionToAddOpenBrace;
let positionToAddCloseBrace = statement.getEnd();
Copy link
Contributor

Choose a reason for hiding this comment

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

this variable is never used

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

Lint.Replacement.appendText(statement.getEnd(), " }"),
];
} else {
let positionToAddOpenBrace;
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be simplified to:

const positionToAddOpenBrace = statement.pos;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

whoa

throw new Error("Unexpected kind of statement");
}

let indentation = "";
Copy link
Contributor

Choose a reason for hiding this comment

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

const match = /\n([\t ])/.exec(node.getFullText(this.sourceFile)); // determine which character to use (tab or space)
const indentation = match === null ? "" : match[1].repeat(ts.getLineAndCharacterOfPosition(node.getStart(this.sourceFile)).character); // indentation should match start of statement

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

return [
Lint.Replacement.appendText(
this.sourceFile.getLineEndOfPosition(positionToAddOpenBrace), " {"),
Lint.Replacement.appendText(statement.getEnd(), `\n${indentation}}`),
Copy link
Contributor

Choose a reason for hiding this comment

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

we should try to infer the correct line ending. See

fix = Lint.Replacement.appendText(length, sourceFile.text[lines[1] - 2] === "\r" ? "\r\n" : "\n");
or
private getEolChar(): string {
const lineEnd = this.sourceFile.getLineEndOfPosition(0);
let newLine;
if (lineEnd > 0) {
if (lineEnd > 1 && this.sourceFile.text[lineEnd - 1] === "\r") {
newLine = "\r\n";
} else if (this.sourceFile.text[lineEnd] === "\n") {
newLine = "\n";
}
}
return newLine === undefined ? ts.sys.newLine : newLine;
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My years away from Windows have softened me.

I tried to add a test but the test system seems to balk at CRLFs.

Copy link
Contributor

Choose a reason for hiding this comment

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

There are already some tests using CRLF. At least the ones in test/rules/linebreak-style/*.
The line endings may be overridden by your editor because of .editorconfig or by git because of .gitattributes.
Your code looks good. I don't think this test case is super important.

}

/** Generate the necessary replacement to add braces to a statement that needs them. */
private addBraceReplacement(statement: ts.Statement, node: ts.IterationStatement | ts.IfStatement, sameLine: boolean) {
Copy link
Contributor

Choose a reason for hiding this comment

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

better name it createMissingBraceFix or similar

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@ajafff ajafff merged commit d797c83 into palantir:master Oct 2, 2017
@ajafff
Copy link
Contributor

ajafff commented Oct 2, 2017

Thanks @calebegg

HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this pull request Apr 9, 2018
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

2 participants