Skip to content

Commit

Permalink
Merge pull request #3 from salesforce/apex
Browse files Browse the repository at this point in the history
A new rule to warn that importing and using Apex module may not work properly on mobile when offline.
  • Loading branch information
sfdctaka authored May 21, 2024
2 parents 8c1607b + 173e70e commit d8eb92d
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/configs/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export = {
skipGraphQLConfig: true
},
rules: {
'@salesforce/lwc-mobile/offline-graphql-no-mutation-supported': 'warn'
'@salesforce/lwc-mobile/offline-graphql-no-mutation-supported': 'warn',
'@salesforce/lwc-mobile/apex-import': 'warn'
}
}
]
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import base from './configs/base.js';
import recommended from './configs/recommended.js';
import enforceFooBar from './rules/dummy/enforce-foo-bar.js';
import { rule as apexImport, APEX_IMPORT_RULE_ID } from './rules/apex/apex-import.js';
import {
rule as mutionNotSupported,
NO_MUTATION_SUPPORTED_RULE_ID
Expand All @@ -24,6 +25,7 @@ export = {
},
rules: {
'enforce-foo-bar': enforceFooBar,
[NO_MUTATION_SUPPORTED_RULE_ID]: mutionNotSupported
[NO_MUTATION_SUPPORTED_RULE_ID]: mutionNotSupported,
[APEX_IMPORT_RULE_ID]: apexImport
}
};
37 changes: 37 additions & 0 deletions src/rules/apex/apex-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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
*/

import createRule from '../../util/createRule';
export const APEX_IMPORT_RULE_ID = 'apex-import';

export const rule = createRule({
create(context) {
return {
ImportDeclaration(node) {
if (node.source.value.startsWith('@salesforce/apex')) {
context.report({
node,
messageId: APEX_IMPORT_RULE_ID
});
}
}
};
},
name: 'apex-import',
meta: {
docs: {
description: 'Importing apex modules can have issues on mobile for offline usage.'
},
messages: {
[APEX_IMPORT_RULE_ID]:
'Importing apex modules can have issues on mobile for offline usage.'
},
type: 'suggestion',
schema: []
},
defaultOptions: []
});
13 changes: 13 additions & 0 deletions src/util/createRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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
*/

import { ESLintUtils } from '@typescript-eslint/utils';
import { version, homepage } from '../../package.json';

export default ESLintUtils.RuleCreator(
(name) => `${homepage}/blob/v${version}/lib/docs/${name}.md`
);
66 changes: 66 additions & 0 deletions test/rules/apex/apex-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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
*/

import { RuleTester } from '@typescript-eslint/rule-tester';

import { rule, APEX_IMPORT_RULE_ID } from '../../../src/rules/apex/apex-import';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser'
});

ruleTester.run('@salesforce/lwc-mobile/apex/apex-import', rule, {
valid: [
{
code: `
import { LightningElement, wire } from 'lwc';
import getContactList from 'ContactController.getContactList';
export default class ApexWireMethodToFunction extends LightningElement {
contacts;
error;
@wire(getContactList)
wiredContacts({ error, data }) {
if (data) {
this.contacts = data;
this.error = undefined;
} else if (error) {
this.error = error;
this.contacts = undefined;
}
}
}
`
}
],
invalid: [
{
code: `
import { LightningElement, wire } from 'lwc';
import getContactList from '@salesforce/apex/ContactController.getContactList';
export default class ApexWireMethodToFunction extends LightningElement {
contacts;
error;
@wire(getContactList)
wiredContacts({ error, data }) {
if (data) {
this.contacts = data;
this.error = undefined;
} else if (error) {
this.error = error;
this.contacts = undefined;
}
}
}
`,
errors: [{ messageId: APEX_IMPORT_RULE_ID }]
}
]
});

0 comments on commit d8eb92d

Please sign in to comment.