Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@zephraph => [TSLint] Rule to warn about using QueryRenderer #3108

Merged
merged 1 commit into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Artsy/Relay/SystemQueryRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react"
/* tslint:disable-next-line:no-query-renderer-import */
import { QueryRenderer, QueryRendererProps } from "react-relay"
import { OperationType } from "relay-runtime"

Expand Down
4 changes: 2 additions & 2 deletions src/Components/Publishing/ToolTip/TooltipsDataLoader.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { TooltipsDataLoaderQueryResponse } from "__generated__/TooltipsDataLoaderQuery.graphql"
import { TooltipsDataLoaderQuery } from "__generated__/TooltipsDataLoaderQuery.graphql"
import * as Artsy from "Artsy"
import { SystemQueryRenderer } from "Artsy/Relay/SystemQueryRenderer"
import { getArtsySlugsFromArticle } from "Components/Publishing/Constants"
import { ArticleData } from "Components/Publishing/Typings"
import { keyBy } from "lodash"
import PropTypes from "prop-types"
import React, { Component } from "react"
import { QueryRenderer } from "react-relay"
import { graphql } from "react-relay"
import { ArticleProps } from "../Article"

Expand Down Expand Up @@ -39,7 +39,7 @@ export class TooltipsDataLoader extends Component<Props> {
}

return (
<QueryRenderer<TooltipsDataLoaderQuery>
<SystemQueryRenderer<TooltipsDataLoaderQuery>
environment={relayEnvironment}
query={graphql`
query TooltipsDataLoaderQuery(
Expand Down
1 change: 1 addition & 0 deletions src/DevTools/MockRelayRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { renderWithLoadProgress } from "Artsy/Relay/renderWithLoadProgress"
import { SystemQueryRenderer } from "Artsy/Relay/SystemQueryRenderer"
import { IMocks } from "graphql-tools/dist/Interfaces"
import React from "react"
/* tslint:disable-next-line:no-query-renderer-import */
import { QueryRenderer } from "react-relay"
import {
Environment,
Expand Down
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"no-console": false,
"no-import-un-mocked": true,
"no-namespace": [true, "allow-declarations"],
"no-query-renderer-import": true,
"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`.
mzikherman marked this conversation as resolved.
Show resolved Hide resolved
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
50 changes: 50 additions & 0 deletions tslint/noQueryRendererImportRule.js
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 {
mzikherman marked this conversation as resolved.
Show resolved Hide resolved
/**
* @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 }