From d54251276aae794c6f6f796d6d9b0a40f79a00ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Calvin=20Ju=C3=A1rez?= Date: Mon, 2 Jul 2018 20:45:29 -0600 Subject: [PATCH] Plugins: If minVersion >= 3.0.0, don't "pre-run" .setOptions() (#3247) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * dev – add .editorconfig * AbstractPluginLoader – check for minVersion < 3 before running .setOptions() twice * tests – separate unspecified minVersion test from minVersion: [2,0,0] test --- .editorconfig | 13 ++++++++ .../environment/abstract-plugin-loader.js | 15 ++++++--- test/less/plugin.less | 14 +++++++++ test/less/plugin/plugin-preeval.js | 3 +- test/less/plugin/plugin-set-options-v2.js | 31 +++++++++++++++++++ test/less/plugin/plugin-set-options-v3.js | 31 +++++++++++++++++++ 6 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 .editorconfig create mode 100644 test/less/plugin/plugin-set-options-v2.js create mode 100644 test/less/plugin/plugin-set-options-v3.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..aa5855a8a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# @see http://editorconfig.org/ + +# the buck stops here +root = true + +# all files +[*] +end_of_line = LF +indent_style = space +indent_size = 4 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/lib/less/environment/abstract-plugin-loader.js b/lib/less/environment/abstract-plugin-loader.js index 6fb0e7522..ba3b6771a 100644 --- a/lib/less/environment/abstract-plugin-loader.js +++ b/lib/less/environment/abstract-plugin-loader.js @@ -64,7 +64,8 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, imports, try { loader = new Function('module', 'require', 'registerPlugin', 'functions', 'tree', 'less', 'fileInfo', contents); loader(localModule, this.require(filename), registerPlugin, registry, this.less.tree, this.less, fileInfo); - } catch (e) { + } + catch (e) { return new LessError(e, imports, filename); } @@ -78,12 +79,16 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, imports, } if (pluginObj) { - // For 2.x back-compatibility - setOptions() before install() pluginObj.imports = imports; pluginObj.filename = filename; - result = this.trySetOptions(pluginObj, filename, shortname, pluginOptions); - if (result) { - return result; + + // For < 3.x (or unspecified minVersion) - setOptions() before install() + if (!pluginObj.minVersion || this.compareVersion('3.0.0', pluginObj.minVersion) < 0) { + result = this.trySetOptions(pluginObj, filename, shortname, pluginOptions); + + if (result) { + return result; + } } // Run on first load diff --git a/test/less/plugin.less b/test/less/plugin.less index 0d880cee7..0d86bc8d7 100644 --- a/test/less/plugin.less +++ b/test/less/plugin.less @@ -120,9 +120,23 @@ test-atrule("@charset"; '"utf-8"'); test-atrule("@arbitrary"; "value after ()"); +// no minVersion specified @plugin (option1) "./plugin/plugin-set-options"; @plugin "./plugin/plugin-set-options"; @plugin (option2) "./plugin/plugin-set-options"; @plugin "./plugin/plugin-set-options"; @plugin (option3) "./plugin/plugin-set-options"; +// specifies minVersion: [2,0,0] +@plugin (option1) "./plugin/plugin-set-options-v2"; +@plugin "./plugin/plugin-set-options-v2"; +@plugin (option2) "./plugin/plugin-set-options-v2"; +@plugin "./plugin/plugin-set-options-v2"; +@plugin (option3) "./plugin/plugin-set-options-v2"; + +// specifies minVersion: [3,0,0] +@plugin (option1) "./plugin/plugin-set-options-v3"; +@plugin "./plugin/plugin-set-options-v3"; +@plugin (option2) "./plugin/plugin-set-options-v3"; +@plugin "./plugin/plugin-set-options-v3"; +@plugin (option3) "./plugin/plugin-set-options-v3"; diff --git a/test/less/plugin/plugin-preeval.js b/test/less/plugin/plugin-preeval.js index 484a0abba..96bc11d39 100644 --- a/test/less/plugin/plugin-preeval.js +++ b/test/less/plugin/plugin-preeval.js @@ -22,5 +22,6 @@ module.exports = { manager.addVisitor(new Visitor()); // console.log(manager); - } + }, + minVersion: [2,0,0] }; diff --git a/test/less/plugin/plugin-set-options-v2.js b/test/less/plugin/plugin-set-options-v2.js new file mode 100644 index 000000000..002c0fd1e --- /dev/null +++ b/test/less/plugin/plugin-set-options-v2.js @@ -0,0 +1,31 @@ +var optionsStack = [ + 'option1', + undefined, + 'option2', + undefined, + 'option3' +]; + +var options, error; + +registerPlugin({ + install: function(less, pluginManager, functions) { + if (!options) { + error = 'setOptions() not called before install'; + } + }, + use: function() { + var pos = optionsStack.indexOf(options); + + if (pos === -1) { + error = 'setOptions() not setting option "' + opt + '" correctly'; + } + if (error) { + throw new Error(error); + } + }, + setOptions: function(opts) { + options = opts; + }, + minVersion: [2,0,0] +}); diff --git a/test/less/plugin/plugin-set-options-v3.js b/test/less/plugin/plugin-set-options-v3.js new file mode 100644 index 000000000..bf1cf70f0 --- /dev/null +++ b/test/less/plugin/plugin-set-options-v3.js @@ -0,0 +1,31 @@ +var optionsStack = [ + 'option1', + undefined, + 'option2', + undefined, + 'option3' +]; + +var options, error; + +registerPlugin({ + install: function(less, pluginManager, functions) { + if (options) { + error = 'setOptions() called before install'; + } + }, + use: function() { + var pos = optionsStack.indexOf(options); + + if (pos === -1) { + error = 'setOptions() not setting option "' + opt + '" correctly'; + } + if (error) { + throw new Error(error); + } + }, + setOptions: function(opts) { + options = opts; + }, + minVersion: [3,0,0] +});