From 08ab9b8a1c68d9727a655097b10746a85eb07c1a Mon Sep 17 00:00:00 2001 From: Jonathan Wilsson Date: Sat, 1 Jul 2017 12:20:01 +0200 Subject: [PATCH 1/2] Supply a Root stringifier --- lib/less-parser.js | 9 +++++++++ lib/root.js | 14 ++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 lib/root.js diff --git a/lib/less-parser.js b/lib/less-parser.js index 7b64943..db5550f 100755 --- a/lib/less-parser.js +++ b/lib/less-parser.js @@ -2,6 +2,7 @@ import Comment from 'postcss/lib/comment'; import Import from './import'; import Parser from 'postcss/lib/parser'; import Rule from './rule'; +import Root from './root'; import findExtendRule from './find-extend-rule'; import isMixinToken from './is-mixin-token'; import lessTokenizer from './less-tokenize'; @@ -10,6 +11,14 @@ const blockCommentEndPattern = /\*\/$/; export default class LessParser extends Parser { + constructor (input) { + super(input); + + this.root = new Root(); + this.current = this.root; + this.root.source = { input, start: { line: 1, column: 1 } }; + } + atrule (token) { if (token[1] === '@import') { this.import(token); diff --git a/lib/root.js b/lib/root.js new file mode 100644 index 0000000..9e73405 --- /dev/null +++ b/lib/root.js @@ -0,0 +1,14 @@ +import PostCssRoot from 'postcss/lib/root'; +import stringify from './less-stringify'; + +export default class Root extends PostCssRoot { + toString (stringifier) { + if (!stringifier) { + stringifier = { + stringify + }; + } + + return super.toString(stringifier); + } +} From b37539a5f3e2dbcb999f0728d241cc1e3e595c3c Mon Sep 17 00:00:00 2001 From: Jonathan Wilsson Date: Sun, 2 Jul 2017 13:49:12 +0200 Subject: [PATCH 2/2] Add a Root node stringifier test --- test/postcss.spec.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/postcss.spec.js b/test/postcss.spec.js index ba7e48d..f4f15e3 100644 --- a/test/postcss.spec.js +++ b/test/postcss.spec.js @@ -62,4 +62,16 @@ describe('#postcss', () => { done(); }).catch(done); }); + + it('should create its own Root node stringifier (#82)', (done) => { + const lessText = '@import "foo.less"'; + + postcss() + .process(lessText, { syntax: lessSyntax }) + .then((result) => { + expect(result.root.toString()).to.eql(lessText); + + done(); + }).catch(done); + }); });