Skip to content

Commit

Permalink
[WIP] getting rid of strict null checks errors
Browse files Browse the repository at this point in the history
All tests are passing with these changes, 70 errors remain.
  • Loading branch information
Manuel Lopez committed May 6, 2017
1 parent dc9518c commit dec451b
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"gulp-sourcemaps": "^1.6.0",
"gulp-spawn-mocha": "^2.2.2",
"gulp-tslint": "^8.0.0",
"gulp-typescript": "^3.0.1",
"gulp-typescript": "^3.1.6",
"tslint": "^5.0.0",
"typescript": "^2.2.0",
"yargs": "^5.0.0"
Expand Down
8 changes: 4 additions & 4 deletions src/readme/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ function requestFromGithub(path, callback) {
};
https.get(options, (resp) => {
resp.setEncoding('utf8');
const buffer = [];
resp.on('data', (chunk) => {
const buffer: string[] = [];
resp.on('data', (chunk: string) => {
buffer.push(chunk);
});
resp.on('end', () => {
Expand All @@ -43,7 +43,7 @@ function compareToESLint() {
const esRules = Object.keys(ruleESMap);
const missing = arrayDiff(rules.map(x => toCamelCase(x)), esRules);
const deprecated = arrayDiff(esRules, rules.map(x => toCamelCase(x)));
const buffer = [];
const buffer: string[] = [];

if (missing.length) {
buffer.push('Missing ESLint rules (http://eslint.org/docs/rules):');
Expand Down Expand Up @@ -88,7 +88,7 @@ function compareToTSLint() {

const tsRules = Object.keys(ruleTSMap);
const missing = arrayDiff(rules, tsRules);
const buffer = [];
const buffer: string[] = [];

if (missing.length) {
buffer.push('Missing TSLint rules (http://palantir.github.io/tslint/rules):');
Expand Down
10 changes: 5 additions & 5 deletions src/readme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ function formatUsage(usage) {
}

function createRuleTable() {
const buffer = [];
let category = null;
const buffer: string[] = [];
let category = '';
rules.forEach((rule) => {
if (category !== rule.category) {
category = rule.category;
Expand Down Expand Up @@ -108,10 +108,10 @@ ${rule.description}${usage}${note}
`;
}

function updateRuleFile(name: string, rule: IRule) {
function updateRuleFile(name: string, rule: IRule): Promise<void> {
const baseUrl = 'https://github.com/buzinas/tslint-eslint-rules/blob/master';
const docFileName = `src/docs/rules/${name}Rule.md`;
return new Promise((fulfill, reject) => {
return new Promise<void>((fulfill, reject) => {
fs.readFile(docFileName, 'utf8', (readErr, data) => {
rule.tslintUrl = rule.tslintUrl || `${baseUrl}/${docFileName}`;
let content = readErr || !data ? '<!-- Start:AutoDoc\n End:AutoDoc -->' : data;
Expand Down Expand Up @@ -142,7 +142,7 @@ function updateRuleFiles(cb: Function) {
const ruleNames = allFiles
.filter(name => /\.ts$/.test(name))
.map(name => name.substr(0, name.length - 7));
const allPromises = [];
const allPromises: Promise<void>[] = [];
ruleNames.forEach((name) => {
allPromises.push(updateRuleFile(name, ruleTSMap[name]));
});
Expand Down
2 changes: 1 addition & 1 deletion src/rules/arrayBracketSpacingRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class ArrayBracketSpacingWalker extends Lint.RuleWalker {
const start = node.end;
const text = this.getSourceFile().text.substring(start, end);
const m = text.match(/\/\*.*\*\//);
if (m) {
if (m && m.index >= 0) {
const len = m[0].length;
return trailing ? end - (start + m.index + len) : m.index;
}
Expand Down
7 changes: 5 additions & 2 deletions src/rules/braceStyleRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ class BraceStyleWalker extends Lint.RuleWalker {
const openingCurlyBrace = blockChildren.filter(ch => ch.kind === ts.SyntaxKind.OpenBraceToken).shift();
const closingCurlyBrace = blockChildren.filter(ch => ch.kind === ts.SyntaxKind.CloseBraceToken).pop();
const syntaxList = blockChildren.filter(ch => ch.kind === ts.SyntaxKind.SyntaxList).shift();
const blockPreviousNode = block.parent.getChildren()[block.parent.getChildren().indexOf(block) - 1];
const parentChildren = block.parent ? block.parent.getChildren() : [];
const blockPreviousNode = parentChildren[parentChildren.indexOf(block) - 1];

if (!openingCurlyBrace || !closingCurlyBrace || !syntaxList || !blockPreviousNode) {
return;
Expand Down Expand Up @@ -165,9 +166,11 @@ class BraceStyleWalker extends Lint.RuleWalker {
let position = children.indexOf(node) - 1;
while (position >= 0) { // is first child always block or catch clause?
if (children[position].kind === ts.SyntaxKind.Block || children[position].kind === ts.SyntaxKind.CatchClause) {
return children[position];
break;
}
position -= 1;
}
return children[position];
}

}
8 changes: 5 additions & 3 deletions src/rules/handleCallbackErrRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ class ErrCallbackHandlerWalker extends Lint.RuleWalker {
* Pops a function scope from the stack.
*/
private exitScope(): IFunctionScope {
return this.stack.pop();
// There will an item to pop because this method is called after `enterScope`.
return this.stack.pop()!;
}

protected visitSourceFile(node: ts.SourceFile) {
Expand Down Expand Up @@ -188,8 +189,9 @@ class ErrCallbackHandlerWalker extends Lint.RuleWalker {
// Only execute when inside a function scope and dealing with an identifier.
if (
this.stack.length > 0 &&
node.parent.kind !== ts.SyntaxKind.Parameter &&
node.kind === ts.SyntaxKind.Identifier
node.kind === ts.SyntaxKind.Identifier &&
node.parent &&
node.parent.kind !== ts.SyntaxKind.Parameter
) {
let doCheck = false;
const inPropertyAccess = this.isPropAccess(node.parent);
Expand Down
45 changes: 28 additions & 17 deletions src/rules/terIndentRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ let indentType = 'space';
let indentSize = 4;
let OPTIONS: any;

interface INodeIndent {
contentStart: number;
firstInLine: boolean;
space: number;
tab: number;
goodChar: number;
badChar: number;
}

function assign(target: any, ...sources: any[]): any {
sources.forEach((source) => {
if (source !== undefined && source !== null) {
Expand Down Expand Up @@ -374,11 +383,11 @@ class IndentWalker extends Lint.RuleWalker {
* user's desired indentation character, and `badChar` is the amount of the other indentation
* character.
*/
private getNodeIndent(node: ts.Node) {
private getNodeIndent(node: ts.Node): INodeIndent {
if (node === this.getSourceFile()) {
return { space: 0, tab: 0, goodChar: 0, badChar: 0 };
return { contentStart: 0, firstInLine: true, space: 0, tab: 0, goodChar: 0, badChar: 0 };
}
if (node.kind === ts.SyntaxKind.SyntaxList) {
if (node.kind === ts.SyntaxKind.SyntaxList && node.parent) {
return this.getNodeIndent(node.parent);
}

Expand Down Expand Up @@ -417,7 +426,7 @@ class IndentWalker extends Lint.RuleWalker {
if (isKind(node, 'IfStatement')) {
const elseStatement = (node as ts.IfStatement).elseStatement;
if (elseStatement) {
const elseKeyword = node.getChildren().filter(ch => isKind(ch, 'ElseKeyword')).shift();
const elseKeyword = node.getChildren().filter(ch => isKind(ch, 'ElseKeyword')).shift()!;
this.checkNodeIndent(elseKeyword, neededIndent);
if (!this.isNodeFirstInLine(elseStatement)) {
this.checkNodeIndent(elseStatement, neededIndent);
Expand All @@ -426,7 +435,7 @@ class IndentWalker extends Lint.RuleWalker {
} else if (isKind(node, 'TryStatement')) {
const handler = (node as ts.TryStatement).catchClause;
if (handler) {
const catchKeyword = handler.getChildren().filter(ch => isKind(ch, 'CatchKeyword')).shift();
const catchKeyword = handler.getChildren().filter(ch => isKind(ch, 'CatchKeyword')).shift()!;
this.checkNodeIndent(catchKeyword, neededIndent);
if (!this.isNodeFirstInLine(handler)) {
this.checkNodeIndent(handler, neededIndent);
Expand All @@ -435,11 +444,11 @@ class IndentWalker extends Lint.RuleWalker {

const finalizer = (node as ts.TryStatement).finallyBlock;
if (finalizer) {
const finallyKeyword = node.getChildren().filter(ch => isKind(ch, 'FinallyKeyword')).shift();
const finallyKeyword = node.getChildren().filter(ch => isKind(ch, 'FinallyKeyword')).shift()!;
this.checkNodeIndent(finallyKeyword, neededIndent);
}
} else if (isKind(node, 'DoStatement')) {
const whileKeyword = node.getChildren().filter(ch => isKind(ch, 'WhileKeyword')).shift();
const whileKeyword = node.getChildren().filter(ch => isKind(ch, 'WhileKeyword')).shift()!;
this.checkNodeIndent(whileKeyword, neededIndent);
}
}
Expand Down Expand Up @@ -473,7 +482,7 @@ class IndentWalker extends Lint.RuleWalker {
}

let indent;
let nodesToCheck = [];
let nodesToCheck: ts.Node[] = [];

/* For these statements we should check indent from statement beginning, not from the beginning
of the block.
Expand All @@ -495,7 +504,7 @@ class IndentWalker extends Lint.RuleWalker {
if (node.parent && isOneOf(node.parent, statementsWithProperties) && this.isNodeBodyBlock(node)) {
indent = this.getNodeIndent(node.parent).goodChar;
} else if (node.parent && isKind(node.parent, 'CatchClause')) {
indent = this.getNodeIndent(node.parent.parent).goodChar;
indent = this.getNodeIndent(node.parent.parent!).goodChar;
} else {
indent = this.getNodeIndent(node).goodChar;
}
Expand All @@ -506,6 +515,7 @@ class IndentWalker extends Lint.RuleWalker {
if (node.kind === ts.SyntaxKind.Block) {
nodesToCheck = node.getChildren()[1].getChildren();
} else if (
node.parent &&
isOneOf(node.parent, [
'ClassDeclaration',
'ClassExpression',
Expand Down Expand Up @@ -623,7 +633,7 @@ class IndentWalker extends Lint.RuleWalker {
*/
private isArgBeforeCalleeNodeMultiline(node: ts.Node): boolean {
const parent = node.parent;
if (parent['arguments'].length >= 2 && parent['arguments'][1] === node) {
if (parent && parent['arguments'].length >= 2 && parent['arguments'][1] === node) {
const firstArg = parent['arguments'][0];
return this.getLine(firstArg, true) > this.getLine(firstArg);
}
Expand Down Expand Up @@ -720,7 +730,7 @@ class IndentWalker extends Lint.RuleWalker {
*/
private expectedVarIndent(node: ts.VariableDeclaration, varIndent?: number) {
// VariableStatement -> VariableDeclarationList -> VariableDeclaration
const varNode = node.parent;
const varNode = node.parent!;
const line = this.getLine(varNode);
let indent;

Expand All @@ -745,31 +755,32 @@ class IndentWalker extends Lint.RuleWalker {
node: ts.Node,
kind: number,
stopAtList: number[] = [ts.SyntaxKind.SourceFile]
): T {
): T | null {
let parent = node.parent;

while (
parent.kind !== kind
parent
&& parent.kind !== kind
&& stopAtList.indexOf(parent.kind) === -1
&& parent.kind !== ts.SyntaxKind.SourceFile
) {
parent = parent.parent;
}

return parent.kind === kind ? parent as T : null;
return parent && parent.kind === kind ? parent as T : null;
}

/**
* Returns the VariableDeclarator based on the current node if not present then return null.
*/
protected getVariableDeclaratorNode(node: ts.Node): ts.VariableDeclaration {
protected getVariableDeclaratorNode(node: ts.Node): ts.VariableDeclaration | null {
return this.getParentNodeByType<ts.VariableDeclaration>(node, ts.SyntaxKind.VariableDeclaration);
}

/**
* Returns the BinaryExpression based on the current node if not present then return null.
*/
protected getBinaryExpressionNode(node: ts.Node): ts.BinaryExpression {
protected getBinaryExpressionNode(node: ts.Node): ts.BinaryExpression | null {
return this.getParentNodeByType<ts.BinaryExpression>(node, ts.SyntaxKind.BinaryExpression);
}

Expand All @@ -794,7 +805,7 @@ class IndentWalker extends Lint.RuleWalker {
let varKind;
const parentVarNode = this.getVariableDeclaratorNode(node);

if (this.isNodeFirstInLine(node)) {
if (this.isNodeFirstInLine(node) && node.parent) {
const parent = node.parent;

nodeIndent = this.getNodeIndent(parent).goodChar;
Expand Down
4 changes: 2 additions & 2 deletions src/test/rules/ruleTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class Test {
].join('\n');
assert(expected.length === 0 && found.length === 0, msg);
if (this.testFixer && this.output) {
const fixes = linter.getResult().failures.filter(f => f.hasFix()).map(f => f.getFix());
const fixes = linter.getResult().failures.filter(f => f.hasFix()).map(f => f.getFix()!);
const fixedCode = Lint.Replacement.applyFixes(this.code, fixes);
const fixerMsg = [
`Fixer output mismatch in ${this.name}:`,
Expand Down Expand Up @@ -257,7 +257,7 @@ class TestGroup {
config.rules[ruleName] = [true, ...groupConfig];
}
const configFile = Lint.Configuration.parseConfigFile(config);
return new Test(codeFileName, test, undefined, configFile, []);
return new Test(codeFileName, test, '', configFile, []);
}
if (test.options) {
config.rules[ruleName] = [true, ...test.options];
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"importHelpers": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"strictNullChecks": false,
"removeComments": true
}
}

0 comments on commit dec451b

Please sign in to comment.