From fda4565e8d447e458e357195d54f0ea896042094 Mon Sep 17 00:00:00 2001 From: Matthew Dean Date: Sat, 31 Dec 2016 11:53:53 -0800 Subject: [PATCH] Replace "return {}" statement in plugins with explicit registerPlugin() function (more logical) --- .../environment/abstract-plugin-loader.js | 22 ++++++++----------- test/browser/less/plugin/plugin.js | 4 ++-- test/less/errors/plugin-1.less | 1 + test/less/errors/plugin-1.txt | 1 + test/less/errors/plugin/plugin-error.js | 3 +++ test/less/plugin/plugin-local.js | 4 ++-- 6 files changed, 18 insertions(+), 17 deletions(-) create mode 100644 test/less/errors/plugin-1.less create mode 100644 test/less/errors/plugin-1.txt create mode 100644 test/less/errors/plugin/plugin-error.js diff --git a/lib/less/environment/abstract-plugin-loader.js b/lib/less/environment/abstract-plugin-loader.js index ab8b5c5b..dc674391 100644 --- a/lib/less/environment/abstract-plugin-loader.js +++ b/lib/less/environment/abstract-plugin-loader.js @@ -12,7 +12,7 @@ function error(msg, type) { } ); } -AbstractPluginLoader.prototype.evalPlugin = function(contents, context, pluginOptions, fileInfo) { +AbstractPluginLoader.prototype.evalPlugin = function(contents, context, imports, pluginOptions, fileInfo) { var loader, registry, @@ -54,8 +54,11 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, pluginOp registry = functionRegistry.create(); try { - loader = new Function("module", "require", "functions", "tree", "less", "fileInfo", contents); - pluginObj = loader(localModule, this.require, registry, this.less.tree, this.less, fileInfo); + var registerPlugin = function(obj) { + pluginObj = obj; + }; + loader = new Function("module", "require", "registerPlugin", "functions", "tree", "less", "fileInfo", contents); + loader(localModule, this.require, registerPlugin, registry, this.less.tree, this.less, fileInfo); if (!pluginObj) { pluginObj = localModule.exports; @@ -78,15 +81,8 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, pluginOp return new this.less.LessError({ message: "Not a valid plugin" }); } - } catch(e) { - // TODO pass the error - console.log(e.stack); - return new this.less.LessError({ - message: "Plugin evaluation error: '" + e.name + ': ' + e.message.replace(/["]/g, "'") + "'" , - filename: filename, - line: this.line, - col: this.column - }); + } catch (e) { + return new this.less.LessError(e, imports, filename); } return pluginObj; @@ -102,7 +98,7 @@ AbstractPluginLoader.prototype.trySetOptions = function(plugin, filename, name, try { plugin.setOptions(options); } - catch(e) { + catch (e) { error("Error setting options on plugin " + name + '\n' + e.message); return null; } diff --git a/test/browser/less/plugin/plugin.js b/test/browser/less/plugin/plugin.js index 1e931025..78fa62b2 100644 --- a/test/browser/less/plugin/plugin.js +++ b/test/browser/less/plugin/plugin.js @@ -1,7 +1,7 @@ -return { +registerPlugin({ install: function(less, pluginManager, functions) { functions.add('func', function() { return less.Anonymous(location.href); }); } -} \ No newline at end of file +}); \ No newline at end of file diff --git a/test/less/errors/plugin-1.less b/test/less/errors/plugin-1.less new file mode 100644 index 00000000..66813d30 --- /dev/null +++ b/test/less/errors/plugin-1.less @@ -0,0 +1 @@ +@plugin "plugin/plugin-error"; \ No newline at end of file diff --git a/test/less/errors/plugin-1.txt b/test/less/errors/plugin-1.txt new file mode 100644 index 00000000..9cfbe0b9 --- /dev/null +++ b/test/less/errors/plugin-1.txt @@ -0,0 +1 @@ +SyntaxError: Unexpected token ) in {path}plugin/plugin-error.js diff --git a/test/less/errors/plugin/plugin-error.js b/test/less/errors/plugin/plugin-error.js new file mode 100644 index 00000000..3b2d317f --- /dev/null +++ b/test/less/errors/plugin/plugin-error.js @@ -0,0 +1,3 @@ +// jscs:disable +functions.addMultiple({ + "test-parse-error" : function() { \ No newline at end of file diff --git a/test/less/plugin/plugin-local.js b/test/less/plugin/plugin-local.js index d074b56c..30be79d1 100644 --- a/test/less/plugin/plugin-local.js +++ b/test/less/plugin/plugin-local.js @@ -7,8 +7,8 @@ functions.addMultiple({ } }); -return { +registerPlugin({ setOptions: function(raw) { // do nothing } -} \ No newline at end of file +}); \ No newline at end of file