-
Notifications
You must be signed in to change notification settings - Fork 7
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
Allow disable comment to be the directly above the statement #61
Conversation
The old behaviour looked for comments in the parent first, to not make it a breaking change I check the parent comments first.
seems to make sense to me I think it was done originally because we may be looking at a deeper node and wanted to look for comments on the owning statement instead looks like we're still doing that with this change so should be fine |
Yep, tried to keep the current logic as the first check so it wouldn't introduce breaking changes :) |
could you add some tests for this too? just also wondered - won't there always be a statement parent somewhere, so it'll never be nullish? tests will help prove it out though |
The old code was handling the case that getStatementParent returns something nullish: const statement = path.getStatementParent();
if (statement && statement.node.leadingComments) { ... } So I left that handling intact by adding optional chaining. |
I now see I made a boo-boo, my change only enables this to work (note the array): const MY_CONST = 600;
class MyClass {
static styles = [
// postcss-lit-disable-next-line
css`
@media (width >= ${MY_CONST}px) {
.foo { color: hotpink; }
}
`,
];
} But I also need this to work (note the absence of the array): const MY_CONST = 600;
class MyClass {
// postcss-lit-disable-next-line
static styles = css`
@media (width >= ${MY_CONST}px) {
.foo { color: hotpink; }
}
`;
} |
I fixed this by also checking the parent node. This also fixed the tests I tried to add:
|
It's a little too specific checking the parent and the current node I think I had a quick dig and I think what we're looking for is let parentStatement = undefined;
const disableNode = path.findParent((p) => {
// Determine if this is the statement
if (!parentStatement) {
if (!p.parentPath || (Array.isArray(p.container) && p.isStatement()) {
parentStatement = p;
}
} else {
// We already saw the root statement, stop looking
return false;
}
// Check if this parent has a disable comment
if (hasDisableComment(p)) {
return true;
}
return false;
});
if (disableNode) {
// It is disabled
} So this will support any parent node having a disabled comment until we reach the root statement I wrote this on mobile so some names are probably wrong |
How about this? 😄 |
Pull Request Test Coverage Report for Build 12376389881Details
💛 - Coveralls |
Pull Request Test Coverage Report for Build 12316190204Details
💛 - Coveralls |
A previous PR forgot to run the formatter, and this also reworks some of the logic here to be a little simpler.
The comment line to disable the postcss-lit plugin (
postcss-lit-disable-next-line
) doesn't necessarily need to be on the parent statement, especially with static class fields being more and more mainstream: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/staticThe following snippet breaks stylelint with postcss-lit:
Errors:
"(width >= POSTCSS_LIT_0px)"
name "POSTCSS_LIT_0px"
To disable the plugin, I'd now have to disable it for the entire class because
styles
is a static class field and therefore the parent statement is theMyClass
class:Because my classes contain more CSS than just this media statement, I'd like to be able to not disable this plugin for the entire class nor do I want to replace my static class field back to a getter. This PR allows disable comments to be placed directly above the statement, but the previous behaviour of looking at the parent comments is taking precedence so that it should not be a breaking change. This now works:
I also made it so the disable comment doesn't necessarily need to be the last comment before the statement. It can be one of the comments which is required if I also want to disable for example eslint for the same line, e.g.:
Let me know what you think, I'm open for suggestions!
EDIT:
Future work: maybe think about also supporting the following syntax:
As far as I can see the CSS tagged template expression is not parsed by Babel though, so it might require using another library for that.