feat: split no-static-styles-assignment messages - #160
saberzero1 wants to merge 2 commits into
Conversation
| node, | ||
| messageId: "avoidStyleAssignment", | ||
| data: { property: "el.setCssStyles" }, | ||
| messageId: "avoidCustomPropertyInSetCssStyles", |
There was a problem hiding this comment.
I'm not sure that this is necessary. Isn't this already a linting error since setCssStyles expects CSSStyleDeclaration as the key
There was a problem hiding this comment.
This distinction isn't that significant so it's probably not really worth a custom linter rule for the setCssStyles case.
|
Why
|
In the Obsidian API, it is defined as such: interface HTMLElement extends Element {
setCssStyles(styles: Partial<CSSStyleDeclaration>): void;
setCssProps(props: Record<string, string>): void;
}
Using classes remains as an option for either. |
Using variables or classes is still preferable. At the same time, |
Summary
Fixes false positive where
no-static-styles-assignmentflaggedel.setCssProps({ 'padding-bottom': 'unset' }). The rule now enforces the intended API contract:setCssPropsis for CSS custom properties (--*) only, andsetCssStylesis for standard CSS properties only.Problem
The rule had a single
avoidStyleAssignmentmessage that told users "Use thesetCssPropsfunction if the CSS properties need to change dynamically.", but then flaggedsetCssPropsitself when called with standard property names. This was contradictory: the error message recommended the exact API it was reporting on.Additionally,
setCssStyles(Case 4) had identical logic tosetCssProps(Case 3); it flagged non---*keys. SincesetCssStylesis the correct API for standard CSS properties, this was backwards.Changes
New message IDs (
lib/rules/noStaticStylesAssignment.ts)avoidNonCustomPropertyInSetCssProps: reported whensetCssPropsreceives a standard property key. Tells the user to usesetCssStylesor CSS classes instead.avoidCustomPropertyInSetCssStyles: reported whensetCssStylesreceives a--*key. Tells the user to usesetCssPropsinstead.avoidStyleAssignment: now mentions bothsetCssPropsandsetCssStylesas alternatives for the genericel.style.*cases.Fixed
setCssStyleslogic (Case 4)--(custom properties don't belong in setCssStyles) instead of keys that don't start with--.Tests (
tests/noStaticStylesAssignment.test.ts)setCssStyleswith standard property,setCssStyleswith computed keysetCssPropscase to expectavoidNonCustomPropertyInSetCssPropssetCssStylescase: now tests{ '--my-var': 'blue' }expectingavoidCustomPropertyInSetCssStylesWhat changes for consumers
el.setCssProps({ '--my-var': 'value' })-> no error (correct usage)el.setCssProps({ 'color': 'blue' })-> error with a message directing tosetCssStylesor CSS classesel.setCssStyles({ 'color': 'blue' })-> no error (correct usage, previously flagged)el.setCssStyles({ '--my-var': 'value' })-> error with a message directing tosetCssPropsel.style.*direct assignments -> unchanged behavior, updated message text