-
Notifications
You must be signed in to change notification settings - Fork 33
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
feat: add valid graphql wire adapter callback parameters rule #144
Merged
Merged
Changes from all commits
Commits
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
29 changes: 29 additions & 0 deletions
29
docs/rules/valid-graphql-wire-adapter-callback-parameters.md
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,29 @@ | ||
# Validate graphql wire adapter callback parameter usage (valid-graphql-wire-adapter-callback-parameters) | ||
|
||
The graphql @wire adapter returns `{ data, errors }` instead of `{ data, error }`. Validate that graphql @wire customers are using `errors` not `error`. | ||
|
||
## Rule details | ||
|
||
Example of **incorrect** code: | ||
|
||
```js | ||
import { wire } from 'lwc'; | ||
import { gql, graphql } from 'lightning/uiGraphQLApi'; | ||
|
||
class Test { | ||
@wire(graphql, {}) | ||
wiredMethod({ error, data }) {} | ||
} | ||
``` | ||
|
||
Example of **correct** code: | ||
|
||
```js | ||
import { wire } from 'lwc'; | ||
import { gql, graphql } from 'lightning/uiGraphQLApi'; | ||
|
||
class Test { | ||
@wire(graphql, {}) | ||
wiredMethod({ errors, data }) {} | ||
} | ||
``` |
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
59 changes: 59 additions & 0 deletions
59
lib/rules/valid-graphql-wire-adapter-callback-parameters.js
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,59 @@ | ||
/* | ||
* Copyright (c) 2024, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
'use strict'; | ||
|
||
const { docUrl } = require('../util/doc-url'); | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'validate graphql error callback parameter', | ||
category: 'LWC', | ||
recommended: true, | ||
url: docUrl('valid-graphql-error-callback-parameter'), | ||
}, | ||
|
||
schema: [], | ||
}, | ||
|
||
create(context) { | ||
return { | ||
MethodDefinition(node) { | ||
const { decorators } = node; | ||
|
||
// Check that the @wire decorator is using graphql | ||
const graphQlDecorator = decorators.find((decorator) => { | ||
return decorator.expression.arguments.find((argument) => { | ||
return argument.name === 'graphql'; | ||
}); | ||
}); | ||
|
||
// Verify that the method definition is using 'errors' not 'error | ||
if (graphQlDecorator !== undefined) { | ||
if (node.value.type === 'FunctionExpression') { | ||
const objectPatternNode = node.value.params.find( | ||
(param) => param.type === 'ObjectPattern', | ||
); | ||
if (objectPatternNode !== undefined) { | ||
const incorrectErrorParameter = objectPatternNode.properties.find( | ||
(property) => { | ||
return property.value.name === 'error'; | ||
}, | ||
); | ||
if (incorrectErrorParameter !== undefined) { | ||
context.report({ | ||
node, | ||
message: | ||
'@wire graphql callback function object must use "errors" instead of "error"', | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
69 changes: 69 additions & 0 deletions
69
test/lib/rules/valid-graphql-wire-adapter-callback-parameters.js
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,69 @@ | ||
/* | ||
* Copyright (c) 2024, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
'use strict'; | ||
|
||
const { RuleTester } = require('eslint'); | ||
|
||
const { ESLINT_TEST_CONFIG } = require('../shared'); | ||
const rule = require('../../../lib/rules/valid-graphql-wire-adapter-callback-parameters'); | ||
|
||
const ruleTester = new RuleTester(ESLINT_TEST_CONFIG); | ||
|
||
ruleTester.run('valid-graphql-wire-adapter-callback-parameters', rule, { | ||
valid: [ | ||
{ | ||
code: `import { wire } from 'lwc'; | ||
import getFoo from 'adapter'; | ||
|
||
class Test { | ||
@wire(getFoo) | ||
wiredMethod() {} | ||
}`, | ||
}, | ||
{ | ||
code: `import { wire } from 'lwc'; | ||
import { gql, graphql } from 'lightning/uiGraphQLApi'; | ||
|
||
class Test { | ||
@wire(graphql, {}) | ||
wiredMethod({errors, data}) {} | ||
}`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: `import { wire } from 'lwc'; | ||
import { gql, graphql } from 'lightning/uiGraphQLApi'; | ||
|
||
class Test { | ||
@wire(graphql, {}) | ||
wiredMethod({error, data}) {} | ||
}`, | ||
errors: [ | ||
{ | ||
message: | ||
'@wire graphql callback function object must use "errors" instead of "error"', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `import { wire } from 'lwc'; | ||
import { gql, graphql } from 'lightning/uiGraphQLApi'; | ||
|
||
class Test { | ||
@wire(graphql, {}) | ||
wiredMethod({error, errors, data}) {} | ||
}`, | ||
errors: [ | ||
{ | ||
message: | ||
'@wire graphql callback function object must use "errors" instead of "error"', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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.
it should throw an error if they use both
errors
anderror
as well, yes?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.
@nolanlawson Yeah. Added a test case for that