Skip to content

Commit 837ebc7

Browse files
authored
Fix unresolved mixin issue (#5)
1 parent 7c21381 commit 837ebc7

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

lib/rules/valid-apex-method-invocation.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ module.exports = {
2424
},
2525

2626
create(context) {
27-
function isApexMethodReference(ref) {
28-
const [def] = ref.defs;
27+
function isApexMethodReference(variable) {
28+
const [def] = variable.defs;
2929

3030
return (
3131
def &&
@@ -63,17 +63,19 @@ module.exports = {
6363
messageId: 'invalidArgumentType',
6464
});
6565
} else if (arg.type === 'Identifier') {
66-
const argReference = scope.references.find((r) => r.identifier === arg).resolved;
66+
const argReference = scope.references.find((r) => r.identifier === arg);
6767

6868
// Ignore unresolved or undefined arguments
69-
if (!argReference || !argReference.defs.length) {
69+
if (!argReference || !argReference.resolved) {
7070
return;
7171
}
7272

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

98100
// Retrieve the callee reference from the current scope.
99101
const scope = context.getScope();
100-
const methodReference = scope.references.find((r) => r.identifier === callee)
101-
.resolved;
102+
const methodReference = scope.references.find((r) => r.identifier === callee);
102103

103104
// Ignore the call expression if it can't be resolved from the current scope or if the
104105
// call expression doesn't reference an Apex method.
105-
if (!methodReference || !isApexMethodReference(methodReference)) {
106+
if (
107+
!methodReference ||
108+
!methodReference.resolved ||
109+
!isApexMethodReference(methodReference.resolved)
110+
) {
106111
return;
107112
}
108113

test/lib/rules/valid-apex-method-invocation.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ ruleTester.run('valid-apex-method-invocation', rule, {
3636
findContacts({ searchKey: 'Ted' });
3737
`,
3838
},
39+
{
40+
// Invocation with arguments passed as a literal from a nested scope.
41+
code: `
42+
import findContacts from '@salesforce/apex/ContactController.findContacts';
43+
44+
class provider {
45+
getData() {
46+
findContacts({ searchKey: 'Ted' });
47+
}
48+
}
49+
`,
50+
},
3951
{
4052
// Invocation on a continuation method with arguments passed as a literal.
4153
code: `
@@ -88,7 +100,7 @@ ruleTester.run('valid-apex-method-invocation', rule, {
88100
import findContacts from '@salesforce/apex/ContactController.findContacts';
89101
90102
function callApex(args) {
91-
findContacts(args);
103+
findContacts(args);
92104
}
93105
`,
94106
},
@@ -107,6 +119,21 @@ ruleTester.run('valid-apex-method-invocation', rule, {
107119
findContacts('Ted');
108120
`,
109121
},
122+
{
123+
// Invocation of an unresolved function
124+
code: `
125+
findContacts('Ted');
126+
`,
127+
},
128+
{
129+
// [#4]: Fix regression related to the usage of Mixin
130+
code: `
131+
import { LightningElement } from 'lwc';
132+
import { NavigationMixin } from 'lightning/navigation';
133+
134+
export default class extends NavigationMixin(LightningElement) {}
135+
`,
136+
},
110137
],
111138
invalid: [
112139
{
@@ -121,6 +148,23 @@ ruleTester.run('valid-apex-method-invocation', rule, {
121148
},
122149
],
123150
},
151+
{
152+
// Invocation with string literal in a nested scope.
153+
code: `
154+
import findContacts from '@salesforce/apex/ContactController.findContacts';
155+
156+
class provider {
157+
getData() {
158+
findContacts('Ted');
159+
}
160+
}
161+
`,
162+
errors: [
163+
{
164+
messageId: 'invalidArgumentType',
165+
},
166+
],
167+
},
124168
{
125169
// Invocation with boolean literal.
126170
code: `

0 commit comments

Comments
 (0)