Skip to content

Commit

Permalink
[TSLint] Rule to warn about using QueryRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
mzikherman committed Jan 24, 2020
1 parent a3b7ed8 commit 6ea23e9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"no-console": false,
"no-import-un-mocked": true,
"no-namespace": [true, "allow-declarations"],
"no-query-renderer-import": {
"severity": "warning",
"options": ["blah"]
},
"no-switch-case-fall-through": true,
"no-unused-expression": false,
"no-var-requires": false,
Expand Down
2 changes: 1 addition & 1 deletion tslint/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Steps to adding a custom TSLint rule

1. Make a new file, the name is important, it must be camel-case and not a `.ts` file (the `@ts-check` declaration at the top of each file uses JSDoc to check types while developing rules – [read here](https://github.com/Microsoft/TypeScript/wiki/Type-Checking-JavaScript-Files) for more info).
1. Make a new file, the name is important, it must be camel-case and not a `.ts` file (the `@ts-check` declaration at the top of each file uses JSDoc to check types while developing rules – [read here](https://github.com/Microsoft/TypeScript/wiki/Type-Checking-JavaScript-Files) for more info). The suffix must be `...Rule.js`.
1. You will need to convert your camelCase name to kebab-case and add it to the [`tslint.json`](../tslint.json)

E.g. `noDoingAnythingRule.js` -> `no-doing-anything` and:
Expand Down
37 changes: 37 additions & 0 deletions tslint/noQueryRendererImportRule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// @ts-check

const Lint = require("tslint")
const ts = require("typescript")

class Rule extends Lint.Rules.AbstractRule {
/**
* @param {ts.SourceFile} sourceFile
*/
apply(sourceFile) {
// Skip storybook files.
if (sourceFile.fileName.includes(".story")) {
return []
}

if (
sourceFile.text.includes("QueryRenderer") &&
!sourceFile.text.includes("SystemQueryRenderer")
) {
const message = "Did you mean to use `SystemQueryRenderer` instead?"

return [
new Lint.RuleFailure(
sourceFile,
0,
0,
message,
"noQueryRendererImport"
),
]
}

return []
}
}

module.exports = { Rule: Rule }

0 comments on commit 6ea23e9

Please sign in to comment.