Skip to content

Commit

Permalink
Fix inaccurate customSyntax inference (#6645)
Browse files Browse the repository at this point in the history
Also, this change mocks `console.warn()` to clean test output.
  • Loading branch information
ybiquitous committed Feb 11, 2023
1 parent d24ee3b commit d9a6098
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-humans-hunt.md
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: inaccurate `customSyntax` inference
8 changes: 8 additions & 0 deletions lib/__tests__/overrides.test.js
Expand Up @@ -204,6 +204,7 @@ describe('single input file. all overrides are matching', () => {

// https://github.com/stylelint/stylelint/issues/5656
it('priority to apply overrides (scss): scss extends', async () => {
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {});
const linted = await standalone({
files: [path.join(fixturesPath, 'style.scss')],
config: {
Expand All @@ -220,10 +221,14 @@ describe('single input file. all overrides are matching', () => {

expect(linted.results).toHaveLength(1);
expect(linted.results[0].warnings).toHaveLength(0);

expect(warn).toHaveBeenCalledWith(expect.stringMatching(/use the "customSyntax" option/));
warn.mockRestore();
});

// https://github.com/stylelint/stylelint/issues/5656
it('priority to apply overrides (scss): apply rules', async () => {
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {});
const linted = await standalone({
files: [path.join(fixturesPath, 'style.scss')],
config: {
Expand All @@ -243,6 +248,9 @@ describe('single input file. all overrides are matching', () => {
expect(linted.results).toHaveLength(1);
expect(linted.results[0].warnings).toHaveLength(1);
expect(linted.results[0].warnings[0].rule).toBe('at-rule-no-unknown');

expect(warn).toHaveBeenCalledWith(expect.stringMatching(/use the "customSyntax" option/));
warn.mockRestore();
});
});

Expand Down
22 changes: 2 additions & 20 deletions lib/getPostcssResult.js
Expand Up @@ -117,24 +117,6 @@ function getCustomSyntax(customSyntax, basedir) {
throw new Error('Custom syntax must be a string or a Syntax object');
}

/** @type {{ [key: string]: string }} */
const previouslyInferredExtensions = {
html: 'postcss-html',
js: '@stylelint/postcss-css-in-js',
jsx: '@stylelint/postcss-css-in-js',
less: 'postcss-less',
md: 'postcss-markdown',
sass: 'postcss-sass',
sss: 'sugarss',
scss: 'postcss-scss',
svelte: 'postcss-html',
ts: '@stylelint/postcss-css-in-js',
tsx: '@stylelint/postcss-css-in-js',
vue: 'postcss-html',
xml: 'postcss-html',
xst: 'postcss-html',
};

/**
* @param {StylelintInternalApi} stylelint
* @param {string|undefined} filePath
Expand All @@ -144,9 +126,9 @@ function cssSyntax(stylelint, filePath) {
const fileExtension = filePath ? path.extname(filePath).slice(1).toLowerCase() : '';
const extensions = ['css', 'pcss', 'postcss'];

if (previouslyInferredExtensions[fileExtension]) {
if (fileExtension && !extensions.includes(fileExtension)) {
console.warn(
`${filePath}: When linting something other than CSS, you should install an appropriate syntax, e.g. "${previouslyInferredExtensions[fileExtension]}", and use the "customSyntax" option`,
`${filePath}: you should use the "customSyntax" option when linting something other than CSS`,
);
}

Expand Down

0 comments on commit d9a6098

Please sign in to comment.