Skip to content

Commit c3474e3

Browse files
committed
Merge pull request #39 from webschik/webschik.38
#38
2 parents 2172710 + 528d869 commit c3474e3

8 files changed

+195
-101
lines changed

.eslintrc

+63-17
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,71 @@
33
"ecmaVersion": 6,
44
"sourceType": "module"
55
},
6-
7-
"extends": ["strict", "strict/es6", "strict/mocha"],
8-
6+
"extends": [
7+
"strict",
8+
"strict/es6",
9+
"strict/mocha"
10+
],
911
"rules": {
1012
// JUSTIFICATION: code cleanliness
1113
// separation of variable declarations and logic makes source code more readable
12-
"newline-after-var": [2, "always"],
13-
"semi": [2, "always"],
14-
"space-before-function-paren": [2, "always"],
15-
"comma-dangle": [2, "never"],
16-
"array-bracket-spacing": [2, "never"],
17-
"object-curly-spacing": [2, "never"],
18-
"no-trailing-spaces": [2, { "skipBlankLines": true }],
19-
20-
//let JSCS to validate indentations
21-
"indent": [0],
22-
"eol-last": [0],
23-
"no-extra-semi": 0,
24-
"semi-spacing": [2, {"before": false, "after": true}],
25-
"quotes": [2, "single", "avoid-escape"]
14+
"newline-after-var": [
15+
"error",
16+
"always"
17+
],
18+
"semi": [
19+
"error",
20+
"always"
21+
],
22+
"space-before-function-paren": [
23+
"error",
24+
"always"
25+
],
26+
"comma-dangle": [
27+
"error",
28+
"never"
29+
],
30+
"array-bracket-spacing": [
31+
"error",
32+
"never"
33+
],
34+
"object-curly-spacing": [
35+
"error",
36+
"never"
37+
],
38+
"no-trailing-spaces": [
39+
"error",
40+
{
41+
"skipBlankLines": true
42+
}
43+
],
44+
"indent": [
45+
"error",
46+
4,
47+
{
48+
"SwitchCase": 1
49+
}
50+
],
51+
"eol-last": [
52+
"off"
53+
],
54+
"no-extra-semi": [
55+
"off"
56+
],
57+
"no-return-assign": [
58+
"off"
59+
],
60+
"semi-spacing": [
61+
"error",
62+
{
63+
"before": false,
64+
"after": true
65+
}
66+
],
67+
"quotes": [
68+
"error",
69+
"single",
70+
"avoid-escape"
71+
]
2672
}
2773
}

.jscsrc

-59
This file was deleted.

README.md

+34-13
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,7 @@ This module also enables parsing of single-line comments in CSS source code.
5757

5858
Note that you don't need a special stringifier to handle the output; the default
5959
one will automatically convert single line comments into block comments.
60-
If you need to get inline comments, use stringifier from `postcss-less` module:
61-
62-
````js
63-
import postCssLess from 'postcss-less';
64-
65-
const root = postCssLess.parse('// Hello world');
66-
67-
root.first.toString(); // returns '/* Hello world */'
68-
69-
root.first.toString({
70-
stringify: postCssLess.stringify
71-
}); // returns '// Hello world'
72-
````
60+
If you need to get inline comments, use [custom PostCSSLess stringifier](#stringifier)
7361

7462
### Rule node
7563
[PostCSS Rule Node](https://github.com/postcss/postcss/blob/master/docs/api.md#rule-node)
@@ -161,6 +149,39 @@ const root = postCssLess.parse('// Hello world');
161149
root.first.raws.content // => '// Hello world'
162150
````
163151

152+
### Stringifier
153+
154+
If you need to have LESS code without PostCSS transformation, you have to specify a custom stringifier:
155+
156+
````js
157+
import postcss from 'postcss';
158+
import postcssLess from 'postcss-less';
159+
import stringify from 'postcss-less/less-stringify';
160+
161+
const lessCode = `
162+
// Non-css comment
163+
164+
.container {
165+
.mixin-1();
166+
.mixin-2;
167+
.mixin-3 (@width: 100px) {
168+
width: @width;
169+
}
170+
}
171+
172+
.rotation(@deg:5deg){
173+
.transform(rotate(@deg));
174+
}
175+
`;
176+
177+
postcss().process(less, {
178+
syntax: postcssLess,
179+
stringifier: stringify
180+
}).then((result) => {
181+
console.log(result.content); // this will be value of `lessCode` without changing of comment nodes and mixins
182+
});
183+
````
184+
164185
## Contribution
165186
Please, check our guidelines: [CONTRIBUTING.md](./CONTRIBUTING.md)
166187

gulpfile.babel.js

-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import babel from 'gulp-babel';
77
import clean from 'gulp-rimraf';
88
import eslint from 'gulp-eslint';
99
import gulp from 'gulp';
10-
import jscs from 'gulp-jscs';
1110
import mocha from 'gulp-mocha';
1211
import path from 'path';
1312
import postcss from 'postcss';
@@ -37,9 +36,6 @@ const config = {
3736
function lint (srcPath) {
3837
return gulp
3938
.src(srcPath)
40-
.pipe(jscs())
41-
.pipe(jscs.reporter())
42-
.pipe(jscs.reporter('failImmediately'))
4339
.pipe(eslint())
4440
.pipe(eslint.format())
4541
.pipe(eslint.failAfterError());

lib/less-parser.js

+3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ export default class LessParser extends Parser {
8585
delete this.current.nodes;
8686
this.current.extendRule = this.current.selector.indexOf('&:extend') >= 0;
8787
this.current.important = this.current.selector.indexOf('!important') >= 0;
88+
89+
this.pos--;
90+
8891
this.end(this.tokens[this.pos]);
8992
}
9093

lib/less-stringifier.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,31 @@ export default class LessStringifier extends Stringifier {
1111
this.builder(`/*${ left }${ node.text }${ right }*/`, node);
1212
}
1313
}
14-
}
14+
15+
block (node, start) {
16+
const {ruleWithoutBody} = node;
17+
const between = this.raw(node, 'between', 'beforeOpen');
18+
let after = '';
19+
20+
if (ruleWithoutBody) {
21+
this.builder(start + between, node, 'start');
22+
} else {
23+
this.builder(`${ start + between }{`, node, 'start');
24+
}
25+
26+
if (node.nodes && node.nodes.length) {
27+
this.body(node);
28+
after = this.raw(node, 'after');
29+
} else {
30+
after = this.raw(node, 'after', 'emptyBody');
31+
}
32+
33+
if (after) {
34+
this.builder(after);
35+
}
36+
37+
if (!ruleWithoutBody) {
38+
this.builder('}', node, 'end');
39+
}
40+
}
41+
}

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-less",
3-
"version": "0.10.0",
3+
"version": "0.11.0",
44
"description": "LESS parser for PostCSS",
55
"keywords": [
66
"css",
@@ -9,6 +9,10 @@
99
"parser",
1010
"less"
1111
],
12+
"main": "dist/less-syntax.js",
13+
"files": [
14+
"dist/*.js"
15+
],
1216
"author": "Denys Kniazevych <webschik@gmail.com>",
1317
"contributors": [
1418
"Andrew Powell <andrew@shellscape.org>",
@@ -37,13 +41,11 @@
3741
"babel-preset-es2015": "^6.6.0",
3842
"babel-register": "^6.7.2",
3943
"chai": "^3.5.0",
40-
"eslint": "^2.5.1",
4144
"eslint-config-strict": "^8.5.0",
4245
"eslint-plugin-filenames": "^0.2.0",
4346
"gulp": "^3.9.1",
4447
"gulp-babel": "^6.1.2",
4548
"gulp-eslint": "^2.0.0",
46-
"gulp-jscs": "^3.0.2",
4749
"gulp-json-editor": "2.2.1",
4850
"gulp-mocha": "^2.2.0",
4951
"gulp-rename": "^1.2.2",

test/stringify.spec.js

+62-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
import cases from 'postcss-parser-tests';
55
import {expect} from 'chai';
6-
import parse from '../lib/less-parse';
7-
import stringify from '../lib/less-stringify';
6+
import parse from './../lib/less-parse';
7+
import postcss from 'postcss';
8+
import postcssLess from 'postcss-less';
9+
import stringify from 'postcss-less/less-stringify';
810

911
describe('#stringify()', () => {
1012
describe('CSS for PostCSS', () => {
@@ -49,17 +51,73 @@ describe('#stringify()', () => {
4951
expect(result).to.eql('// comment ');
5052
});
5153
});
52-
54+
55+
describe('Extend', () => {
56+
it('stringifies mixin without body. #1', (done) => {
57+
const less = '.selector:extend(.f, .g) {&:extend(.a);}';
58+
59+
postcss().process(less, {
60+
syntax: postcssLess,
61+
stringifier: stringify
62+
}).then((result) => {
63+
expect(result.content).to.eql(less);
64+
done();
65+
}).catch((error) => {
66+
done(error);
67+
});
68+
});
69+
});
70+
5371
describe('Mixins', () => {
5472
it('stringifies mixins', () => {
5573
const root = parse('.foo (@bar; @baz...) { border: @{baz}; }');
5674
let result = '';
57-
75+
5876
stringify(root, (i) => {
5977
result += i;
6078
});
6179

6280
expect(result).to.eql('.foo (@bar; @baz...) { border: @{baz}; }');
6381
});
82+
83+
it('stringifies mixin without body. #1', (done) => {
84+
const less = '.mix() {color: red} .selector {.mix()}';
85+
86+
postcss().process(less, {
87+
syntax: postcssLess,
88+
stringifier: stringify
89+
}).then((result) => {
90+
expect(result.content).to.eql(less);
91+
done();
92+
}).catch((error) => {
93+
done(error);
94+
});
95+
});
96+
97+
it('stringifies mixin without body. #2', (done) => {
98+
const less = `
99+
.container {
100+
.mixin-1();
101+
.mixin-2;
102+
.mixin-3 (@width: 100px) {
103+
width: @width;
104+
}
105+
}
106+
107+
.rotation(@deg:5deg){
108+
.transform(rotate(@deg));
109+
}
110+
`;
111+
112+
postcss().process(less, {
113+
syntax: postcssLess,
114+
stringifier: stringify
115+
}).then((result) => {
116+
expect(result.content).to.eql(less);
117+
done();
118+
}).catch((error) => {
119+
done(error);
120+
});
121+
});
64122
});
65123
});

0 commit comments

Comments
 (0)