Skip to content
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
21 changes: 13 additions & 8 deletions lib/rules/valid-apex-method-invocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ module.exports = {
},

create(context) {
function isApexMethodReference(ref) {
const [def] = ref.defs;
function isApexMethodReference(variable) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the changes in this file are to readjust the variable name. Everything used to be called a reference, when it is actually 2 different concepts in ESLint: Reference and Variable.

const [def] = variable.defs;

return (
def &&
Expand Down Expand Up @@ -63,17 +63,19 @@ module.exports = {
messageId: 'invalidArgumentType',
});
} else if (arg.type === 'Identifier') {
const argReference = scope.references.find((r) => r.identifier === arg).resolved;
const argReference = scope.references.find((r) => r.identifier === arg);

// Ignore unresolved or undefined arguments
if (!argReference || !argReference.defs.length) {
if (!argReference || !argReference.resolved) {
return;
}

// Report an error when the first argument is bound to a constant identifier initialized
// with an unsupported type.
const [argDefinition] = argReference.defs;
const argVariable = argReference.resolved;
const [argDefinition] = argVariable.defs;
if (
argDefinition &&
argDefinition.type === 'Variable' &&
argDefinition.parent.kind === 'const' &&
argDefinition.node.init &&
Expand All @@ -97,12 +99,15 @@ module.exports = {

// Retrieve the callee reference from the current scope.
const scope = context.getScope();
const methodReference = scope.references.find((r) => r.identifier === callee)
.resolved;
const methodReference = scope.references.find((r) => r.identifier === callee);

// Ignore the call expression if it can't be resolved from the current scope or if the
// call expression doesn't reference an Apex method.
if (!methodReference || !isApexMethodReference(methodReference)) {
if (
!methodReference ||
!methodReference.resolved ||
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key difference is here. In some cases, Reference.resolved might be undefined. The fix is to add this extra check.

!isApexMethodReference(methodReference.resolved)
) {
return;
}

Expand Down
46 changes: 45 additions & 1 deletion test/lib/rules/valid-apex-method-invocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ ruleTester.run('valid-apex-method-invocation', rule, {
findContacts({ searchKey: 'Ted' });
`,
},
{
// Invocation with arguments passed as a literal from a nested scope.
code: `
import findContacts from '@salesforce/apex/ContactController.findContacts';

class provider {
getData() {
findContacts({ searchKey: 'Ted' });
}
}
`,
},
{
// Invocation on a continuation method with arguments passed as a literal.
code: `
Expand Down Expand Up @@ -88,7 +100,7 @@ ruleTester.run('valid-apex-method-invocation', rule, {
import findContacts from '@salesforce/apex/ContactController.findContacts';

function callApex(args) {
findContacts(args);
findContacts(args);
}
`,
},
Expand All @@ -107,6 +119,21 @@ ruleTester.run('valid-apex-method-invocation', rule, {
findContacts('Ted');
`,
},
{
// Invocation of an unresolved function
code: `
findContacts('Ted');
`,
},
{
// [#4]: Fix regression related to the usage of Mixin
code: `
import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';

export default class extends NavigationMixin(LightningElement) {}
`,
},
],
invalid: [
{
Expand All @@ -121,6 +148,23 @@ ruleTester.run('valid-apex-method-invocation', rule, {
},
],
},
{
// Invocation with string literal in a nested scope.
code: `
import findContacts from '@salesforce/apex/ContactController.findContacts';

class provider {
getData() {
findContacts('Ted');
}
}
`,
errors: [
{
messageId: 'invalidArgumentType',
},
],
},
{
// Invocation with boolean literal.
code: `
Expand Down