forked from artsy/reaction
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'lint_rule' of https://github.com/artsy/reaction
- Loading branch information
Showing
6 changed files
with
56 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// @ts-check | ||
|
||
const Lint = require("tslint") | ||
const ts = require("typescript") | ||
|
||
const message = "Did you mean to use `SystemQueryRenderer` instead?" | ||
|
||
class Rule extends Lint.Rules.AbstractRule { | ||
/** | ||
* @param {ts.SourceFile} sourceFile | ||
*/ | ||
apply(sourceFile) { | ||
// Skip storybook and test files. | ||
if ( | ||
sourceFile.fileName.includes(".story") || | ||
sourceFile.fileName.includes(".test") | ||
) { | ||
return [] | ||
} | ||
|
||
return this.applyWithWalker( | ||
new NoQueryRendererImportWalker(sourceFile, this.getOptions()) | ||
) | ||
} | ||
} | ||
|
||
class NoQueryRendererImportWalker extends Lint.RuleWalker { | ||
visitImportDeclaration(node) { | ||
const importSource = node.moduleSpecifier.text | ||
|
||
if (importSource === "react-relay") { | ||
const namedBindings = node.importClause.namedBindings | ||
const namedImports = | ||
namedBindings && | ||
namedBindings.elements && | ||
namedBindings.elements.map(e => e.getText()) | ||
|
||
if (namedImports.includes("QueryRenderer")) { | ||
this.addFailure( | ||
this.createFailure(node.getStart(), node.getWidth(), message) | ||
) | ||
} | ||
} | ||
|
||
// call the base version of this visitor to actually parse this node | ||
super.visitImportDeclaration(node) | ||
} | ||
} | ||
|
||
module.exports = { Rule: Rule } |