-
-
Notifications
You must be signed in to change notification settings - Fork 977
Fix no-invalid-position-declaration
false positives for embedded styles
#8697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix no-invalid-position-declaration
false positives for embedded styles
#8697
Conversation
🦋 Changeset detectedLatest commit: 299318f The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
no-invalid-position-declaration
in HTML style attribute.no-invalid-position-declaration
false positives for embedded styles
@@ -316,3 +316,29 @@ testRule({ | |||
}, | |||
], | |||
}); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe after adding the main logic, add tests with the css-in-js example that @jeddy3 mentioned in the issue?
I mean
const foo = styled.a`color: red;`
@reduckted Thank you for making a start on this.
Thanks for looking. We've previously removed syntax-specific code from our rules, especially when it accesses something that's not part of their public API. Custom syntaxes that extract embedded code use We do this already in one of our utils: stylelint/lib/utils/isFirstNested.mjs Lines 77 to 83 in c56de6a
The postcss-html AST: jeddy3@mac % node -e 'require("postcss-html").parse(`<a style="color: red;"></a>`).walkDecls(console.log)'
<ref *1> Declaration {
raws: { before: '', between: ': ' },
type: 'decl',
parent: Root {
raws: {
semicolon: true,
after: '',
codeBefore: '<a style="',
codeAfter: '"></a>'
},
type: 'root',
nodes: [ [Circular *1] ],
source: {
input: [Input],
start: [Object],
end: [Object],
inline: true,
lang: 'css',
syntax: [Object]
},
lastEach: 2,
indexes: { '2': 0 },
document: Document {
raws: {},
type: 'document',
nodes: [Array],
source: [Object],
Symbol(isClean): false,
Symbol(my): true
},
Symbol(isClean): false,
Symbol(my): true
},
source: {
input: Input {
css: 'color: red;',
hasBOM: false,
document: '<a style="color: red;"></a>',
id: '<input css zeyKSU>',
Symbol(lineToIndexCache): [Array]
},
start: { column: 11, line: 1, offset: 10 },
end: { column: 21, line: 1, offset: 21 }
},
prop: 'color',
value: 'red',
Symbol(isClean): false,
Symbol(my): true
} Compared to the default postcss parser: jeddy3@mac % node -e 'require("postcss").parse(`color: red;`).walkDecls(console.log)'
<ref *1> Declaration {
raws: { before: '', between: ': ' },
type: 'decl',
parent: Root {
raws: { semicolon: true, after: '' },
type: 'root',
nodes: [ [Circular *1] ],
source: { input: [Input], start: [Object], end: [Object] },
lastEach: 1,
indexes: { '1': 0 },
Symbol(isClean): false,
Symbol(my): true
},
source: {
input: Input {
css: 'color: red;',
hasBOM: false,
document: 'color: red;',
id: '<input css 5sX5ub>',
Symbol(lineToIndexCache): [Array]
},
start: { column: 1, line: 1, offset: 0 },
end: { column: 11, line: 1, offset: 11 }
},
prop: 'color',
value: 'red',
Symbol(isClean): false,
Symbol(my): true
} This should also fix the false positives styled css-in-js due to its AST: jeddy3@mac % node -e 'require("postcss-styled-syntax").parse("const foo = styled.a`color: red;`").walkDecls(console.log)'
<ref *1> Declaration {
raws: { before: '', between: ': ' },
type: 'decl',
parent: Root {
raws: {
styledSyntaxRangeStart: 21,
styledSyntaxRangeEnd: 32,
isRuleLike: true,
styledSyntaxIsComponent: true,
semicolon: true,
after: '',
codeBefore: 'const foo = styled.a`',
codeAfter: '`'
},
type: 'root',
nodes: [ [Circular *1] ],
source: { input: [Input], start: [Object], end: [Object] },
parent: Document {
raws: {},
type: 'document',
source: [Object],
nodes: [Array],
lastEach: 1,
indexes: [Object],
Symbol(isClean): false,
Symbol(my): true
},
lastEach: 1,
indexes: { '1': 0 },
Symbol(isClean): false,
Symbol(my): true
},
source: {
input: Input {
css: 'color: red;',
hasBOM: false,
document: 'const foo = styled.a`color: red;`',
id: '<input css VgzK4F>',
Symbol(lineToIndexCache): [Array]
},
start: { offset: 21, line: 1, column: 22 },
end: { offset: 31, line: 1, column: 32 }
},
prop: 'color',
value: 'red',
Symbol(isClean): false,
Symbol(my): true
} However, this approach will introduce false negatives for embedded <style>
color: red;
</style> But we typically prefer false negatives to false positives as they're less disruptive to users. If we go this route, let's open up a follow-up issue to track the false negatives. It'll likely be part of a broader effort to consistently handle embedded styles (see #6833). |
@@ -23,6 +23,18 @@ const rule = (primary) => { | |||
|
|||
if (!validOptions) return; | |||
|
|||
if ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to move this condition before validOptions
assignment to avoid unnecessary operations
|
Completely understandable. Unfortunately, I can't see a way to detect this using only the public API. The only sure-fire way to prevent false positives is to disable the rule in your config for files with embedded styles. |
@reduckted hi, let me try to research this question |
Closing in favor of #8701. |
Closes #8695
I couldn't find any other rules that specifically check for the source coming from a
style
attribute, so I don't know if this is the preferred solution.For reference,
postcss-html
setsinline: true
andlang: "css"
when the CSS comes from astyle
attribute: