From 74ef1ebacdf4ff51506d096efa8cb752ca413a8f Mon Sep 17 00:00:00 2001 From: Jacques Favreau Date: Sat, 29 Aug 2015 23:54:26 -0700 Subject: [PATCH] Reference inline comments. As described in https://github.com/less/less.js/issues/2675 in-value comments are not preserved in referenced rules. This patch adds reference marking to nodes below rules and expressions if markReferenced is available. --- .gitignore | 1 + lib/less/tree/expression.js | 5 +++++ lib/less/tree/rule.js | 20 +++++++++++++++++++- test/css/import-reference.css | 2 +- test/less/import/import-reference.less | 2 +- 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index f461f61dd..52a8aabf4 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *~ .#* .idea +*.iml *.sublime-* # npm diff --git a/lib/less/tree/expression.js b/lib/less/tree/expression.js index 4f939a6f4..8773a4b4e 100644 --- a/lib/less/tree/expression.js +++ b/lib/less/tree/expression.js @@ -53,4 +53,9 @@ Expression.prototype.throwAwayComments = function () { return !(v instanceof Comment); }); }; +Expression.prototype.markReferenced = function () { + this.value.forEach(function (value) { + if (value.markReferenced) { value.markReferenced(); } + }); +}; module.exports = Expression; diff --git a/lib/less/tree/rule.js b/lib/less/tree/rule.js index cd2fe0c48..5b9d2c138 100644 --- a/lib/less/tree/rule.js +++ b/lib/less/tree/rule.js @@ -92,4 +92,22 @@ Rule.prototype.makeImportant = function () { this.index, this.currentFileInfo, this.inline); }; -module.exports = Rule; +// Recursive marking for rules +var mark = function(value) { + if (!Array.isArray(value)) { + if (value.markReferenced) { + value.markReferenced(); + } + } else { + value.forEach(function (ar) { + mark(ar); + }); + } +}; +Rule.prototype.markReferenced = function () { + if (this.value) { + mark(this.value); + } +}; + +module.exports = Rule; \ No newline at end of file diff --git a/test/css/import-reference.css b/test/css/import-reference.css index 7aa622c65..03e0fc039 100644 --- a/test/css/import-reference.css +++ b/test/css/import-reference.css @@ -59,7 +59,7 @@ div#id.class[a=1][b=2].class:not(1) { color: green; } .y { - pulled-in: yes; + pulled-in: yes /* inline comment survives */; } /* comment pulled in */ .visible { diff --git a/test/less/import/import-reference.less b/test/less/import/import-reference.less index cadf09f65..8f9002e8c 100644 --- a/test/less/import/import-reference.less +++ b/test/less/import/import-reference.less @@ -37,7 +37,7 @@ .zz { .y { - pulled-in: yes; + pulled-in: yes /* inline comment survives */; } /* comment pulled in */ }