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

feat: add valid graphql wire adapter callback parameters rule #144

Merged
merged 3 commits into from
Apr 18, 2024
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
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,24 @@ To choose from three configuration settings, install the [`eslint-config-lwc`](h

### LWC

| Rule ID | Description | Fixable |
| -------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- | ------- |
| [lwc/consistent-component-name](./docs/rules/consistent-component-name.md) | ensure component class name matches file name | 🔧 |
| [lwc/no-api-reassignments](./docs/rules/no-api-reassignments.md) | prevent public property reassignments | |
| [lwc/no-deprecated](./docs/rules/no-deprecated.md) | disallow usage of deprecated LWC APIs | |
| [lwc/no-document-query](./docs/rules/no-document-query.md) | disallow DOM query at the document level | |
| [lwc/no-attributes-during-construction](./docs/rules/no-attributes-during-construction.md) | disallow setting attributes during construction | |
| [lwc/no-disallowed-lwc-imports](./docs/rules/no-disallowed-lwc-imports.md) | disallow importing unsupported APIs from the `lwc` package | |
| [lwc/no-leading-uppercase-api-name](./docs/rules/no-leading-uppercase-api-name.md) | ensure public property doesn't start with an upper-case character | |
| [lwc/no-unexpected-wire-adapter-usages](./docs/rules/no-unexpected-wire-adapter-usages.md) | enforce wire adapters to be used with `wire` decorator | |
| [lwc/no-unknown-wire-adapters](./docs/rules/no-unknown-wire-adapters.md) | disallow usage of unknown wire adapters | |
| [lwc/valid-api](./docs/rules/valid-api.md) | validate `api` decorator usage | |
| [lwc/valid-track](./docs/rules/valid-track.md) | validate `track` decorator usage | |
| [lwc/valid-wire](./docs/rules/valid-wire.md) | validate `wire` decorator usage | |
| [lwc/no-restricted-browser-globals-during-ssr](./docs/rules/no-restricted-browser-globals-during-ssr.md) | disallow access to global browser APIs during SSR | |
| [lwc/no-unsupported-ssr-properties](./docs/rules/no-unsupported-ssr-properties.md) | disallow access of unsupported properties in SSR | |
| [lwc/no-node-env-in-ssr](./docs/rules/no-node-env-in-ssr.md) | disallow usage of process.env.NODE_ENV in SSR | |
| Rule ID | Description | Fixable |
| -------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------ | ------- |
| [lwc/consistent-component-name](./docs/rules/consistent-component-name.md) | ensure component class name matches file name | 🔧 |
| [lwc/no-api-reassignments](./docs/rules/no-api-reassignments.md) | prevent public property reassignments | |
| [lwc/no-deprecated](./docs/rules/no-deprecated.md) | disallow usage of deprecated LWC APIs | |
| [lwc/no-document-query](./docs/rules/no-document-query.md) | disallow DOM query at the document level | |
| [lwc/no-attributes-during-construction](./docs/rules/no-attributes-during-construction.md) | disallow setting attributes during construction | |
| [lwc/no-disallowed-lwc-imports](./docs/rules/no-disallowed-lwc-imports.md) | disallow importing unsupported APIs from the `lwc` package | |
| [lwc/no-leading-uppercase-api-name](./docs/rules/no-leading-uppercase-api-name.md) | ensure public property doesn't start with an upper-case character | |
| [lwc/no-unexpected-wire-adapter-usages](./docs/rules/no-unexpected-wire-adapter-usages.md) | enforce wire adapters to be used with `wire` decorator | |
| [lwc/no-unknown-wire-adapters](./docs/rules/no-unknown-wire-adapters.md) | disallow usage of unknown wire adapters | |
| [lwc/valid-api](./docs/rules/valid-api.md) | validate `api` decorator usage | |
| [lwc/valid-track](./docs/rules/valid-track.md) | validate `track` decorator usage | |
| [lwc/valid-wire](./docs/rules/valid-wire.md) | validate `wire` decorator usage | |
| [lwc/no-restricted-browser-globals-during-ssr](./docs/rules/no-restricted-browser-globals-during-ssr.md) | disallow access to global browser APIs during SSR | |
| [lwc/no-unsupported-ssr-properties](./docs/rules/no-unsupported-ssr-properties.md) | disallow access of unsupported properties in SSR | |
| [lwc/no-node-env-in-ssr](./docs/rules/no-node-env-in-ssr.md) | disallow usage of process.env.NODE_ENV in SSR | |
| [lwc/valid-graphql-wire-adapter-callback-parameters](./docs/rules/valid-graphql-wire-adapter-callback-parameters.md) | ensure graphql wire adapters are using 'errors' instead of 'error' | |

### Best practices

Expand Down
29 changes: 29 additions & 0 deletions docs/rules/valid-graphql-wire-adapter-callback-parameters.md
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 }) {}
}
```
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const rules = {
'no-unknown-wire-adapters': require('./rules/no-unknown-wire-adapters'),
'prefer-custom-event': require('./rules/prefer-custom-event'),
'valid-api': require('./rules/valid-api'),
'valid-graphql-wire-adapter-callback-parameters': require('./rules/valid-graphql-wire-adapter-callback-parameters'),
'valid-track': require('./rules/valid-track'),
'valid-wire': require('./rules/valid-wire'),
'no-restricted-browser-globals-during-ssr': require('./rules/no-restricted-browser-globals-during-ssr'),
Expand Down
59 changes: 59 additions & 0 deletions lib/rules/valid-graphql-wire-adapter-callback-parameters.js
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 test/lib/rules/valid-graphql-wire-adapter-callback-parameters.js
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"',
},
],
},
],
Copy link
Contributor

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 and error as well, yes?

{errors, error, data}

Copy link
Contributor Author

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

});