From 48c1df89a98b2114a7d610a7d6e03b21022871ee Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Mon, 16 Jan 2017 22:16:15 +0100 Subject: [PATCH] Ensure backwards compatibility of #2036 This commit can be reverted once tslint drops support for typescript 2.0.10 Fixes #2054 --- docs/_data/rules.json | 2 +- docs/rules/whitespace/index.html | 2 +- src/language/utils.ts | 28 ++++++++++++++++++---------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/docs/_data/rules.json b/docs/_data/rules.json index c8e76a99a14..44c43320380 100644 --- a/docs/_data/rules.json +++ b/docs/_data/rules.json @@ -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": { diff --git a/docs/rules/whitespace/index.html b/docs/rules/whitespace/index.html index 97d6f7bfb29..c3f685c206a 100644 --- a/docs/rules/whitespace/index.html +++ b/docs/rules/whitespace/index.html @@ -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. diff --git a/src/language/utils.ts b/src/language/utils.ts index 151a35db694..f9d31bec4c0 100644 --- a/src/language/utils.ts +++ b/src/language/utils.ts @@ -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); } @@ -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}); + } + } } }); } @@ -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; }