Created
July 18, 2018 13:41
-
-
Save djungowski/7d9126bb79970446b4ffeb5656c6bf1f to your computer and use it in GitHub Desktop.
TSLint Check to disallow fdescribe and fit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ts = require('typescript'); | |
const Lint = require('tslint'); | |
/** | |
* TSLint Check to disallow fdescribe and fit | |
* | |
* Usage in tslint.json: "no-jasmine-focus": true | |
*/ | |
class Rule extends Lint.Rules.AbstractRule { | |
/** | |
* @param {ts.SourceFile} sourceFile | |
* @return {RuleFailure[]} | |
*/ | |
apply(sourceFile) { | |
return this.applyWithWalker(new JasmineWalker(sourceFile, this.getOptions())); | |
} | |
} | |
class JasmineWalker extends Lint.RuleWalker { | |
/** | |
* @param {ts.SourceFile} sourceFile | |
* @param {IOptions} options | |
*/ | |
constructor(sourceFile, options) { | |
super(sourceFile, options); | |
this.disallowedFunctionNames = [ | |
'fdescribe', | |
'fit' | |
]; | |
} | |
/** | |
* Callback that is called by tslint when an identifier is called | |
* The identifier are the called functions | |
* | |
* @param {ts.Identifier} node | |
*/ | |
visitIdentifier(node) { | |
if (this.isFunctionNameDisallowed(node.text)) { | |
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), `${node.text} not allowed`)); | |
} | |
} | |
/** | |
* Check if a given function is not allowed | |
* | |
* @param {string} functionName | |
* @return {boolean} | |
*/ | |
isFunctionNameDisallowed(functionName) { | |
return this.disallowedFunctionNames.indexOf(functionName) !== -1; | |
} | |
} | |
exports.Rule = Rule; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment