diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..0a5f624 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,25 @@ +/* + * 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 base = require('./configs/base'); +const recommended = require('./configs/recommended'); + +const enforceFooBar = require('./rules/enforce-foo-bar'); +const apexImport = require('./rules/apex/apex-import'); + +module.exports = { + rules: { + 'enforce-foo-bar': enforceFooBar, + 'apex-import': apexImport + }, + configs: { + base, + recommended + } +}; diff --git a/lib/rules/apex/import-apex.js b/lib/rules/apex/apex-import.js similarity index 64% rename from lib/rules/apex/import-apex.js rename to lib/rules/apex/apex-import.js index d96c278..4dea8aa 100644 --- a/lib/rules/apex/import-apex.js +++ b/lib/rules/apex/apex-import.js @@ -14,19 +14,19 @@ module.exports = { type: 'problem', docs: { description: - "Importing apex modules can have issue for offline." + `When a client device is offline, Apex-based features can read data that was cached while online, but changes (writing data) can’t be saved back to the server.` }, fixable: 'code', schema: [] }, create(context) { return { - ImportDeclaration(node) { - if (node.source.includes('apex')) { + ImportDeclaration: (node) => { + if (node.source.value.startsWith('@salesforce/apex/')) { context.report({ node, message: - 'Watch out for importing apex!' + `Importing apex modules can have issue for offline.` }); } } diff --git a/src/configs/recommended.ts b/src/configs/recommended.ts index 3eba7d7..9b98a32 100644 --- a/src/configs/recommended.ts +++ b/src/configs/recommended.ts @@ -9,6 +9,7 @@ import type { ClassicConfig } from '@typescript-eslint/utils/ts-eslint'; export = { extends: ['./configs/base'], rules: { - '@salesforce/lwc-mobile/enforce-foo-bar': 'warn' + '@salesforce/lwc-mobile/enforce-foo-bar': 'warn', + '@salesforce/lwc-mobile/apex-import': 'warn' } } satisfies ClassicConfig.Config; diff --git a/test/lib/rules/apex/apex-import.js b/test/lib/rules/apex/apex-import.js new file mode 100644 index 0000000..a3d041d --- /dev/null +++ b/test/lib/rules/apex/apex-import.js @@ -0,0 +1,47 @@ +/* + * 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 { RULE_TESTER_CONFIG } = require('../shared'); + +const allRules = require('../../../../lib/index'); +const runtTester = new RuleTester(RULE_TESTER_CONFIG); + +runtTester.run('@salesforce/lwc-mobile/apex-import', allRules.rules['apex-import'], { + valid: [], + 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: [ + { + message: `Importing apex modules can have issue for offline.` + } + ] + }, + ], +}); diff --git a/test/lib/rules/apex/import-apex.js b/test/lib/rules/apex/import-apex.js deleted file mode 100644 index 774ac96..0000000 --- a/test/lib/rules/apex/import-apex.js +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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 { RULE_TESTER_CONFIG } = require('../shared'); - -const allRules = require('../../../../lib/index'); -const runtTester = new RuleTester(RULE_TESTER_CONFIG); - -runtTester.run('@salesforce/lwc-mobile/import-apex', allRules.rules['import-apex'], { - valid: [ - { - code: "import getContactList from '@salesforce/apex/ContactController.getContactList';" - } - ], - // 'invalid' checks cases that should not pass - invalid: [ - { - code: "import getContactList from '@salesforce/apex/ContactController.getContactList';", - output: "import getContactList from '@salesforce/apex/ContactController.getContactList';", - errors: 1 - } - ] -});