-
Notifications
You must be signed in to change notification settings - Fork 2
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
Import relayOperationGenericsRule #1
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0c115f5
Initial, WIP import.
ashfurrow ec6dcfb
Feedback on #1.
ashfurrow d3294c5
Addresses existing compiler errors.
ashfurrow be960fc
Lint fixes.
ashfurrow c42bfc9
Finishes TypeScript migration.
ashfurrow 931146a
Rename src to rules, removes tests.
ashfurrow ec86cd1
Finishes packaging.
ashfurrow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
.editorconfig | ||
.travis.yml | ||
node_modules/ | ||
src/ | ||
rules/ | ||
dangerfile.ts | ||
tsconfig.json | ||
tslint.json | ||
|
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,173 @@ | ||
import * as GraphQL from "graphql" | ||
import * as Lint from "tslint" | ||
import * as ts from "typescript" | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
apply(sourceFile) { | ||
return this.applyWithWalker(new RelayOperationGenericsWalker(sourceFile, this.getOptions())) | ||
} | ||
} | ||
|
||
class RelayOperationGenericsWalker extends Lint.RuleWalker { | ||
imports: ts.ImportDeclaration[] = [] | ||
|
||
visitImportDeclaration(node: ts.ImportDeclaration) { | ||
this.imports.push(node) | ||
super.visitImportDeclaration(node) | ||
} | ||
|
||
visitJsxSelfClosingElement(node: ts.JsxSelfClosingElement) { | ||
if (node.tagName.getText() === "QueryRenderer") { | ||
for (const property of node.attributes.properties) { | ||
if ( | ||
property.kind === ts.SyntaxKind.JsxAttribute && | ||
property.name.getText() === "query" && | ||
property.initializer | ||
) { | ||
const initializer = property.initializer | ||
if (initializer.kind === ts.SyntaxKind.JsxExpression) { | ||
const expression = initializer.expression | ||
if (expression) { | ||
this.visitOperationConfiguration(node, expression, node.tagName) | ||
} | ||
} else { | ||
this.addFailureAtNode(initializer, "expected a graphql`…` tagged-template expression") | ||
} | ||
break | ||
} | ||
} | ||
} | ||
|
||
super.visitJsxSelfClosingElement(node) | ||
} | ||
|
||
visitCallExpression(node: ts.CallExpression) { | ||
const functionName = node.expression as ts.Identifier | ||
if (functionName.text === "commitMutation") { | ||
const config = node.arguments[1] as undefined | ts.ObjectLiteralExpression | ||
if (config && config.kind === ts.SyntaxKind.ObjectLiteralExpression) { | ||
// any = this.visitOperationConfiguration(node, config, functionName) | ||
for (const property of config.properties) { | ||
if (property.name && property.name.getText() === "mutation") { | ||
if (property.kind === ts.SyntaxKind.PropertyAssignment) { | ||
this.visitOperationConfiguration(node, property.initializer, functionName) | ||
} else { | ||
// TODO: Need to expand parsing if we want to support e.g. | ||
// short-hand property assignment. | ||
this.addFailureAtNode(property, "use traditional assignment for mutation query") | ||
} | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
super.visitCallExpression(node) | ||
} | ||
|
||
visitOperationConfiguration( | ||
node: ts.CallExpression | ts.JsxSelfClosingElement, | ||
expression: ts.Expression, | ||
functionOrTagName: ts.Node, | ||
) { | ||
const taggedTemplate = expression as ts.TaggedTemplateExpression | ||
if ( | ||
taggedTemplate.kind === ts.SyntaxKind.TaggedTemplateExpression && | ||
taggedTemplate.tag.getText() === "graphql" | ||
) { | ||
const typeArgument = node.typeArguments && (node.typeArguments[0] as ts.TypeReferenceNode) | ||
if (!typeArgument) { | ||
const operationName = getOperationName(taggedTemplate) | ||
if (operationName) { | ||
const fixes = this.createFixes(functionOrTagName.getEnd(), 0, `<${operationName}>`, operationName) | ||
this.addFailureAtNode(functionOrTagName, "missing operation type parameter", fixes) | ||
} | ||
} else { | ||
const operationName = getOperationName(taggedTemplate) | ||
if ( | ||
operationName && | ||
(typeArgument.kind !== ts.SyntaxKind.TypeReference || typeArgument.typeName.getText() !== operationName) | ||
) { | ||
const fixes = this.createFixes( | ||
typeArgument.getStart(), | ||
typeArgument.getWidth(), | ||
operationName, | ||
operationName, | ||
) | ||
this.addFailureAtNode( | ||
typeArgument, | ||
`expected operation type parameter to be \`${operationName}\``, | ||
fixes, | ||
) | ||
} | ||
} | ||
} else { | ||
this.addFailureAtNode(taggedTemplate, "expected a graphql`…` tagged-template") | ||
} | ||
} | ||
|
||
createFixes(start: number, width: number, replacement: string, operationName: string): Lint.Replacement[] { | ||
const fixes = [new Lint.Replacement(start, width, replacement)] | ||
if (!this.hasImportForOperation(operationName)) { | ||
fixes.push(this.importDeclarationFixForOperation(operationName)) | ||
} | ||
return fixes | ||
} | ||
|
||
importPathForOperation(operationName: string) { | ||
const options = this.getOptions()[0] || { | ||
artifactDirectory: "__generated__", | ||
makeRelative: false, | ||
} | ||
if (options.makeRelative) { | ||
throw new Error("[relayOperationGenericsRule] Making import declarations relative is not implemented yet.") | ||
} | ||
return `${options.artifactDirectory}/${operationName}.graphql` | ||
} | ||
|
||
importDeclarationFixForOperation(operationName: string) { | ||
const path = this.importPathForOperation(operationName) | ||
const importDeclaration = `import { ${operationName} } from "${path}"\n` | ||
|
||
const lastImport = this.imports[this.imports.length - 1] | ||
|
||
let start = 0 | ||
if (lastImport) { | ||
start = lastImport.getEnd() + 1 | ||
} | ||
|
||
return new Lint.Replacement(start, 0, importDeclaration) | ||
} | ||
|
||
hasImportForOperation(operationName: string) { | ||
const importPath = this.importPathForOperation(operationName) | ||
|
||
return this.imports.some(node => { | ||
const path = node.moduleSpecifier as ts.StringLiteral | ||
if (path.text === importPath && node.importClause) { | ||
const namedBindings = node.importClause.namedBindings as ts.NamedImports | ||
if (namedBindings) { | ||
return namedBindings.elements.some(element => element.name.getText() === operationName) | ||
} | ||
} | ||
return false | ||
}) | ||
} | ||
} | ||
|
||
function getOperationName(taggedTemplate: ts.TaggedTemplateExpression): string | null { | ||
const template = taggedTemplate.template.getFullText() | ||
// Strip backticks | ||
const source = template.substring(1, template.length - 1) | ||
|
||
const ast = GraphQL.parse(source) | ||
let queryName = null | ||
GraphQL.visit(ast, { | ||
OperationDefinition(node) { | ||
queryName = node.name.value | ||
return GraphQL.BREAK | ||
}, | ||
}) | ||
|
||
return queryName | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,3 @@ | ||
{ | ||
"rulesDirectory": "./build" | ||
} |
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,6 @@ | ||
{ | ||
"extends": "./tslint-base.json", | ||
"rules": { | ||
"tslint-plugin-relay": true | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to put this in
devDependencies
because peer deps aren't installed by default.