Skip to content

Commit 2eddda9

Browse files
authored
feat(lint): add noVueComputedPropertiesInData (#12554)
1 parent 8ff63f4 commit 2eddda9

31 files changed

Lines changed: 971 additions & 1 deletion
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Added the nursery rule [`noVueComputedPropertiesInData`](https://biomejs.dev/linter/rules/no-vue-computed-properties-in-data/), which reports computed properties used inside the `data` function of a Vue component. Vue runs `data` before it sets up computed properties, so the value read there is `undefined`.
6+
7+
```vue
8+
<script>
9+
export default {
10+
data() {
11+
return { total: this.doubled };
12+
},
13+
computed: {
14+
doubled() {
15+
return 2;
16+
},
17+
},
18+
};
19+
</script>
20+
```

‎crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs‎

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/biome_configuration/src/analyzer/linter/rules.rs‎

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/biome_configuration/src/generated/domain_selector.rs‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/biome_configuration/src/generated/linter_options_check.rs‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/biome_diagnostics_categories/src/categories.rs‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
use crate::services::vue::VueComponent;
2+
use biome_analyze::{
3+
Rule, RuleDiagnostic, RuleDomain, RuleSource, context::RuleContext, declare_lint_rule,
4+
};
5+
use biome_console::markup;
6+
use biome_diagnostics::Severity;
7+
use biome_js_syntax::{
8+
AnyFunctionLike, AnyJsMemberExpression, AnyJsRoot, JsComputedMemberAssignment,
9+
JsGetterClassMember, JsGetterObjectMember, JsLanguage, JsSetterClassMember,
10+
JsSetterObjectMember, JsStaticMemberAssignment, TextRange,
11+
};
12+
use biome_rowan::{AstNode, SyntaxKindSet};
13+
use biome_rule_options::no_vue_computed_properties_in_data::NoVueComputedPropertiesInDataOptions;
14+
use biome_vue_semantic::{Computed, RefBase};
15+
16+
declare_lint_rule! {
17+
/// Disallow using computed properties inside the `data` function of a Vue component.
18+
///
19+
/// Vue runs the `data` function before it sets up the component's computed properties.
20+
/// Reading a computed property there through `this` gives `undefined` instead of the
21+
/// computed value.
22+
///
23+
/// Only code written directly in `data` is checked. Code inside a function created in
24+
/// `data`, such as an arrow function stored as a data property, is not reported.
25+
///
26+
/// ## Examples
27+
///
28+
/// ### Invalid
29+
///
30+
/// ```vue,expect_diagnostic
31+
/// <script>
32+
/// export default {
33+
/// data() {
34+
/// return {
35+
/// total: this.doubled,
36+
/// };
37+
/// },
38+
/// computed: {
39+
/// doubled() {
40+
/// return 2;
41+
/// },
42+
/// },
43+
/// };
44+
/// </script>
45+
/// ```
46+
///
47+
/// ### Valid
48+
///
49+
/// ```vue
50+
/// <script>
51+
/// export default {
52+
/// props: ["initial"],
53+
/// data() {
54+
/// return {
55+
/// count: this.initial,
56+
/// label: this.formatLabel(),
57+
/// read: () => this.doubled,
58+
/// };
59+
/// },
60+
/// computed: {
61+
/// doubled() {
62+
/// return this.count * 2;
63+
/// },
64+
/// },
65+
/// methods: {
66+
/// formatLabel() {
67+
/// return "Count";
68+
/// },
69+
/// },
70+
/// };
71+
/// </script>
72+
/// ```
73+
///
74+
pub NoVueComputedPropertiesInData {
75+
version: "next",
76+
name: "noVueComputedPropertiesInData",
77+
language: "js",
78+
recommended: true,
79+
severity: Severity::Error,
80+
domains: &[RuleDomain::Vue],
81+
sources: &[RuleSource::EslintVueJs("no-computed-properties-in-data").same()],
82+
}
83+
}
84+
85+
impl Rule for NoVueComputedPropertiesInData {
86+
type Query = VueComponent;
87+
type State = RuleState;
88+
type Signals = Box<[Self::State]>;
89+
type Options = NoVueComputedPropertiesInDataOptions;
90+
91+
fn run(ctx: &RuleContext<Self>) -> Self::Signals {
92+
let component = ctx.query();
93+
let Some(data) = component
94+
.data_function_range()
95+
.and_then(|range| find_function(&ctx.root(), range))
96+
else {
97+
return Box::default();
98+
};
99+
let data_range = data.syntax().text_trimmed_range();
100+
let snippet = component.snippet().map(|snippet| snippet.id());
101+
102+
let mut signals = Vec::new();
103+
for property in component.computed() {
104+
for reference in property.uses() {
105+
let range = reference.range();
106+
if reference.base() == RefBase::This
107+
&& reference.snippet().map(|snippet| snippet.id()) == snippet
108+
&& data_range.contains_range(range)
109+
&& runs_with(&data, range)
110+
{
111+
signals.push(RuleState {
112+
computed: property.clone(),
113+
range: member_access_range(&data, range),
114+
});
115+
}
116+
}
117+
}
118+
signals.into_boxed_slice()
119+
}
120+
121+
fn text_range(_ctx: &RuleContext<Self>, state: &Self::State) -> Option<TextRange> {
122+
Some(state.range)
123+
}
124+
125+
fn diagnostic(_ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> {
126+
Some(
127+
RuleDiagnostic::new(
128+
rule_category!(),
129+
state.range,
130+
markup! {
131+
"The computed property "<Emphasis>{state.computed.name()}</Emphasis>" is used inside the "<Emphasis>"data"</Emphasis>" function."
132+
},
133+
)
134+
.detail(
135+
state.computed.range(),
136+
markup! { "The computed property is declared here." },
137+
)
138+
.note(markup! {
139+
"Vue runs the "<Emphasis>"data"</Emphasis>" function before it sets up computed properties, so this value is "<Emphasis>"undefined"</Emphasis>"."
140+
})
141+
.note(markup! {
142+
"Compute the value from props or other data instead, or turn this data property into a computed property."
143+
}),
144+
)
145+
}
146+
}
147+
148+
pub struct RuleState {
149+
/// The computed property that is used.
150+
computed: Computed,
151+
/// The range of the member access, such as `this.name`.
152+
range: TextRange,
153+
}
154+
155+
/// Nodes that start code that runs at a different time than the function
156+
/// that contains them.
157+
const FUNCTION_KINDS: SyntaxKindSet<JsLanguage> = AnyFunctionLike::KIND_SET
158+
.union(JsGetterObjectMember::KIND_SET)
159+
.union(JsSetterObjectMember::KIND_SET)
160+
.union(JsGetterClassMember::KIND_SET)
161+
.union(JsSetterClassMember::KIND_SET);
162+
163+
/// Returns the function in `root` whose range is exactly `range`.
164+
fn find_function(root: &AnyJsRoot, range: TextRange) -> Option<AnyFunctionLike> {
165+
root.syntax()
166+
.covering_element(range)
167+
.ancestors()
168+
.find_map(AnyFunctionLike::cast)
169+
.filter(|function| function.syntax().text_trimmed_range() == range)
170+
}
171+
172+
/// Returns `true` when the code at `range` runs as part of `function` itself,
173+
/// and not inside a function nested in it.
174+
fn runs_with(function: &AnyFunctionLike, range: TextRange) -> bool {
175+
function
176+
.syntax()
177+
.covering_element(range)
178+
.ancestors()
179+
.take_while(|node| node != function.syntax())
180+
.all(|node| !FUNCTION_KINDS.matches(node.kind()))
181+
}
182+
183+
/// Returns the range of the member access whose member name is at `name`,
184+
/// such as `this.name` for `name`.
185+
fn member_access_range(function: &AnyFunctionLike, name: TextRange) -> TextRange {
186+
function
187+
.syntax()
188+
.covering_element(name)
189+
.ancestors()
190+
.find(|node| MEMBER_KINDS.matches(node.kind()))
191+
.map_or(name, |member| member.text_trimmed_range())
192+
}
193+
194+
/// Nodes that read or assign a member of an object.
195+
const MEMBER_KINDS: SyntaxKindSet<JsLanguage> = AnyJsMemberExpression::KIND_SET
196+
.union(JsStaticMemberAssignment::KIND_SET)
197+
.union(JsComputedMemberAssignment::KIND_SET);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* should generate diagnostics */
2+
import { defineComponent } from "vue";
3+
4+
export default defineComponent({
5+
data() {
6+
return {
7+
copy: this.foo,
8+
};
9+
},
10+
computed: {
11+
foo() {
12+
return 1;
13+
},
14+
},
15+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
source: crates/biome_js_analyze/tests/spec_tests.rs
3+
assertion_line: 196
4+
expression: invalid-define-component.js
5+
---
6+
# Input
7+
```js
8+
/* should generate diagnostics */
9+
import { defineComponent } from "vue";
10+
11+
export default defineComponent({
12+
data() {
13+
return {
14+
copy: this.foo,
15+
};
16+
},
17+
computed: {
18+
foo() {
19+
return 1;
20+
},
21+
},
22+
});
23+
24+
```
25+
26+
# Diagnostics
27+
```
28+
invalid-define-component.js:7:13 lint/nursery/noVueComputedPropertiesInData ━━━━━━━━━━━━━━━━━━━━━━━━
29+
30+
× The computed property foo is used inside the data function.
31+
32+
5 │ data() {
33+
6 │ return {
34+
> 7 │ copy: this.foo,
35+
│ ^^^^^^^^
36+
8 │ };
37+
9 │ },
38+
39+
i The computed property is declared here.
40+
41+
9 │ },
42+
10 │ computed: {
43+
> 11 │ foo() {
44+
│ ^^^
45+
12 │ return 1;
46+
13 │ },
47+
48+
i Vue runs the data function before it sets up computed properties, so this value is undefined.
49+
50+
i Compute the value from props or other data instead, or turn this data property into a computed property.
51+
52+
i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
53+
54+
55+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
/* should generate diagnostics */
3+
export default {
4+
data: function () {
5+
return {
6+
copy: this.foo,
7+
};
8+
},
9+
computed: {
10+
foo() {
11+
return 1;
12+
},
13+
},
14+
};
15+
</script>

0 commit comments

Comments
 (0)
⌂ 换网址