Skip to content

Commit

Permalink
Plugins: If minVersion >= 3.0.0, don't "pre-run" .setOptions() (#3247)
Browse files Browse the repository at this point in the history
* dev – add .editorconfig
* AbstractPluginLoader – check for minVersion < 3 before running .setOptions() twice
* tests – separate unspecified minVersion test from minVersion: [2,0,0] test
  • Loading branch information
calvinjuarez authored and matthew-dean committed Jul 3, 2018
1 parent 39ef69c commit d542512
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 6 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
15 changes: 10 additions & 5 deletions lib/less/environment/abstract-plugin-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/less/plugin.less
Original file line number Diff line number Diff line change
Expand Up @@ -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";
3 changes: 2 additions & 1 deletion test/less/plugin/plugin-preeval.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ module.exports = {

manager.addVisitor(new Visitor());
// console.log(manager);
}
},
minVersion: [2,0,0]
};
31 changes: 31 additions & 0 deletions test/less/plugin/plugin-set-options-v2.js
Original file line number Diff line number Diff line change
@@ -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]
});
31 changes: 31 additions & 0 deletions test/less/plugin/plugin-set-options-v3.js
Original file line number Diff line number Diff line change
@@ -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]
});

0 comments on commit d542512

Please sign in to comment.