|
| 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); |
0 commit comments