Skip to content

Commit

Permalink
Merge pull request palantir#722 from myitcv/no_null_keyword
Browse files Browse the repository at this point in the history
Define no-null-keyword rule
  • Loading branch information
adidahiya committed Dec 5, 2015
2 parents 850c655 + 1064cd1 commit c6dc9ca
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ A sample configuration file with all options is available [here](https://github.
* `no-eval` disallows `eval` function invocations.
* `no-inferrable-types` disallows explicit type declarations for variables or parameters initialized to a number, string, or boolean.
* `no-internal-module` disallows internal `module` (use `namespace` instead).
* `no-null-keyword` disallows use of the `null` keyword literal
* `no-require-imports` disallows invocation of `require()` (use ES6-style imports instead).
* `no-shadowed-variable` disallows shadowed variable declarations.
* `no-string-literal` disallows object access via string literals.
Expand Down
1 change: 1 addition & 0 deletions docs/sample.tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"no-eval": true,
"no-inferrable-types": false,
"no-internal-module": true,
"no-null-keyword": true,
"no-require-imports": true,
"no-shadowed-variable": true,
"no-string-literal": true,
Expand Down
37 changes: 37 additions & 0 deletions src/rules/noNullKeywordRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// with due reference to https://github.com/Microsoft/TypeScript/blob/7813121c4d77e50aad0eed3152ef1f1156c7b574/scripts/tslint/noNullRule.ts

import * as Lint from "../lint";
import * as ts from "typescript";

export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "Use 'undefined' instead of 'null'";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NullWalker(sourceFile, this.getOptions()));
}
}

class NullWalker extends Lint.RuleWalker {
public visitNode(node: ts.Node) {
super.visitNode(node);
if (node.kind === ts.SyntaxKind.NullKeyword) {
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
}
}
}
1 change: 1 addition & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"rules/noEvalRule.ts",
"rules/noInferrableTypesRule.ts",
"rules/noInternalModuleRule.ts",
"rules/noNullKeywordRule.ts",
"rules/noRequireImportsRule.ts",
"rules/noShadowedVariableRule.ts",
"rules/noStringLiteralRule.ts",
Expand Down
2 changes: 2 additions & 0 deletions test/files/rules/nonullkeyword.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var x = null; // error
console.log(null, x); // error
38 changes: 38 additions & 0 deletions test/rules/noNullKeywordRuleTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as Lint from "../lint";

describe("<no-null-keyword>", () => {
const fileName = "rules/nonullkeyword.test.ts";
const NoNullKeywordRule = Lint.Test.getRule("no-null-keyword");
const actualFailures = Lint.Test.applyRuleOnFile(fileName, NoNullKeywordRule);

const createFailure = Lint.Test.createFailuresOnFile(fileName, NoNullKeywordRule.FAILURE_STRING);

it("disallows assignment 'null'", () => {
assert.equal(actualFailures.length, 2);

const expectedFailures = [
createFailure([1, 9], [1, 13]),
createFailure([2, 13], [2, 17])
];

for (let failure of expectedFailures) {
Lint.Test.assertContainsFailure(actualFailures, failure);
}
});
});
4 changes: 3 additions & 1 deletion test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"../src/rules/noEvalRule.ts",
"../src/rules/noInferrableTypesRule.ts",
"../src/rules/noInternalModuleRule.ts",
"../src/rules/noNullKeywordRule.ts",
"../src/rules/noRequireImportsRule.ts",
"../src/rules/noShadowedVariableRule.ts",
"../src/rules/noStringLiteralRule.ts",
Expand Down Expand Up @@ -139,6 +140,7 @@
"rules/noEvalRuleTests.ts",
"rules/noInferrableTypesRuleTests.ts",
"rules/noInternalModuleRuleTests.ts",
"rules/noNullKeywordRuleTests.ts",
"rules/noRequireImportsRuleTests.ts",
"rules/noShadowedVariableRuleTests.ts",
"rules/noStringLiteralRuleTests.ts",
Expand Down Expand Up @@ -169,4 +171,4 @@
"formatters/proseFormatterTests.ts",
"formatters/verboseFormatterTests.ts"
]
}
}

0 comments on commit c6dc9ca

Please sign in to comment.