From 94073827ef5d64fffb88d310e4072e8b190f784d Mon Sep 17 00:00:00 2001 From: Thomas Vantuycom Date: Fri, 16 Mar 2018 13:52:20 +0100 Subject: [PATCH 1/2] Add lint task --- package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/package.json b/package.json index fcfc2cd..163a0a8 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,8 @@ "view" ], "scripts": { + "lint": "eslint .", + "pretest": "npm run lint", "test": "mocha" } } From c30d153e14100912032205dbd69bbcf38e53debd Mon Sep 17 00:00:00 2001 From: Thomas Vantuycom Date: Fri, 16 Mar 2018 13:58:12 +0100 Subject: [PATCH 2/2] Fix linting errors --- .eslintrc | 4 +++ examples/express.js | 11 ++++--- index.js | 2 +- lib/consolidate.js | 24 +++++++-------- test/shared/dust.js | 2 +- test/shared/filters.js | 13 ++++---- test/shared/helpers.js | 67 +++++++++++++++++++++-------------------- test/shared/includes.js | 8 ++--- test/shared/index.js | 12 ++++---- test/shared/partials.js | 13 ++++---- test/shared/react.js | 14 ++++----- 11 files changed, 87 insertions(+), 83 deletions(-) diff --git a/.eslintrc b/.eslintrc index 223c519..8ccc3bd 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,4 +1,8 @@ { + "env": { + "node": true, + "mocha": true + }, "extends": "standard", "rules": { "padded-blocks": 0, diff --git a/examples/express.js b/examples/express.js index 2c84ca9..64278e7 100644 --- a/examples/express.js +++ b/examples/express.js @@ -1,12 +1,13 @@ // npm install express -var express = require('../../express') - , cons = require('../') - , app = express(); +var express = require('../../express'); +var cons = require('../'); +var app = express(); +var path = require('path'); app.engine('html', cons.swig); app.set('view engine', 'html'); -app.set('views', __dirname + '/views'); +app.set('views', path.join(__dirname, 'views')); var users = []; users.push({ name: 'tobi' }); @@ -27,4 +28,4 @@ app.get('/users', function(req, res){ }); app.listen(3000); -console.log('Express server listening on port 3000'); \ No newline at end of file +console.log('Express server listening on port 3000'); diff --git a/index.js b/index.js index 4a0df17..3a1d52c 100644 --- a/index.js +++ b/index.js @@ -1 +1 @@ -module.exports = require('./lib/consolidate'); \ No newline at end of file +module.exports = require('./lib/consolidate'); diff --git a/lib/consolidate.js b/lib/consolidate.js index 37386bf..ec27502 100644 --- a/lib/consolidate.js +++ b/lib/consolidate.js @@ -337,7 +337,7 @@ exports.liquid.render = function(str, options, fn){ // set up liquid-node engine try { Liquid = requires.liquid = require('liquid-node'); - engine = new Liquid.Engine; + engine = new Liquid.Engine(); } catch (err) { throw err; } @@ -376,10 +376,6 @@ exports.liquid.render = function(str, options, fn){ * callback: */ - var compileOptions = { - customTags: {} - }; - if (options.customTags){ var tagFunctions = options.customTags; @@ -1178,10 +1174,10 @@ exports.dot.render = function (str, options, fn) { var engine = requires.dot || (requires.dot = require('dot')); var extend = (requires.extend || (requires.extend = require('util')._extend)); try { - var settings = {}; - settings = extend(settings, engine.templateSettings); - settings = extend(settings, options ? options.dot : {}); - var tmpl = cache(options) || cache(options, engine.template(str, settings, options)); + var settings = {}; + settings = extend(settings, engine.templateSettings); + settings = extend(settings, options ? options.dot : {}); + var tmpl = cache(options) || cache(options, engine.template(str, settings, options)); fn(null, tmpl(options)); } catch (err) { fn(err); @@ -1236,7 +1232,8 @@ exports.ractive.render = function(str, options, fn){ options.data = extend({}, options); // Remove consolidate-specific properties from the clone - var i, length; + var i; + var length; var properties = ["template", "filename", "cache", "partials"]; for (i = 0, length = properties.length; i < length; i++) { var property = properties[i]; @@ -1377,7 +1374,7 @@ function requireReactString(src, filename) { if (!filename) filename = ''; var m = new module.constructor(); - filename = filename || ''; + filename = filename || ''; // Compile Using React var compiled = babel.transform(src, { presets: [ 'react' ] }).code; @@ -1594,6 +1591,7 @@ exports.vash.render = function(str, options, fn) { var tmpl = cache(options) || cache(options, engine.compile(str, options)); tmpl(options, function sealLayout(err, ctx) { + if(err) fn(err); ctx.finishLayout(); fn(null, ctx.toString().replace(/\n$/, '')); }); @@ -1670,7 +1668,7 @@ exports.teacup = function(path, options, fn) { return promisify(fn, function(fn) { var engine = requires.teacup || (requires.teacup = require('teacup/lib/express')); require.extensions['.teacup'] = require.extensions['.coffee']; - if (path[0] != '/') { + if (path[0] !== '/') { path = join(process.cwd(), path); } if (!options.cache) { @@ -1704,4 +1702,4 @@ exports.teacup.render = function(str, options, fn){ /** * expose the instance of the engine */ - exports.requires = requires; +exports.requires = requires; diff --git a/test/shared/dust.js b/test/shared/dust.js index bd2503a..f4dd241 100644 --- a/test/shared/dust.js +++ b/test/shared/dust.js @@ -22,7 +22,7 @@ exports.test = function(name) { if (name === 'dust') { var dust = require('dustjs-helpers'); - dust.helpers.templateName = function(chunk, context, bodies, params) { + dust.helpers.templateName = function(chunk, context) { return chunk.write(context.getTemplateName()); }; cons.requires.dust = dust; diff --git a/test/shared/filters.js b/test/shared/filters.js index 625a227..f4d619a 100644 --- a/test/shared/filters.js +++ b/test/shared/filters.js @@ -1,7 +1,5 @@ -var cons = require('../../') - , fs = require('fs'); - -var should = require('should'); +var cons = require('../../'); +var fs = require('fs'); exports.test = function(name) { var user = { name: 'Tobi' }; @@ -12,9 +10,10 @@ exports.test = function(name) { it('should support filters', function(done) { var str = fs.readFileSync('test/fixtures/' + name + '/filters.' + name).toString(); - var locals = { user: user, filters: { toupper: function(object) { - return object.toUpperCase(); - }}}; + var locals = { user: user, + filters: { toupper: function(object) { + return object.toUpperCase(); + }}}; cons[name].render(str, locals, function(err, html){ if (err) return done(err); diff --git a/test/shared/helpers.js b/test/shared/helpers.js index 0553a9c..f2c0a16 100644 --- a/test/shared/helpers.js +++ b/test/shared/helpers.js @@ -1,10 +1,9 @@ -var cons = require('../../') - , handlebars = require('handlebars') - , vash = require('vash') - , fs = require('fs') - , readFile = fs.readFile - , readFileSync = fs.readFileSync; +var cons = require('../../'); +var handlebars = require('handlebars'); +var fs = require('fs'); +var readFile = fs.readFile; +var readFileSync = fs.readFileSync; exports.test = function(name) { @@ -15,43 +14,47 @@ exports.test = function(name) { fs.readFileSync = readFileSync; }); + var user; + if (name === 'handlebars') { - var user = { name: 'Tobi' }; + user = { name: 'Tobi' }; // Use case: return safe HTML that won’t be escaped in the final render. - it('should support helpers', function(done) { - var str = fs.readFileSync('test/fixtures/' + name + '/helpers.' + name).toString(); - - var locals = { user: user, helpers: { safe: function(object) { - return new handlebars.SafeString(object); - }}}; - - cons[name].render(str, locals, function(err, html){ - if (err) return done(err); - html.should.equal('Tobi'); - done(); - }); + it('should support helpers', function(done) { + var str = fs.readFileSync('test/fixtures/' + name + '/helpers.' + name).toString(); + + var locals = { user: user, + helpers: { safe: function(object) { + return new handlebars.SafeString(object); + }}}; + + cons[name].render(str, locals, function(err, html){ + if (err) return done(err); + html.should.equal('Tobi'); + done(); }); + }); } if (name === 'vash') { - var user = { name: 'Tobi' }; + user = { name: 'Tobi' }; // See this for Vash helper system : https://github.com/kirbysayshi/vash#helper-system // Use case: return as as lower case - it('should support helpers', function(done) { - var str = fs.readFileSync('test/fixtures/' + name + '/helpers.' + name).toString(); - - var locals = { user: user, helpers: { lowerCase: function(text) { - return text.toLowerCase(); - }}}; - - cons[name].render(str, locals, function(err, html){ - if (err) return done(err); - html.should.equal('tobi'); - done(); - }); + it('should support helpers', function(done) { + var str = fs.readFileSync('test/fixtures/' + name + '/helpers.' + name).toString(); + + var locals = { user: user, + helpers: { lowerCase: function(text) { + return text.toLowerCase(); + }}}; + + cons[name].render(str, locals, function(err, html){ + if (err) return done(err); + html.should.equal('tobi'); + done(); }); + }); } }); }; diff --git a/test/shared/includes.js b/test/shared/includes.js index 7224e23..eb4879d 100644 --- a/test/shared/includes.js +++ b/test/shared/includes.js @@ -1,7 +1,5 @@ -var cons = require('../../') - , fs = require('fs'); - -var should = require('should'); +var cons = require('../../'); +var fs = require('fs'); exports.test = function(name) { var user = { name: 'Tobi' }; @@ -33,7 +31,7 @@ exports.test = function(name) { }); }); - if (name == 'nunjucks') { + if (name === 'nunjucks') { it('should support extending views', function (done) { var str = fs.readFileSync('test/fixtures/' + name + '/layouts.' + name).toString(); diff --git a/test/shared/index.js b/test/shared/index.js index 0d510fa..a15611b 100644 --- a/test/shared/index.js +++ b/test/shared/index.js @@ -1,8 +1,8 @@ -var cons = require('../../') - , fs = require('fs') - , readFile = fs.readFile - , readFileSync = fs.readFileSync; +var cons = require('../../'); +var fs = require('fs'); +var readFile = fs.readFile; +var readFileSync = fs.readFileSync; exports.test = function(name) { var user = { name: 'Tobi' }; @@ -109,8 +109,8 @@ exports.test = function(name) { }); it('should be exposed in the requires object', function(){ - var should = require('should'), - requiredName = name; + var should = require('should'); + var requiredName = name; should.exist(cons.requires[requiredName]); }); }); diff --git a/test/shared/partials.js b/test/shared/partials.js index 5982b6c..3996439 100644 --- a/test/shared/partials.js +++ b/test/shared/partials.js @@ -1,8 +1,9 @@ -var cons = require('../../') - , fs = require('fs') - , readFile = fs.readFile - , readFileSync = fs.readFileSync; +var cons = require('../../'); +var join = require('path').join; +var fs = require('fs'); +var readFile = fs.readFile; +var readFileSync = fs.readFileSync; exports.test = function(name) { var user = { name: 'Tobi' }; @@ -13,7 +14,7 @@ exports.test = function(name) { fs.readFileSync = readFileSync; }); - if (name == 'hogan' || name == 'mustache' || name == 'handlebars' || name == 'ractive') { + if (name === 'hogan' || name === 'mustache' || name === 'handlebars' || name === 'ractive') { it('should support partials', function(done){ var path = 'test/fixtures/' + name + '/partials.' + name; var locals = { user: user, partials: { partial: 'user' } }; @@ -25,7 +26,7 @@ exports.test = function(name) { }); it('should support absolute path partial', function(done){ var path = 'test/fixtures/' + name + '/partials.' + name; - var locals = {user: user, partials: {partial: __dirname + '/../../test/fixtures/' + name + '/user' }}; + var locals = {user: user, partials: {partial: join(__dirname, '/../../test/fixtures/' , name, '/user') }}; cons[name](path, locals, function(err, html){ if (err) return done(err); html.should.equal('

Tobi

'); diff --git a/test/shared/react.js b/test/shared/react.js index 481a399..01cf70c 100644 --- a/test/shared/react.js +++ b/test/shared/react.js @@ -1,10 +1,10 @@ /*eslint-env node, mocha */ /*eslint quotes: [2, "single"] */ -var consolidate = require('../../'), - fs = require('fs'); +var consolidate = require('../../'); +var fs = require('fs'); -var readFile = fs.readFile, - readFileSync = fs.readFileSync; +var readFile = fs.readFile; +var readFileSync = fs.readFileSync; exports.test = function(name) { @@ -82,9 +82,9 @@ exports.test = function(name) { it('should support rendering into a base template', function(done){ var path = 'test/fixtures/' + name + '/user.' + name; var locals = { - user: user, - base: 'test/fixtures/' + name + '/base.html', - title: 'My Title' + user: user, + base: 'test/fixtures/' + name + '/base.html', + title: 'My Title' }; cons[name](path, locals, function(err, html){