Skip to content

Commit 267076f

Browse files
committed
Use function declaration
Removes the need for prototypes, at the same time gives pretty feedback in stack traces. It is also recommended by the Geddy style guide.
1 parent 31dba51 commit 267076f

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

lib/ejs.js

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@
2121
var utils = require('./utils')
2222
, templateCache = {}
2323
, jsCache = {}
24-
, includeFile
25-
, includeSource
26-
, resolveInclude
27-
, compile
28-
, rethrow
2924
, _DEFAULT_DELIMITER = '%'
3025
, _REGEX_STRING = '(<%%)|(<%=)|(<%-)|(<%#)|(<%)|(%>)|(-%>)'
3126
, _OPTS_MAP = {
@@ -39,7 +34,7 @@ var utils = require('./utils')
3934
, client: 'client'
4035
};
4136

42-
resolveInclude = function (name, filename) {
37+
function resolveInclude(name, filename) {
4338
var path = require('path')
4439
, dirname = path.dirname
4540
, extname = path.extname
@@ -50,9 +45,9 @@ resolveInclude = function (name, filename) {
5045
includePath += '.ejs';
5146
}
5247
return includePath;
53-
};
48+
}
5449

55-
includeFile = function (path, options) {
50+
function includeFile(path, options) {
5651
var fn
5752
, opts = utils.shallowCopy({}, options || /* istanbul ignore next */ {})
5853
, fs = require('fs')
@@ -76,9 +71,9 @@ includeFile = function (path, options) {
7671
opts.filename = includePath;
7772
fn = exports.compile(template, opts);
7873
return fn;
79-
};
74+
}
8075

81-
includeSource = function (path, options) {
76+
function includeSource(path, options) {
8277
var opts = utils.shallowCopy({}, options || {})
8378
, fs = require('fs')
8479
, includePath
@@ -102,15 +97,15 @@ includeSource = function (path, options) {
10297
var templ = new Template(template, opts);
10398
templ.generateSource();
10499
return templ.source;
105-
};
100+
}
106101

107-
rethrow = function (err, str, filename, lineno){
102+
function rethrow(err, str, filename, lineno){
108103
var lines = str.split('\n')
109104
, start = Math.max(lineno - 3, 0)
110105
, end = Math.min(lines.length, lineno + 3);
111106

112107
// Error context
113-
var context = lines.slice(start, end).map(function(line, i){
108+
var context = lines.slice(start, end).map(function (line, i){
114109
var curr = i + start + 1;
115110
return (curr === lineno ? ' >> ' : ' ')
116111
+ curr
@@ -126,12 +121,13 @@ rethrow = function (err, str, filename, lineno){
126121
+ err.message;
127122

128123
throw err;
129-
};
124+
}
130125

131-
compile = exports.compile = function (template, opts) {
126+
function compile(template, opts) {
132127
var templ = new Template(template, opts);
133128
return templ.compile();
134-
};
129+
}
130+
exports.compile = compile;
135131

136132
// template, [data], [opts]
137133
// Have to include an empty data object if you want opts and no data
@@ -188,13 +184,12 @@ exports.renderFile = function () {
188184
, cb = args.pop()
189185
, data = args.shift() || {}
190186
, opts = args.pop() || {}
191-
, template
192-
, handleTemplate;
187+
, template;
193188

194189
// Set the filename for includes
195190
opts.filename = path;
196191

197-
handleTemplate = function (template) {
192+
function handleTemplate(template) {
198193
var result
199194
, failed = false;
200195
try {
@@ -207,7 +202,7 @@ exports.renderFile = function () {
207202
if (!failed) {
208203
cb(null, result);
209204
}
210-
};
205+
}
211206

212207
template = templateCache[path];
213208
if (opts.cache && template) {
@@ -233,7 +228,7 @@ exports.clearCache = function () {
233228
jsCache = {};
234229
};
235230

236-
var Template = function (text, opts) {
231+
function Template(text, opts) {
237232
opts = opts || {};
238233
var options = {};
239234
this.templateText = text;
@@ -251,10 +246,9 @@ var Template = function (text, opts) {
251246
this.opts = options;
252247

253248
this.regex = this.createRegex();
254-
};
249+
}
255250

256251
Template.prototype = new function () {
257-
258252
this.modes = {
259253
EVAL: 'eval'
260254
, ESCAPED: 'escaped'
@@ -411,10 +405,9 @@ Template.prototype = new function () {
411405
this.scanLine = function (line) {
412406
var self = this
413407
, d = this.opts.delimiter
414-
, newLineCount = 0
415-
, _addOutput;
408+
, newLineCount = 0;
416409

417-
_addOutput = function () {
410+
function _addOutput() {
418411
if (self.truncate) {
419412
line = line.replace('\n', '');
420413
}
@@ -430,7 +423,7 @@ Template.prototype = new function () {
430423
// - this will be the delimiter during execution
431424
line = line.replace(/"/g, '\\"');
432425
self.source += ';__output += "' + line + '";';
433-
};
426+
}
434427

435428
newLineCount = (line.split('\n').length - 1);
436429

0 commit comments

Comments
 (0)