Skip to content

Commit

Permalink
Fix ts 2.x issue with literal spacing rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
abierbaum committed Dec 16, 2016
1 parent af125ca commit b93a2c8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"request": "launch",
"program": "${workspaceRoot}/node_modules/.bin/tslint",
"stopOnEntry": false,
"args": ["--test", "test/rules/ext-variable-name/default"],
"args": ["--test", "test/rules/literal-spacing/never"],
"cwd": "${workspaceRoot}",
"preLaunchTask": "build",
"runtimeExecutable": null,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
"tslint": "^4.1.1"
},
"devDependencies": {
"typescript": "~2.1.4"
"typescript": "~2.1.0"
}
}
26 changes: 15 additions & 11 deletions rules/literalSpacingRule.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 17 additions & 13 deletions rules/literalSpacingRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,32 +94,36 @@ class LiteralSpacingRuleWalker extends Lint.RuleWalker {
}
// const first_child = children[0]; // " ["
const second_child = children[1]; // " 1, 3, 5,"
const last_child = children[children.length - 1]; // " ]"
const last_child = children[children.length - 1]; // " ]"
const is_empty = second_child.getText() === "";

let [front_start, front_end] = [second_child.getFullStart(), second_child.getStart()];
let [back_start, back_end] = [last_child.getFullStart(), last_child.getStart()];

let front_text = this.getSourceText(front_start, front_end);
let back_text = this.getSourceText(back_start, back_end);

function doCheck(text: string, start: number, end: number) {
// outerText: the string between the full edge and the start of the node content
// nodeEdgeChar: the first character of the node content
function doCheck(edgeText: string, nodeEdgeChar: string, start: number, end: number) {
if ((NEVER_OPT === checkType) &&
(text.length > 0) &&
(text.indexOf("\n") === -1)) {
self.addFailure(self.createFailure(start, (end - start), NEVER_FAIL));
((edgeText.length > 0) || (nodeEdgeChar === " ")) &&
(edgeText.indexOf("\n") === -1)) {
const len = Math.max((end - start), 1);
self.addFailure(self.createFailure(start, len, NEVER_FAIL));
}

// if the space text is empty and second child has text (ie. there is a body)
// (this handles the case of {} and [])
if ((ALWAYS_OPT === checkType) &&
(text.length === 0) &&
(second_child.getText() !== "")) {
self.addFailure(self.createFailure(start, 1, ALWAYS_FAIL));
if (ALWAYS_OPT === checkType) {
if((!is_empty) &&
(edgeText.length === 0) &&
(nodeEdgeChar !== " ")) {
self.addFailure(self.createFailure(start, 1, ALWAYS_FAIL));
}
}
}

doCheck(front_text, front_start, front_end);
doCheck(back_text, back_start, back_end);
doCheck(front_text, second_child.getText()[0], front_start, front_end);
doCheck(back_text, last_child.getText()[-1], back_start, back_end);
}

protected getSourceText(pos: number, end: number): string {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ tslint@^4.1.1:
underscore.string "^3.3.4"
update-notifier "^1.0.2"

typescript@~2.1.4:
typescript@~2.1.0:
version "2.1.4"
resolved "http://yogi.priority5.com:9090/typescript/-/typescript-2.1.4.tgz#b53b69fb841126acb1dd4b397d21daba87572251"

Expand Down

0 comments on commit b93a2c8

Please sign in to comment.