Skip to content

Commit

Permalink
cosmetic changes to all files to match new styles
Browse files Browse the repository at this point in the history
Implemented eslint and mostly airbnb's style guide.
  • Loading branch information
rcs committed Oct 10, 2016
1 parent 7134761 commit 0ce6f49
Show file tree
Hide file tree
Showing 15 changed files with 276 additions and 285 deletions.
23 changes: 12 additions & 11 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* eslint-disable no-console */

'use strict';
var gulp = require('gulp'),
childProcess = require('child_process'),
jshint = require('gulp-jshint');

var gulp = require('gulp');
var childProcess = require('child_process');
var eslint = require('gulp-eslint');

var realCodePaths = [
'**/*.{js,jsx,coffee}',
Expand All @@ -12,23 +14,22 @@ var realCodePaths = [
'!docs/**'
];

gulp.task('lint', function() {
gulp.task('lint', function () {
gulp.src(realCodePaths)
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'));
.pipe(eslint())
.pipe(eslint.format());
});

gulp.task('jsdoc', function() {
gulp.task('jsdoc', function () {
childProcess.exec(
'./node_modules/.bin/jsdoc -c jsdoc.json',
function(error,stdout,stderr) {
function (error, stdout, stderr) {
console.log(stdout);
console.error(stderr);
}
);
});

gulp.task('default',function() {
gulp.watch(realCodePaths, ['lint','jsdoc']);
gulp.task('default', function () {
gulp.watch(realCodePaths, ['lint', 'jsdoc']);
});

4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* @module Passage
*/

'use strict';

var Route = require('./lib/route');


module.exports = Route;
module.exports = Route;
11 changes: 4 additions & 7 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*jshint maxlen:120 */
// Karma configuration
// Generated on Sat Feb 15 2014 18:21:16 GMT-0800 (PST)

'use strict';

module.exports = function(config) {
module.exports = function (config) {
config.set({

// base path, that will be used to resolve files and exclude
Expand Down Expand Up @@ -49,18 +49,15 @@ module.exports = function(config) {
}
},



// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
// possible values: config.LOG_DISABLE || config.LOG_ERROR
// || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_DEBUG,


Expand Down
78 changes: 40 additions & 38 deletions lib/route.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
'use strict';
var Parser = require('./route/parser'),
RegexpVisitor = require('./route/visitors/regexp'),
ReverseVisitor = require('./route/visitors/reverse');

Route.prototype = Object.create(null)
var Parser = require('./route/parser');
var RegexpVisitor = require('./route/visitors/regexp');
var ReverseVisitor = require('./route/visitors/reverse');


/**
* Represents a route
* @example
* var route = Route('/:foo/:bar');
* @example
* var route = Route('/:foo/:bar');
* @param {string} spec - the string specification of the route.
* use :param for single portion captures, *param for splat style captures,
* and () for optional route branches
* @constructor
*/
function Route(spec) {
var route;
if (this) {
// constructor called with new
route = this;
} else {
// constructor called as a function
route = Object.create(Route.prototype);
}
if (typeof spec === 'undefined') {
throw new Error('A route spec is required');
}
route.spec = spec;
route.ast = Parser.parse(spec);
return route;
}

Route.prototype = Object.create(null);

/**
* Match a path against this route, returning the matched parameters if
Expand All @@ -18,12 +48,11 @@ Route.prototype = Object.create(null)
* @return {(Object.<string,string>|false)} A map of the matched route
* parameters, or false if matching failed
*/
Route.prototype.match = function(path) {
var re = RegexpVisitor.visit(this.ast),
matched = re.match(path);

return matched ? matched : false;
Route.prototype.match = function (path) {
var re = RegexpVisitor.visit(this.ast);
var matched = re.match(path);

return matched !== null ? matched : false;
};

/**
Expand All @@ -35,36 +64,9 @@ Route.prototype.match = function(path) {
* @param {Object} params The parameters to fill in
* @return {(String|false)} The filled in path
*/
Route.prototype.reverse = function(params) {
Route.prototype.reverse = function (params) {
return ReverseVisitor.visit(this.ast, params);
};

/**
* Represents a route
* @example
* var route = Route('/:foo/:bar');
* @example
* var route = Route('/:foo/:bar');
* @param {string} spec - the string specification of the route.
* use :param for single portion captures, *param for splat style captures,
* and () for optional route branches
* @constructor
*/
function Route(spec) {
var route;
if (this) {
// constructor called with new
route = this;
} else {
// constructor called as a function
route = Object.create(Route.prototype);
}
if( typeof spec === 'undefined' ) {
throw new Error('A route spec is required');
}
route.spec = spec;
route.ast = Parser.parse(spec);
return route;
}

module.exports = Route;
module.exports = Route;
53 changes: 24 additions & 29 deletions lib/route/grammar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** @module route/grammar */

'use strict';

/**
Expand All @@ -12,62 +13,56 @@
* @private
*/
function o(patternString, action) {
if( typeof action === 'undefined') {
if (typeof action === 'undefined') {
return [patternString, '$$ = $1;'];
}
else {
action = action.replace(/\bnew /g, '$&yy.');
return [patternString, '$$ = ' + action + ';'];
}

action = action.replace(/\bnew /g, '$&yy.');
return [patternString, '$$ = ' + action + ';'];
}


module.exports = {
'lex': {
'rules': [
['\\(', 'return "(";' ],
['\\)', 'return ")";' ],
['\\*+\\w+', 'return "SPLAT";' ],
[':+\\w+', 'return "PARAM";' ],
['[\\w%\\-~\\n]+', 'return "LITERAL";' ],
['.', 'return "LITERAL";' ],
['$', 'return "EOF";' ]
/* eslint-disable no-multi-spaces */
lex: {
rules: [
['\\(', 'return "(";'],
['\\)', 'return ")";'],
['\\*+\\w+', 'return "SPLAT";'],
[':+\\w+', 'return "PARAM";'],
['[\\w%\\-~\\n]+', 'return "LITERAL";'],
['.', 'return "LITERAL";'],
['$', 'return "EOF";']
]
},
'bnf': {
'root': [
bnf: {
root: [
['expressions EOF', 'return new yy.Root({},[$1])'],
['EOF', 'return new yy.Root({},[new yy.Literal({value: \'\'})])']
],
'expressions': [
expressions: [
o('expressions expression', 'new Concat({},[$1,$2])'),
o('expression')
],
'expression': [
expression: [
o('optional'),
o('literal', 'new Literal({value: $1})'),
o('splat', 'new Splat({name: $1})'),
o('param', 'new Param({name: $1})')
],
'optional': [
optional: [
o('( expressions )', 'new Optional({},[$2])')
],
'literal': [
literal: [
o('LITERAL', 'yytext')
],
'splat': [
splat: [
o('SPLAT', 'yytext.slice(1)')
],
'param': [
param: [
o('PARAM', 'yytext.slice(1)')
]
},
'startSymbol': 'root'
startSymbol: 'root'
/* eslint-enable no-multi-spaces */
};


//var parser = new (require('jison').Parser)(grammar);
//parser.yy = require('./nodes');

//module.exports = parser;
3 changes: 2 additions & 1 deletion lib/route/nodes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

/** @module route/nodes */


Expand All @@ -10,7 +11,7 @@
* @return {{displayName: string, props: Object, children: Array}}
*/
function createNode(displayName) {
return function(props, children) {
return function (props, children) {
return {
displayName: displayName,
props: props,
Expand Down
1 change: 1 addition & 0 deletions lib/route/parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @module route/parser
*/

'use strict';

/** Wrap the compiled parser with the context to create node objects */
Expand Down
10 changes: 5 additions & 5 deletions lib/route/visitors/create_visitor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

/**
* @module route/visitors/create_visitor
*/
Expand All @@ -14,11 +15,10 @@ var nodeTypes = Object.keys(require('../nodes'));
* method that can be called on a node with a context
*/
function createVisitor(handlers) {
nodeTypes.forEach(function(nodeType) {
if( typeof handlers[nodeType] === 'undefined') {
nodeTypes.forEach(function (nodeType) {
if (typeof handlers[nodeType] === 'undefined') {
throw new Error('No handler defined for ' + nodeType.displayName);
}

});

return {
Expand All @@ -28,8 +28,8 @@ function createVisitor(handlers) {
* @param {Object} context context to pass through to handlers
* @return {Object}
*/
visit: function(node, context) {
return this.handlers[node.displayName].call(this,node, context);
visit: function (node, context) {
return this.handlers[node.displayName].call(this, node, context);
},
handlers: handlers
};
Expand Down
18 changes: 9 additions & 9 deletions lib/route/visitors/reconstruct.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
'use strict';

var createVisitor = require('./create_visitor');
var createVisitor = require('./create_visitor');

/**
* Visitor for the AST to reconstruct the normalized input
* @class ReconstructVisitor
* @borrows Visitor-visit
*/
var ReconstructVisitor = createVisitor({
'Concat': function(node) {
Concat: function (node) {
return node.children
.map( function(child) {
.map(function (child) {
return this.visit(child);
}.bind(this))
.join('');
},

'Literal': function(node) {
Literal: function (node) {
return node.props.value;
},

'Splat': function(node) {
Splat: function (node) {
return '*' + node.props.name;
},

'Param': function(node) {
Param: function (node) {
return ':' + node.props.name;
},

'Optional': function(node) {
Optional: function (node) {
return '(' + this.visit(node.children[0]) + ')';
},

'Root': function(node) {
Root: function (node) {
return this.visit(node.children[0]);
}
});

module.exports = ReconstructVisitor;
module.exports = ReconstructVisitor;
Loading

0 comments on commit 0ce6f49

Please sign in to comment.