From 8dca498e9f5c07e3acfcf91f6408ae055655b68a Mon Sep 17 00:00:00 2001 From: Aleks Hudochenkov Date: Thu, 21 Oct 2021 06:29:45 +0200 Subject: [PATCH] Show more info in missing customSyntax warning (#5611) * Show more info in missing customSyntax warning * Change markdown syntax * Update lib/getPostcssResult.js Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> --- lib/__tests__/standalone-syntax.test.js | 7 ++++- lib/getPostcssResult.js | 39 +++++++++++++------------ 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/lib/__tests__/standalone-syntax.test.js b/lib/__tests__/standalone-syntax.test.js index 8199377242..a8b2c2786f 100644 --- a/lib/__tests__/standalone-syntax.test.js +++ b/lib/__tests__/standalone-syntax.test.js @@ -9,6 +9,9 @@ const { promises: fs } = require('fs'); const fixturesPath = replaceBackslashes(path.join(__dirname, 'fixtures')); it('standalone with postcss-safe-parser', () => { + // Hide “When linting something other than CSS...” warning from test + jest.spyOn(console, 'warn').mockImplementation(() => {}); + return standalone({ files: `${fixturesPath}/syntax_error.*`, config: { @@ -47,7 +50,9 @@ it('standalone with postcss-safe-parser', () => { return fs.writeFile(root.source.input.file, root.source.input.css); }), - ); + ).then(() => { + jest.restoreAllMocks(); + }); }); }); diff --git a/lib/getPostcssResult.js b/lib/getPostcssResult.js index fe48734af9..599fd2658c 100644 --- a/lib/getPostcssResult.js +++ b/lib/getPostcssResult.js @@ -13,23 +13,6 @@ const { promises: fs } = require('fs'); const postcssProcessor = postcss(); -const previouslyInferedExtensions = [ - 'html', - 'js', - 'jsx', - 'less', - 'md', - 'sass', - 'sss', - 'scss', - 'svelte', - 'ts', - 'tsx', - 'vue', - 'xml', - 'xst', -]; - /** * @param {StylelintInternalApi} stylelint * @param {GetPostcssOptions} options @@ -145,6 +128,24 @@ function getCustomSyntax(customSyntax) { throw new Error(`Custom syntax must be a string or a Syntax object`); } +/** @type {{ [key: string]: string }} */ +const previouslyInferedExtensions = { + 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 @@ -160,9 +161,9 @@ function cssSyntax(stylelint, filePath) { const extensions = ['css', 'pcss', 'postcss']; - if (previouslyInferedExtensions.includes(fileExtension)) { + if (previouslyInferedExtensions[fileExtension]) { console.warn( - 'When linting something other than CSS, you should install an appropriate syntax, e.g. postcss-scss, and use the "customSyntax" option', + `${filePath}: When linting something other than CSS, you should install an appropriate syntax, e.g. "${previouslyInferedExtensions[fileExtension]}", and use the "customSyntax" option`, ); }