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

Add --print-config equivalent to Node.js API #5723

Closed
adalinesimonian opened this issue Nov 18, 2021 · 3 comments · Fixed by #5734
Closed

Add --print-config equivalent to Node.js API #5723

adalinesimonian opened this issue Nov 18, 2021 · 3 comments · Fixed by #5734
Assignees
Labels
type: enhancement a new feature that isn't related to rules

Comments

@adalinesimonian
Copy link
Member

In vscode-stylelint, we continue to get issues with users confused as to why the extension doesn't work for their non-CSS syntaxes (e.g. SCSS, Less) when their Stylelint config is configured correctly. I was thinking of ways to make this easier for users and I thought that maybe, if there was a way to auto-detect if a user wants a certain file type to be linted, we could have the extension lint the file without having to have extra configuration in the extension.

However, there are different ways this could be achieved, each with pros and cons, and there still stands the question of if this is the right approach in the first place. I think some discussion and brainstorming could help figure out how we can make this less of a headache for users. I'm opening this issue in this repository since my gut feeling is that if there is a way to do this, it would belong in some form in the API here since Stylelint owns all the logic for reading, parsing, and determining effective configuration.

Excludes don't work for this — just because a user didn't exclude a file doesn't mean that they intend for that file to be linted. Since Stylelint is run on the CLI with the paths that it should lint, that's the only context where we know specifically what files the user wants to be linted. I think we should have a way to know this information in the API.

Maybe check overrides?

For example, let's say we ask Stylelint, "hey, do you have overrides set up for 'test.scss'?" If the resolved config for that particular file is:

module.exports = {
  // …
  overrides: [
    {
      files: ['**/*.scss'], // aha, there is an override for scss files
      customSyntax: 'postcss-scss',
      // …
    },
  ],
};

the response would be yes, and then we'd know downstream that we should lint that file. One way this could look:

const shouldLint = stylelint.hasOverride(path);
// -> shouldLint === true

Caveats

This would only work if the user is using overrides. If, instead, their configuration is:

module.exports = {
  customSyntax: 'postcss-scss',
  // …
};

this would result in a no, even though the user's intent here is that SCSS should be listed.

Includes, similar to tsc?

Another approach could be having an include option, similar to some tools like tsc:

module.exports = {
  include: ['**/*.scss', '**/*.css'],
  // …
};

This would indicate that Stylelint should only lint files matching those globs. This also would have the additional benefit that you could have Stylelint lint the files you want it to without having to provide it the globs directly on the CLI: npx stylelint . would only lint the files the user has configured in their project.

This would also mean that we now have the precise context to know if a user wants a file to be linted. We could ask Stylelint, downstream, if it has a particular file included in the "project":

const shouldLint = stylelint.included(path);
// -> shouldLint === true

While at first glance, this seems similar to what we do in the extension with validate: ['css', 'scss'], it is fundamentally different in that the user is now able to put into their config the exact files that they want Stylelint to apply to. Therefore, if the user is using include, we can know for absolute certain if a user wants to lint a specific file. There is no gap as there is with checking overrides.

Caveats

This would be another property added to the configuration. However, at least it wouldn't break any existing behaviour, as it would only come into effect when used.


These are just the solutions I came up with off the top of my head. I'd love to hear your thoughts. I definitely feel that there are ways we can make the user experience better here. Unlike ESLint, there are far more preprocessors and contexts in which CSS is used, even in other languages, so we have many more cases where users are going to have all sorts of configurations for all sorts of files.

@adalinesimonian adalinesimonian added the status: needs discussion triage needs further discussion label Nov 18, 2021
@adalinesimonian adalinesimonian changed the title Have a way to know for certain if user intends to lint a specific file Have a way in the API to know for certain if user intends to lint a specific file Nov 18, 2021
@ota-meshi
Copy link
Member

In my opinion, it is possible to automatically determine the target language by exposing a Node API that does the same thing as --print-config.
https://stylelint.io/user-guide/usage/cli#--print-config

If customSyntax is set in the obtained configuration, I think it is the target of linting.

@adalinesimonian
Copy link
Member Author

In my opinion, it is possible to automatically determine the target language by exposing a Node API that does the same thing as --print-config.
https://stylelint.io/user-guide/usage/cli#--print-config

If customSyntax is set in the obtained configuration, I think it is the target of linting.

I love this idea! It seems to be very minimally intrusive and simple to implement.

@jeddy3 jeddy3 added status: ready to implement is ready to be worked on by someone type: enhancement a new feature that isn't related to rules and removed status: needs discussion triage needs further discussion labels Nov 21, 2021
@jeddy3 jeddy3 changed the title Have a way in the API to know for certain if user intends to lint a specific file Add --print-config equivalent to Node API Nov 21, 2021
@jeddy3 jeddy3 changed the title Add --print-config equivalent to Node API Add --print-config equivalent to Node.js API Nov 21, 2021
@adalinesimonian adalinesimonian added status: wip is being worked on by someone and removed status: ready to implement is ready to be worked on by someone labels Nov 22, 2021
@adalinesimonian adalinesimonian self-assigned this Nov 22, 2021
@adalinesimonian
Copy link
Member Author

Implemented in #5734

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement a new feature that isn't related to rules
Development

Successfully merging a pull request may close this issue.

3 participants