Skip to content

Commit

Permalink
Typescript implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
sfdctaka committed May 15, 2024
1 parent 0285239 commit 0af590a
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 52 deletions.
6 changes: 2 additions & 4 deletions lib/rules/apex/apex-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ module.exports = {
meta: {
type: 'problem',
docs: {
description:
`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.`
description: `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: []
Expand All @@ -25,8 +24,7 @@ module.exports = {
if (node.source.value.startsWith('@salesforce/apex/')) {
context.report({
node,
message:
`Importing apex modules can have issue for offline.`
message: `Importing apex modules can have issue for offline.`
});
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { Linter } from '@typescript-eslint/utils/ts-eslint';
import base from './configs/base.js';
import recommended from './configs/recommended.js';
import enforceFooBar from './rules/enforce-foo-bar.js';
import apexImport from './rules/apex/apex-import.js';
import { name, version } from '../package.json';
export = {
configs: {
Expand All @@ -21,6 +22,7 @@ export = {
version
},
rules: {
'enforce-foo-bar': enforceFooBar
'enforce-foo-bar': enforceFooBar,
'apex-import': apexImport
}
} satisfies Linter.Plugin;
35 changes: 35 additions & 0 deletions src/rules/apex/apex-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 default createRule({
create(context) {
return {
ImportDeclaration(node) {
if (node.source.value.startsWith('@salesforce/apex')) {
context.report({
node,
messageId: 'message'
});
}
}
};
},
name: 'apex-import',
meta: {
docs: {
description: 'Importing apex modules can have issues on mobile for offline usage.'
},
messages: {
message: 'Importing apex modules can have issues on mobile for offline usage.'
},
type: 'suggestion',
schema: []
},
defaultOptions: []
});
14 changes: 14 additions & 0 deletions src/util/createRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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';

export default ESLintUtils.RuleCreator(
// TODO: Add path to the doc like it is done here when the docs are ready
// https://github.com/salesforce/eslint-plugin-lwc-graph-analyzer/blob/main/lib/util/doc-url.js
(_name) => ``

Check failure on line 13 in src/util/createRule.ts

View workflow job for this annotation

GitHub Actions / Linting on Ubuntu with Node 18

'_name' is defined but never used

Check failure on line 13 in src/util/createRule.ts

View workflow job for this annotation

GitHub Actions / Linting on Ubuntu with Node 20

'_name' is defined but never used
);
47 changes: 0 additions & 47 deletions test/lib/rules/apex/apex-import.js

This file was deleted.

43 changes: 43 additions & 0 deletions test/rules/apex/apex-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 apexImport from '../../../src/rules/apex/apex-import';

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

ruleTester.run('@salesforce/lwc-mobile/apex/apex-import', apexImport, {
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: [{ messageId: 'message' }]
}
]
});

0 comments on commit 0af590a

Please sign in to comment.