diff --git a/lib/__tests__/standalone-syntax.test.js b/lib/__tests__/standalone-syntax.test.js index 7fd148d845..070d48b083 100644 --- a/lib/__tests__/standalone-syntax.test.js +++ b/lib/__tests__/standalone-syntax.test.js @@ -1,15 +1,11 @@ "use strict"; const fs = require("fs"); -const less = require("postcss-less"); const path = require("path"); const pify = require("pify"); -const sass = require("postcss-sass"); -const scss = require("postcss-scss"); const standalone = require("../standalone"); const stringFormatter = require("../formatters/stringFormatter"); const stripAnsi = require("strip-ansi"); -const sugarss = require("sugarss"); const fixturesPath = path.join(__dirname, "fixtures"); @@ -95,7 +91,7 @@ it("standalone with postcss-html syntax", () => { return standalone({ config, - customSyntax: `${fixturesPath}/postcss-html-syntax`, + customSyntax: "postcss-html", files: [ `${fixturesPath}/at-rule-empty-line-before.html`, `${fixturesPath}/comment-empty-line-before.html`, @@ -146,8 +142,7 @@ describe("standalone with syntax set by extension", () => { beforeEach(() => { return standalone({ files: `${fixturesPath}/extension-sensitive.*`, - config: { rules: { "color-no-invalid-hex": true } }, - syntax: "sass" + config: { rules: { "color-no-invalid-hex": true } } }).then(data => (results = data.results)); }); @@ -157,7 +152,7 @@ describe("standalone with syntax set by extension", () => { it("parsed as SASS", () => { const sassResult = results.find(r => path.extname(r.source) === ".sass"); - expect(sassResult._postcssResult.opts.syntax).toBe(sass); + expect(sassResult._postcssResult.root.source.lang).toBe("sass"); }); }); @@ -165,8 +160,7 @@ describe("standalone with syntax set by extension", () => { beforeEach(() => { return standalone({ files: `${fixturesPath}/extension-sensitive.*`, - config: { rules: { "block-no-empty": true } }, - syntax: "scss" + config: { rules: { "block-no-empty": true } } }).then(data => (results = data.results)); }); @@ -176,7 +170,7 @@ describe("standalone with syntax set by extension", () => { it("parsed as SCSS", () => { const scssResult = results.find(r => path.extname(r.source) === ".scss"); - expect(scssResult._postcssResult.opts.syntax).toBe(scss); + expect(scssResult._postcssResult.root.source.lang).toBe("scss"); }); }); @@ -184,8 +178,7 @@ describe("standalone with syntax set by extension", () => { beforeEach(() => { return standalone({ files: `${fixturesPath}/extension-sensitive.*`, - config: { rules: { "block-no-empty": true } }, - syntax: "less" + config: { rules: { "block-no-empty": true } } }).then(data => (results = data.results)); }); @@ -195,7 +188,7 @@ describe("standalone with syntax set by extension", () => { it("parsed as Less", () => { const lessResult = results.find(r => path.extname(r.source) === ".less"); - expect(lessResult._postcssResult.opts.syntax).toBe(less); + expect(lessResult._postcssResult.root.source.lang).toBe("less"); }); }); @@ -203,8 +196,7 @@ describe("standalone with syntax set by extension", () => { beforeEach(() => { return standalone({ files: `${fixturesPath}/extension-sensitive.*`, - config: { rules: { "block-no-empty": true } }, - syntax: "sugarss" + config: { rules: { "block-no-empty": true } } }).then(data => (results = data.results)); }); @@ -214,7 +206,7 @@ describe("standalone with syntax set by extension", () => { it("parsed as SugarSS", () => { const sssResult = results.find(r => path.extname(r.source) === ".sss"); - expect(sssResult._postcssResult.opts.syntax).toBe(sugarss); + expect(sssResult._postcssResult.root.source.lang).toBe("sugarss"); }); }); @@ -235,10 +227,10 @@ describe("standalone with syntax set by extension", () => { const lessResult = results.find(r => path.extname(r.source) === ".less"); const sassResult = results.find(r => path.extname(r.source) === ".sass"); const scssResult = results.find(r => path.extname(r.source) === ".scss"); - expect(sssResult._postcssResult.opts.syntax).toBe(sugarss); - expect(lessResult._postcssResult.opts.syntax).toBe(less); - expect(sassResult._postcssResult.opts.syntax).toBe(sass); - expect(scssResult._postcssResult.opts.syntax).toBe(scss); + expect(sssResult._postcssResult.root.source.lang).toBe("sugarss"); + expect(lessResult._postcssResult.root.source.lang).toBe("less"); + expect(sassResult._postcssResult.root.source.lang).toBe("sass"); + expect(scssResult._postcssResult.root.source.lang).toBe("scss"); results.forEach(result => { expect(result.warnings.length).toBe(1); expect(result.warnings[0].rule).toBe("color-no-invalid-hex"); diff --git a/lib/__tests__/standalone.test.js b/lib/__tests__/standalone.test.js index 7b10eb6c6a..f79614a56d 100644 --- a/lib/__tests__/standalone.test.js +++ b/lib/__tests__/standalone.test.js @@ -194,7 +194,7 @@ it("unknown syntax option", () => { }) .catch(err => { expect(err.message).toBe( - "You must use a valid syntax option, either: scss, less or sugarss" + "You must use a valid syntax option, either: scss, sass, less, sugarss, markdown, styled or html" ); }); }); diff --git a/lib/getPostcssResult.js b/lib/getPostcssResult.js index 2d3d31c550..15d93ee572 100644 --- a/lib/getPostcssResult.js +++ b/lib/getPostcssResult.js @@ -1,28 +1,20 @@ /* @flow */ "use strict"; -const autoSyntax = require("postcss-html"); +const autoSyntax = require("postcss-syntax"); const dynamicRequire = require("./dynamicRequire"); const fs = require("fs"); -const less = require("postcss-less"); -const path = require("path"); const postcss = require("postcss"); -const safeParser = require("postcss-safe-parser"); -const sass = require("postcss-sass"); -const scss = require("postcss-scss"); -const sugarss = require("sugarss"); + +const importLazy = require("import-lazy")(require); const syntaxes /*: { [syntaxName: string]: postcss$syntax, }*/ = { - css: { - stringify: postcss.stringify - }, - less, - sass, - scss, - sss: sugarss, - sugarss + sugarss: importLazy("sugarss") }; +["less", "sass", "scss", "markdown", "styled", "html"].forEach(mod => { + syntaxes[mod] = importLazy("postcss-" + mod); +}); const postcssProcessor = postcss(); @@ -68,37 +60,37 @@ module.exports = function( `Cannot resolve custom syntax module ${customSyntax}` ); } + /* + * PostCSS allows for syntaxes that only contain a parser, however, + * it then expects the syntax to be set as the `parser` option rather than `syntax. + */ + if (!syntax.parse) { + syntax = { + parse: syntax, + stringify: postcss.stringify + }; + } } else if (syntax) { syntax = syntaxes[syntax]; if (!syntax) { throw new Error( - "You must use a valid syntax option, either: scss, less or sugarss" + "You must use a valid syntax option, either: scss, sass, less, sugarss, markdown, styled or html" ); } - } else { - syntaxes.css.parse = stylelint._options.fix - ? safeParser - : postcss.parse; - const fileExtension = path - .extname(options.filePath || "") - .slice(1) - .toLowerCase(); - syntax = syntaxes[fileExtension] || autoSyntax(syntaxes); + } else if (!options.codeProcessors) { + syntaxes.css = stylelint._options.fix + ? { + parse: importLazy("postcss-safe-parser"), + stringify: postcss.stringify + } + : postcss.syntax; + syntax = autoSyntax(syntaxes); } - const postcssOptions /*: postcss$options*/ = {}; - - postcssOptions.from = options.filePath; - - /* - * PostCSS allows for syntaxes that only contain a parser, however, - * it then expects the syntax to be set as the `parser` option rather than `syntax. - */ - if (syntax && !syntax.stringify) { - postcssOptions.parser = syntax; - } else { - postcssOptions.syntax = syntax; - } + const postcssOptions /*: postcss$options*/ = { + from: options.filePath, + syntax + }; const source = options.code ? options.codeFilename : options.filePath; let preProcessedCode = code; diff --git a/package.json b/package.json index aa3c06326d..ed2e4c85e4 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,6 @@ "normalize-selector": "^0.2.0", "pify": "^3.0.0", "postcss": "^6.0.16", - "postcss-html": "^0.18.0", "postcss-less": "^1.1.5", "postcss-media-query-parser": "^0.2.3", "postcss-reporter": "^5.0.0", @@ -98,7 +97,11 @@ "lint-staged": "^7.0.0", "npm-run-all": "^4.0.2", "npmpub": "^3.1.0", + "postcss-html": "^0.21.0", "postcss-import": "^11.0.0", + "postcss-markdown": "^0.21.0", + "postcss-styled": "0.0.2", + "postcss-syntax": "0.0.2", "prettier": "~1.12.0", "remark-cli": "^5.0.0", "remark-lint-no-missing-blank-lines": "^1.0.1",