Skip to content
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 property-no-unknown false negatives for newer custom syntaxes #6553

Merged
merged 3 commits into from Jan 5, 2023

Conversation

43081j
Copy link
Contributor

@43081j 43081j commented Jan 2, 2023

it seems this is leftover logic from back when we bundled various parts of the css-in-js solutions in the past.

by removing this, newer postcss syntaxes can be correctly used by stylelint (as they often have declarations with no parent rule).

cc @jeddy3

@changeset-bot
Copy link

changeset-bot bot commented Jan 2, 2023

🦋 Changeset detected

Latest commit: 54a5098

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
stylelint Patch

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

@jeddy3 jeddy3 changed the title fix: consider ruleless declarations as standard Fix property-no-unknown false negatives for newer custom syntaxes Jan 2, 2023
Copy link
Member

@jeddy3 jeddy3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -19,18 +12,6 @@ module.exports = function isStandardSyntaxDeclaration(decl) {
const prop = decl.prop;
Copy link
Contributor

@Mouvedia Mouvedia Jan 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isStandardSyntaxDeclaration is used in at least 7 rules. Are we sure this is not gonna cause regressions?
Is being more lax already covered by tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, tests don't cover. Test using postcss-css-in-js are skipped:

testRule({
skip: true,
ruleName,
config: [true],
customSyntax: 'postcss-css-in-js',

@43081j
Copy link
Contributor Author

43081j commented Jan 4, 2023

It seems to have originally existed to detect sources coming from the old css-in-js implementation. Something in the past must have been patching up the node source to contain the lang property it was looking for. However, that isn't a property within postcss (if it ever was), the css-in-js code must have been adding it in.

Those old syntaxes are deprecated afaik and their newer replacements won't set such a property anymore

@Mouvedia
Copy link
Contributor

Mouvedia commented Jan 4, 2023

We don't want #3930 to regress, else we will end up in a back and forth.
If there are still code bases where the condition is necessary, it should be an option/flag.
The default would be what you are asking for: lax.

@ybiquitous
Copy link
Member

I'm not sure, but @Mouvedia's point may be correct. @stylelint/postcss-css-in-js package depends on postcss-syntax, and the lang property is set in the package. So, problems may come for configs or plugins depending on @stylelint/postcss-css-in-js. See also:

@ybiquitous
Copy link
Member

[suggest] For compatibility with @stylelint/postcss-css-in-js and postcss-syntax, what about changing the logic as below?

/**
 * @param {unknown} lang
 */
function isStandardSyntaxLang(lang) {
	return lang && (lang === 'css' || lang === 'custom-template' || lang === 'template-literal');
}

// ...

module.exports = function isStandardSyntaxDeclaration(decl) {
	// ...

	// Declarations belong in a declaration block or standard CSS source
	if (
		parent &&
		isRoot(parent) &&
		parent.source &&
		'lang' in parent.source &&
		!isStandardSyntaxLang(parent.source.lang)
	) {
		return false;
	}

	// ...

@43081j
Copy link
Contributor Author

43081j commented Jan 4, 2023

i feel like you may be misunderstanding.

the original PR that introduced it didn't come from nothing, it changed from this:

  if (parent.type === "root") {
    return false;
  }

to this:

  if (parent.type === "root" && !isStandardSyntaxLang(parent.source.lang)) {
    return false;
  }

the former would have caused css-in-js syntaxes (old or new ones) to always return false in this statement when they have a top-level declaration (hence the issue you mentioned). the latter just added an escape hatch for those declarations produced by css-in-js syntaxes (such that the if would be skipped).

before:

  • if the declaration is a direct child of a root, it is non-standard

after:

  • if the declaration is a direct child of a root and isn't from a css-in-js syntax, it is non-standard

by removing it entirely here, we're saying declarations which are a direct child of a root are always standard/valid, not only when produced by css-in-js syntaxes.

importantly, we're not going back to the code from before the PR you mentioned. we're getting rid of it entirely.

this means all declarations which are children of roots will be valid, including the ones from before.

we're expanding the set, not reducing it. that regression/issue shouldn't be affected.

@Mouvedia
Copy link
Contributor

Mouvedia commented Jan 4, 2023

You are correct.
I guess the approach of waiting for issues to be raised if ppl somehow relied on

if (decl.parent.type === "root") { return false }

to filter standard declarations is a pragmatic choice.

@ybiquitous
Copy link
Member

@43081j Thank you for your explanation. Indeed, I misunderstood. 😅

by removing it entirely here, we're saying declarations which are a direct child of a root are always standard/valid, not only when produced by css-in-js syntaxes.

This approach is reasonable. And I believe it will not affect both @stylelint/postcss-css-in-js and postcss-syntax.

Copy link
Member

@ybiquitous ybiquitous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! LGTM 👍🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants