Skip to content

Commit

Permalink
Ensure backwards compatibility of palantir#2036 (palantir#2055)
Browse files Browse the repository at this point in the history
This commit can be reverted once tslint drops support for typescript 2.0.10

Fixes palantir#2054
  • Loading branch information
ajafff authored and nchen63 committed Jan 18, 2017
1 parent 15ed0fe commit d0322f4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/_data/rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -1744,7 +1744,7 @@
"ruleName": "whitespace",
"description": "Enforces whitespace style conventions.",
"rationale": "Helps maintain a readable, consistent style in your codebase.",
"optionsDescription": "\nSeven arguments may be optionally provided:\n\n* `\"check-branch\"` checks branching statements (`if`/`else`/`for`/`while`) are followed by whitespace.\n* `\"check-decl\"`checks that variable declarations have whitespace around the equals token.\n* `\"check-operator\"` checks for whitespace around operator tokens.\n* `\"check-module\"` checks for whitespace in import & export statements.\n* `\"check-separator\"` checks for whitespace after separator tokens (`,`/`;`).\n* `\"check-type\"` checks for whitespace before a variable type specification.\n* `\"check-typecast\"` checks for whitespace between a typecast and its target.\n* `\"check-preblock\"` checks for whitespace before the opening brace of a block",
"optionsDescription": "\nEight arguments may be optionally provided:\n\n* `\"check-branch\"` checks branching statements (`if`/`else`/`for`/`while`) are followed by whitespace.\n* `\"check-decl\"`checks that variable declarations have whitespace around the equals token.\n* `\"check-operator\"` checks for whitespace around operator tokens.\n* `\"check-module\"` checks for whitespace in import & export statements.\n* `\"check-separator\"` checks for whitespace after separator tokens (`,`/`;`).\n* `\"check-type\"` checks for whitespace before a variable type specification.\n* `\"check-typecast\"` checks for whitespace between a typecast and its target.\n* `\"check-preblock\"` checks for whitespace before the opening brace of a block",
"options": {
"type": "array",
"items": {
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/whitespace/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
rationale: 'Helps maintain a readable, consistent style in your codebase.'
optionsDescription: |-

Seven arguments may be optionally provided:
Eight arguments may be optionally provided:

* `"check-branch"` checks branching statements (`if`/`else`/`for`/`while`) are followed by whitespace.
* `"check-decl"`checks that variable declarations have whitespace around the equals token.
Expand Down
28 changes: 18 additions & 10 deletions src/language/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ export function forEachToken(node: ts.Node, skipTrivia: boolean, cb: ForEachToke
}

function iterateChildren(child: ts.Node): void {
if (child.kind < ts.SyntaxKind.FirstNode) {
if (child.kind < ts.SyntaxKind.FirstNode ||
// for backwards compatibility to typescript 2.0.10
// JsxText was no Token, but a Node in that version
child.kind === ts.SyntaxKind.JsxText) {
// we found a token, tokens have no children, stop recursing here
return callback(child);
}
Expand Down Expand Up @@ -341,13 +344,20 @@ export function forEachComment(node: ts.Node, cb: ForEachCommentCallback) {
// don't search for comments inside JsxText
if (canHaveLeadingTrivia(tokenKind, parent)) {
// Comments before the first token (pos.fullStart === 0) are all considered leading comments, so no need for special treatment
ts.forEachLeadingCommentRange(fullText, pos.fullStart, commentCallback);
const comments = ts.getLeadingCommentRanges(fullText, pos.fullStart);
if (comments !== undefined) {
for (const comment of comments) {
cb(fullText, comment.kind, {fullStart: pos.fullStart, tokenStart: comment.pos, end: comment.end});
}
}
}
if (canHaveTrailingTrivia(tokenKind, parent)) {
ts.forEachTrailingCommentRange(fullText, pos.end, commentCallback);
}
function commentCallback(tokenStart: number, end: number, kind: ts.SyntaxKind) {
cb(fullText, kind, {tokenStart, end, fullStart: pos.fullStart});
const comments = ts.getTrailingCommentRanges(fullText, pos.end);
if (comments !== undefined) {
for (const comment of comments) {
cb(fullText, comment.kind, {fullStart: pos.fullStart, tokenStart: comment.pos, end: comment.end});
}
}
}
});
}
Expand Down Expand Up @@ -402,8 +412,6 @@ function canHaveTrailingTrivia(tokenKind: ts.SyntaxKind, parent: ts.Node): boole
* This value is typically obtained from `node.getFullStart()` or `node.getEnd()`
*/
export function hasCommentAfterPosition(text: string, position: number): boolean {
const cb = () => true;
return ts.forEachTrailingCommentRange(text, position, cb) ||
ts.forEachLeadingCommentRange(text, position, cb) ||
false; // return boolean instead of undefined if no comment is found
return ts.getTrailingCommentRanges(text, position) !== undefined ||
ts.getTrailingCommentRanges(text, position) !== undefined;
}

0 comments on commit d0322f4

Please sign in to comment.