Skip to content

Commit

Permalink
Enable stricter TS compiler options (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
adidahiya authored Jan 10, 2017
1 parent 8b680d3 commit 29fb2f7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 49 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"glob": "^7.0.3",
"npm-run-all": "^2.1.2",
"path": "^0.12.7",
"tslint": "^4.0.1",
"typescript": "^2.0.10"
"tslint": "^4.3.1",
"typescript": "^2.1.4"
}
}
76 changes: 37 additions & 39 deletions src/rules/jsxCurlySpacingRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,11 @@ import * as ts from "typescript";
const OPTION_ALWAYS = "always";
const OPTION_NEVER = "never";
const SPACING_VALUES = [OPTION_ALWAYS, OPTION_NEVER];
/* tslint:disable:object-literal-sort-keys */
const SPACING_OBJECT = {
type: "string",
enum: SPACING_VALUES,
type: "string",
};
/* tslint:enable:object-literal-sort-keys */
const newLineRegexp = /\n/;

function isExpressionMultiline(text: string) {
return newLineRegexp.test(text);
}

function getTokensCombinedText(firstToken: ts.Node, nextToken: ts.Node) {
const parentNodeText = nextToken.parent.getText();
const firstTokenText = firstToken.getText();
const secondTokenText = nextToken.getText();
const secondTokenTextLocation = parentNodeText.indexOf(secondTokenText);
const firstTokenTextLocation = parentNodeText.indexOf(firstTokenText);
const combinedTokeText = parentNodeText.slice(
firstTokenTextLocation,
secondTokenTextLocation + secondTokenText.length);

return combinedTokeText;
}

function getTotalCharCount(comments: ts.CommentRange[]) {
return comments
.map((comment) => comment.end - comment.pos)
.reduce((l, r) => l + r, 0);
}
const NEWLINE_REGEX = /\n/;

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
Expand Down Expand Up @@ -102,7 +77,6 @@ export class Rule extends Lint.Rules.AbstractRule {
class JsxCurlySpacingWalker extends Lint.RuleWalker {
protected visitJsxExpression(node: ts.JsxExpression) {
this.validateBraceSpacing(node);

super.visitJsxExpression(node);
}

Expand All @@ -122,7 +96,7 @@ class JsxCurlySpacingWalker extends Lint.RuleWalker {
const nodeWidth = node.getWidth();

if (this.hasOption(OPTION_ALWAYS)) {
let deleteFix = this.getDeleteFixForSpaceBetweenTokens(firstToken, secondToken);
let deleteFix = this.maybeGetDeleteFixForSpaceBetweenTokens(firstToken, secondToken);
if (deleteFix === undefined) {
const fix = new Lint.Fix(Rule.metadata.ruleName, [
this.appendText(secondToken.getFullStart(), " "),
Expand All @@ -132,7 +106,7 @@ class JsxCurlySpacingWalker extends Lint.RuleWalker {
this.addFailure(this.createFailure(nodeStart, 1, failureString, fix));
}

deleteFix = this.getDeleteFixForSpaceBetweenTokens(secondToLastToken, lastToken);
deleteFix = this.maybeGetDeleteFixForSpaceBetweenTokens(secondToLastToken, lastToken);
if (deleteFix === undefined) {
const fix = new Lint.Fix(Rule.metadata.ruleName, [
this.appendText(lastToken.getStart(), " "),
Expand All @@ -146,7 +120,7 @@ class JsxCurlySpacingWalker extends Lint.RuleWalker {
const lastAndSecondToLastCombinedText = getTokensCombinedText(secondToLastToken, lastToken);

if (!isExpressionMultiline(firstAndSecondTokensCombinedText)) {
const fix = this.getDeleteFixForSpaceBetweenTokens(firstToken, secondToken);
const fix = this.maybeGetDeleteFixForSpaceBetweenTokens(firstToken, secondToken);
if (fix !== undefined) {
const failureString = Rule.FAILURE_FORBIDDEN_SPACES_BEGINNING(firstToken.getText());

Expand All @@ -155,32 +129,32 @@ class JsxCurlySpacingWalker extends Lint.RuleWalker {
}

if (!isExpressionMultiline(lastAndSecondToLastCombinedText)) {
const fix = this.getDeleteFixForSpaceBetweenTokens(secondToLastToken, lastToken);
const fix = this.maybeGetDeleteFixForSpaceBetweenTokens(secondToLastToken, lastToken);
if (fix !== undefined) {
const failureString = Rule.FAILURE_FORBIDDEN_SPACES_END(lastToken.getText());
// degenerate case when firstToken is the same as the secondToLastToken as we would
// have already queued up a fix in the previous branch, do not apply fix
const failure = firstToken === secondToLastToken ?
this.createFailure(nodeStart + nodeWidth - 1, 1, failureString) :
this.createFailure(nodeStart + nodeWidth - 1, 1, failureString, fix);
const failure = firstToken === secondToLastToken
? this.createFailure(nodeStart + nodeWidth - 1, 1, failureString)
: this.createFailure(nodeStart + nodeWidth - 1, 1, failureString, fix);
this.addFailure(failure);
}
}
}
}

private getDeleteFixForSpaceBetweenTokens(firstNode: ts.Node, secondNode: ts.Node) {
private maybeGetDeleteFixForSpaceBetweenTokens(firstNode: ts.Node, secondNode: ts.Node) {
if (firstNode.parent !== secondNode.parent) {
throw Error("Expected identical parents for both nodes");
}

const parent = firstNode.parent;
const parentStart = parent.getStart();
const { parent } = firstNode;
const parentStart = parent!.getStart();
const secondNodeStart = secondNode.getFullStart();
const firstNodeEnd = firstNode.getStart() + firstNode.getWidth();
const secondNodeRelativeStart = secondNodeStart - parentStart;
const firstNodeRelativeEnd = firstNodeEnd - parentStart;
const parentText = parent.getText();
const parentText = parent!.getText();
const trailingComments = ts.getTrailingCommentRanges(parentText, firstNodeRelativeEnd) || [];
const leadingComments = ts.getLeadingCommentRanges(parentText, secondNodeRelativeStart) || [];
const comments = trailingComments.concat(leadingComments);
Expand All @@ -195,3 +169,27 @@ class JsxCurlySpacingWalker extends Lint.RuleWalker {
}
}
}

function isExpressionMultiline(text: string) {
return NEWLINE_REGEX.test(text);
}

function getTokensCombinedText(firstToken: ts.Node, nextToken: ts.Node) {
const parentNodeText = nextToken.parent!.getText();
const firstTokenText = firstToken.getText();
const secondTokenText = nextToken.getText();
const secondTokenTextLocation = parentNodeText.indexOf(secondTokenText);
const firstTokenTextLocation = parentNodeText.indexOf(firstTokenText);
const combinedTokeText = parentNodeText.slice(
firstTokenTextLocation,
secondTokenTextLocation + secondTokenText.length,
);

return combinedTokeText;
}

function getTotalCharCount(comments: ts.CommentRange[]) {
return comments
.map((comment) => comment.end - comment.pos)
.reduce((l, r) => l + r, 0);
}
1 change: 1 addition & 0 deletions src/rules/jsxNoStringRefRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class JsxNoStringRefWalker extends Lint.RuleWalker {
const hasStringInitializer = initializer.kind === ts.SyntaxKind.StringLiteral;
const hasStringExpressionInitializer =
nodeIsKind<ts.JsxExpression>(initializer, ts.SyntaxKind.JsxExpression)
&& initializer.expression !== undefined
&& (initializer.expression.kind === ts.SyntaxKind.StringLiteral
|| initializer.expression.kind === ts.SyntaxKind.TemplateExpression);

Expand Down
18 changes: 12 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
{
"version": "1.8.10",
"version": "2.1.4",
"compilerOptions": {
"declaration": true,
"lib": ["es6"],
"module": "commonjs",
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"declaration": true,
"sourceMap": false,
"target": "es5",
"rootDir": "src/",
"noImplicitThis": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "build/",
"lib": ["es6"]
"rootDir": "src/",
"sourceMap": false,
"strictNullChecks": true,
"target": "es5"
},
"exclude": [
"build",
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ timed-out@^3.0.0:
version "3.1.3"
resolved "https://registry.yarnpkg.com/timed-out/-/timed-out-3.1.3.tgz#95860bfcc5c76c277f8f8326fd0f5b2e20eba217"

tslint@^4.0.1:
tslint@^4.3.1:
version "4.3.1"
resolved "https://registry.yarnpkg.com/tslint/-/tslint-4.3.1.tgz#28f679c53ca4b273688bcb6fcf0dde7ff1bb2169"
dependencies:
Expand All @@ -782,7 +782,7 @@ tslint@^4.0.1:
underscore.string "^3.3.4"
update-notifier "^1.0.2"

typescript@^2.0.10:
typescript@^2.1.4:
version "2.1.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.1.4.tgz#b53b69fb841126acb1dd4b397d21daba87572251"

Expand Down

0 comments on commit 29fb2f7

Please sign in to comment.