Skip to content

Commit 617f87f

Browse files
authored
Merge pull request #9 from mike-shine-salesforce/main
Static Analyzer docs
2 parents ea56772 + fac63c6 commit 617f87f

File tree

36 files changed

+1251
-0
lines changed

36 files changed

+1251
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# no-assignment-expression-assigns-value-to-member-variable
2+
3+
This rule flags getters in custom components that mutate properties, which prevents the static analyzer from being able to determine a value for the property when priming. To resolve this error, don’t assign or mutate member variables in your getter functions.
4+
5+
## ❌ Incorrect
6+
7+
```javascript
8+
import { LightningElement } from 'lwc';
9+
export default class Example extends LightningElement {
10+
prop;
11+
get input() {
12+
const value = 'value';
13+
this.prop = value;
14+
return value;
15+
}
16+
}
17+
```
18+
19+
## ✅ Correct
20+
21+
```javascript
22+
import { LightningElement } from 'lwc';
23+
export default class Example extends LightningElement {
24+
prop;
25+
get input() {
26+
return 'value';
27+
}
28+
}
29+
```
30+
31+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# no-assignment-expression-for-external-components
2+
3+
This rule flags assignment expressions with components created outside of a Salesforce namespace. These external components aren’t supported by assignment expressions.
4+
5+
6+
## ❌ Incorrect
7+
8+
```javascript
9+
import { LightningElement } from 'lwc';
10+
export default class Example extends LightningElement {
11+
prop;
12+
get getter() {
13+
return (this.prop = 'hi'), this.prop;
14+
}
15+
}
16+
17+
```
18+
19+
## ✅ Correct
20+
21+
```javascript
22+
import { LightningElement } from 'lwc';
23+
export default class Example extends LightningElement {
24+
@api prop;
25+
get getter() {
26+
return this.prop;
27+
}
28+
}
29+
30+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# no-call-expression-references-unsupported-namespace
2+
3+
This rule flags getters that invoke a function from an unsupported import. Getters don’t support invoking functions. To resolve this error, review the getter and remove the invocation of functions.
4+
5+
## ❌ Incorrect
6+
7+
```javascript
8+
import { LightningElement } from 'lwc';
9+
import invalid from 'x/invalid';
10+
export default class Example extends LightningElement {
11+
get invalidGetter() {
12+
return invalid();
13+
}
14+
}
15+
16+
```
17+
18+
## ✅ Correct
19+
20+
```javascript
21+
import { LightningElement } from 'lwc';
22+
export default class Example extends LightningElement {}
23+
24+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# no-class-refers-to-parent-class-from-unsupported-namespace
2+
3+
This rule flags components that extend from a parent class that’s not supported for Lightning web components, such as a custom class.
4+
5+
## ❌ Incorrect
6+
7+
```javascript
8+
import Foo from './foo';
9+
10+
export default class Example extends Foo {}
11+
12+
```
13+
14+
## ✅ Correct
15+
16+
```javascript
17+
import Foo from 'lightning/recordForm';
18+
19+
export default class Example extends Foo {}
20+
21+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# no-composition-on-unanalyzable-getter-property
2+
3+
This rule flags iterators, image `src` attributes, or child components that reference an unanalyzable getter. To resolve this error, review the referenced getter and make sure that it’s valid.
4+
5+
## ❌ Incorrect
6+
7+
```javascript
8+
import { LightningElement } from 'lwc';
9+
export default class Example extends LightningElement {
10+
prop;
11+
get invalidGetter() {
12+
this.prop = 'new value'; // mutations not allowed
13+
return this.prop;
14+
}
15+
}
16+
17+
```
18+
19+
```html
20+
<template>
21+
<c-child input={invalidGetter}></c-child>
22+
</template>
23+
24+
```
25+
26+
## ✅ Correct
27+
28+
```javascript
29+
import { LightningElement } from 'lwc';
30+
export default class Example extends LightningElement {
31+
@api prop;
32+
get getter() {
33+
return this.prop;
34+
}
35+
}
36+
37+
```
38+
39+
```html
40+
<template>
41+
<c-child input={getter}></c-child>
42+
</template>
43+
44+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# no-composition-on-unanalyzable-property-from-unresolvable-wire
2+
3+
This rule flags iterators, image `src` attributes, or child components of an LWC template that reference an unanalyzable property, such as an invalid getter, or a wire output from an unsupported or unprimeable wire. To resolve this error, review the property reference and the referenced variable to make sure they’re both valid.
4+
5+
## ❌ Incorrect
6+
7+
```javascript
8+
import { LightningElement, wire, api } from 'lwc';
9+
import { invalid } from 'x/invalid';
10+
export default class Example extends LightningElement {
11+
@wire(invalid, {})
12+
wireOutput;
13+
}
14+
15+
```
16+
17+
```html
18+
<template>
19+
<c-child input={wireOutput}></c-child>
20+
</template>
21+
22+
```
23+
24+
## ✅ Correct
25+
26+
```javascript
27+
import { LightningElement, wire, api } from 'lwc';
28+
import { getRecord } from 'lightning/uiRecordApi';
29+
export default class Example extends LightningElement {
30+
@wire(getRecord, {})
31+
wireOutput;
32+
}
33+
34+
```
35+
36+
```html
37+
<template>
38+
<c-child input={wireOutput}></c-child>
39+
</template>
40+
41+
```
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# no-composition-on-unanalyzable-property-missing
2+
3+
This rule flags iterators, image `src` attributes, or child components that reference a property that’s not found in the JavaScript file. This rule also flags a child class that references a property that’s only defined on the parent class. To resolve this error, review the referenced properties and make sure that they’re correctly defined.
4+
5+
## ❌ Incorrect
6+
7+
```javascript
8+
import { LightningElement } from 'lwc';
9+
export default class Example extends LightningElement {}
10+
11+
```
12+
13+
```html
14+
<template>
15+
<c-child input={doesNotExist}></c-child>
16+
</template>
17+
18+
```
19+
20+
## ✅ Correct
21+
22+
```javascript
23+
import { LightningElement } from 'lwc';
24+
export default class Example extends LightningElement {
25+
@api doesExist;
26+
}
27+
28+
```
29+
30+
```html
31+
<template>
32+
<c-child input={doesExist}></c-child>
33+
</template>
34+
35+
```
36+
37+
## 😟 Limitations
38+
39+
This rule flags a child class that references a property that’s only defined on the parent class.
40+
41+
### ❌ Incorrect
42+
43+
```javascript
44+
import Parent from 'x/parent';
45+
export default class Example extends Parent {}
46+
47+
```
48+
49+
```html
50+
<template>
51+
<c-child input={existsOnParent}></c-child>
52+
</template>
53+
54+
```
55+
56+
### ✅ Correct
57+
58+
Update the JavaScript file to explicitly define properties that are referenced in the template.
59+
60+
```javascript
61+
import Parent from 'x/parent';
62+
export default class Example extends Parent {
63+
@api existsOnParent;
64+
}
65+
66+
```
67+
68+
```html
69+
<template>
70+
<c-child input={existsOnParent}></c-child>
71+
</template>
72+
73+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# no-composition-on-unanalyzable-property-non-public
2+
3+
This rule flags iterators, image `src` attributes, or child components that reference a property that isn’t public. To resolve this error, review the referenced property to make sure that it’s public and valid.
4+
5+
## ❌ Incorrect
6+
7+
```javascript
8+
import { LightningElement } from 'lwc';
9+
export default class Example extends LightningElement {
10+
notPublic;
11+
}
12+
13+
```
14+
15+
```html
16+
<template>
17+
<c-child input={notPublic}></c-child>
18+
</template>
19+
20+
```
21+
22+
## ✅ Correct
23+
24+
```javascript
25+
import { LightningElement } from 'lwc';
26+
export default class Example extends LightningElement {
27+
@api public;
28+
}
29+
30+
```
31+
32+
```html
33+
<template>
34+
<c-child input={public}></c-child>
35+
</template>
36+
37+
```

lib/docs/no-eval-usage.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# no-eval-usage
2+
3+
This rule flags getters that use the `eval` function. To resolve this error, review the getter and refactor it to remove the `eval` function.
4+
5+
## ❌ Incorrect
6+
7+
```javascript
8+
import { LightningElement } from 'lwc';
9+
export default class Example extends LightningElement {
10+
get invalid() {
11+
return eval('something evil');
12+
}
13+
}
14+
15+
```
16+
17+
## ✅ Correct
18+
19+
```javascript
20+
import { LightningElement } from 'lwc';
21+
export default class Example extends LightningElement {
22+
get valid() {
23+
return 'do no evil';
24+
}
25+
}
26+
27+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# no-expression-contains-module-level-variable-ref
2+
3+
This rule flags getters that reference a variable defined at the module level. To resolve this error, remove the variable from the module level.
4+
5+
## ❌ Incorrect
6+
7+
```javascript
8+
import { LightningElement } from 'lwc';
9+
const moduleValue = 'val';
10+
export default class Example extends LightningElement {
11+
get invalid() {
12+
return moduleValue;
13+
}
14+
}
15+
16+
```
17+
18+
## ✅ Correct
19+
20+
```javascript
21+
import { LightningElement } from 'lwc';
22+
export default class Example extends LightningElement {
23+
get propValue() {
24+
return 'val';
25+
}
26+
get valid() {
27+
return this.propValue;
28+
}
29+
}
30+
31+
```

0 commit comments

Comments
 (0)