From 92f33141fd6ca642b94f0ab9efa1c8dd30bd88e0 Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Sun, 20 Oct 2019 14:30:00 +0200 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20Set=C2=A0compiled=20function=C2=A0n?= =?UTF-8?q?ame?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ejs.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/ejs.js b/lib/ejs.js index 449d5da9..571c79f7 100755 --- a/lib/ejs.js +++ b/lib/ejs.js @@ -666,15 +666,10 @@ Template.prototype = { throw e; } - if (opts.client) { - fn.dependencies = this.dependencies; - return fn; - } - // Return a callable function which will execute the function // created by the source-code, with the passed data as locals // Adds a local `include` function which allows full recursive include - var returnedFn = function (data) { + var returnedFn = opts.client ? fn : function anonymous(data) { var include = function (path, includeData) { var d = utils.shallowCopy({}, data); if (includeData) { @@ -685,6 +680,18 @@ Template.prototype = { return fn.apply(opts.context, [data || {}, escapeFn, include, rethrow]); }; returnedFn.dependencies = this.dependencies; + if (opts.filename && typeof Object.defineProperty === 'function') { + var filename = opts.filename; + var basename = path.basename(filename, path.extname(filename)); + try { + Object.defineProperty(returnedFn, 'name', { + value: basename, + writable: false, + enumerable: false, + configurable: true + }); + } catch (e) {/* ignore */} + } return returnedFn; }, From ef38defe3d51229d9f1629eeffbdeb71850b85e7 Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Sun, 20 Oct 2019 18:45:00 +0200 Subject: [PATCH 2/2] =?UTF-8?q?test:=20Add=C2=A0tests=20for=C2=A0compiled?= =?UTF-8?q?=20function=C2=A0name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/ejs.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/ejs.js b/test/ejs.js index 795fc6d0..7bac967d 100755 --- a/test/ejs.js +++ b/test/ejs.js @@ -209,6 +209,24 @@ suite('ejs.compile(str, options)', function () { } throw new Error('no error reported when there should be'); }); + + var testFuncName = typeof Object.defineProperty === 'function' ? test : test.skip; + + testFuncName('Compiled function name matches `filename` without the extension', function (done) { + var func = ejs.compile('<%= "Foo" %>', { + filename: 'foo.ejs' + }); + + assert.ok(func.name === 'foo'); + return done(); + }); + + testFuncName('Compiled function name defaults to "anonymous" when `filename` is unspecified', function (done) { + var func = ejs.compile('<%= "Foo" %>'); + + assert.ok(func.name === 'anonymous'); + return done(); + }); }); suite('client mode', function () {