Skip to content

Commit

Permalink
fix syntax support
Browse files Browse the repository at this point in the history
  • Loading branch information
gucong3000 committed Apr 13, 2018
1 parent 98e842c commit 39877e6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 61 deletions.
34 changes: 13 additions & 21 deletions 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");

Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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));
});

Expand All @@ -157,16 +152,15 @@ 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");
});
});

describe("SCSS", () => {
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));
});

Expand All @@ -176,16 +170,15 @@ 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");
});
});

describe("Less", () => {
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));
});

Expand All @@ -195,16 +188,15 @@ 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");
});
});

describe("SugarSS", () => {
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));
});

Expand All @@ -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");
});
});

Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion lib/__tests__/standalone.test.js
Expand Up @@ -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"
);
});
});
Expand Down
68 changes: 30 additions & 38 deletions 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();

Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 39877e6

Please sign in to comment.