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

Commit

Permalink
cleaning up code by using node line, col, position conversion functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
patsissons committed Apr 29, 2016
1 parent 23df6ce commit 954501f
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions src/enableDisableRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export class EnableDisableRulesWalker extends SkippableTokenAwareRuleWalker {
public visitSourceFile(node: ts.SourceFile) {
super.visitSourceFile(node);
const scan = ts.createScanner(ts.ScriptTarget.ES5, false, ts.LanguageVariant.Standard, node.text);
let lineStartPos = 0;

scanAllTokens(scan, (scanner: ts.Scanner) => {
const startPos = scanner.getStartPos();
Expand All @@ -37,21 +36,23 @@ export class EnableDisableRulesWalker extends SkippableTokenAwareRuleWalker {
return;
}

// keep track of the beginning of the line so that we can use it for same line switches
if (scanner.getToken() === ts.SyntaxKind.NewLineTrivia) {
lineStartPos = startPos + 1;
}

if (scanner.getToken() === ts.SyntaxKind.MultiLineCommentTrivia ||
scanner.getToken() === ts.SyntaxKind.SingleLineCommentTrivia) {
const commentText = scanner.getTokenText();
const startPosition = scanner.getTokenPos();
this.handlePossibleTslintSwitch(commentText, startPosition, lineStartPos, scanner);
// const lineStartPos = node.getPositionOfLineAndCharacter(node.getLineAndCharacterOfPosition(startPos).line, 0);
this.handlePossibleTslintSwitch(commentText, startPosition, node, scanner);
}
});
}

private handlePossibleTslintSwitch(commentText: string, startingPosition: number, lineStartingPosition: number, scanner: ts.Scanner) {
private getStartOfLinePosition(node: ts.SourceFile, position: number, lineDelta = 0) {
return node.getPositionOfLineAndCharacter(
node.getLineAndCharacterOfPosition(position).line + lineDelta, 0
);
}

private handlePossibleTslintSwitch(commentText: string, startingPosition: number, node: ts.SourceFile, scanner: ts.Scanner) {
// regex is: start of string followed by "/*" or "//" followed by any amount of whitespace followed by "tslint:"
if (commentText.match(/^(\/\*|\/\/)\s*tslint:/)) {
const commentTextParts = commentText.split(":");
Expand All @@ -73,42 +74,27 @@ export class EnableDisableRulesWalker extends SkippableTokenAwareRuleWalker {
this.enableDisableRuleMap[ruleToAdd] = [];
}
if (isCurrentLine) {
// start at the line beginning
// start at the beginning of the current line
this.enableDisableRuleMap[ruleToAdd].push({
isEnabled: isEnabled,
position: lineStartingPosition,
position: this.getStartOfLinePosition(node, startingPosition),
});
// end at the end of the comment
// end at the beginning of the next line
this.enableDisableRuleMap[ruleToAdd].push({
isEnabled: !isEnabled,
position: scanner.getTextPos() + 1,
});
} else {
// start at the current position
this.enableDisableRuleMap[ruleToAdd].push({
isEnabled: isEnabled,
position: startingPosition,
});

// end at the beginning of the line following the next line
if (isNextLine) {
const nextLineEndPos = scanner.lookAhead(() => {
// look ahead for the end of the following line
// we need to skip the next new line and the following one
let newLineCount = 2;

while (scanner.scan() !== ts.SyntaxKind.EndOfFileToken) {
if (scanner.getToken() === ts.SyntaxKind.NewLineTrivia) {
if (--newLineCount === 0) {
break;
}
}
}

return scanner.getStartPos() + 1;
});

this.enableDisableRuleMap[ruleToAdd].push({
isEnabled: !isEnabled,
position: nextLineEndPos,
position: this.getStartOfLinePosition(node, startingPosition, 2),
});
}
}
Expand Down

0 comments on commit 954501f

Please sign in to comment.