-
Notifications
You must be signed in to change notification settings - Fork 886
Conversation
src/rules/curlyRule.ts
Outdated
]; | ||
} else { | ||
let positionToAddOpenBrace; | ||
let positionToAddCloseBrace = statement.getEnd(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
src/rules/curlyRule.ts
Outdated
Lint.Replacement.appendText(statement.getEnd(), " }"), | ||
]; | ||
} else { | ||
let positionToAddOpenBrace; |
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whoa
src/rules/curlyRule.ts
Outdated
throw new Error("Unexpected kind of statement"); | ||
} | ||
|
||
let indentation = ""; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/rules/curlyRule.ts
Outdated
return [ | ||
Lint.Replacement.appendText( | ||
this.sourceFile.getLineEndOfPosition(positionToAddOpenBrace), " {"), | ||
Lint.Replacement.appendText(statement.getEnd(), `\n${indentation}}`), |
There was a problem hiding this comment.
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
tslint/src/rules/eoflineRule.ts
Line 49 in 02dd308
fix = Lint.Replacement.appendText(length, sourceFile.text[lines[1] - 2] === "\r" ? "\r\n" : "\n"); |
tslint/src/rules/orderedImportsRule.ts
Lines 365 to 377 in 02dd308
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; | |
} | |
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
src/rules/curlyRule.ts
Outdated
} | ||
|
||
/** 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) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Thanks @calebegg |
No description provided.