Skip to content

Commit

Permalink
Merge pull request #47 from pugjs/plugins
Browse files Browse the repository at this point in the history
Add test to make sure all lexer functions are called with callLexerFunction
  • Loading branch information
ForbesLindesay committed Dec 21, 2015
2 parents f8d4b5e + dadfa5a commit 40d591d
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ Lexer.prototype = {
}
},

pipelessText: function(indents) {
pipelessText: function pipelessText(indents) {
while (this.callLexerFunction('blank'));

var captures = this.scanIndentation();
Expand Down Expand Up @@ -1207,7 +1207,7 @@ Lexer.prototype = {
// line is indented less than the first line but is still indented
// need to retry lexing the text block
this.tokens.pop();
return this.pipelessText(lineCaptures[1].length);
return pipelessText.call(this, lineCaptures[1].length);
}
} while((this.input.length - stringPtr) && isMatch);
this.consume(stringPtr);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"jade-error": "^1.0.0"
},
"devDependencies": {
"acorn": "^2.6.2",
"istanbul": "^0.3.2"
},
"scripts": {
Expand Down
96 changes: 96 additions & 0 deletions test/check-lexer-functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
var fs = require('fs');
var acorn = require('acorn');
var walk = require('acorn/dist/walk');

var hadErrors = false;

var lexerFunctions = {
advance: true,
append: true,
attributesBlock: true,
attrs: true,
blank: true,
block: true,
blockCode: true,
call: true,
case: true,
className: true,
code: true,
colon: true,
comment: true,
conditional: true,
default: true,
doctype: true,
dot: true,
each: true,
eos: true,
endInterpolation: true,
extends: true,
filter: true,
id: true,
include: true,
indent: true,
interpolation: true,
mixin: true,
mixinBlock: true,
path: true,
pipelessText: true,
prepend: true,
slash: true,
tag: true,
text: true,
textHtml: true,
when: true,
while: true,
yield: true,
};

module.exports = function () {
var str = fs.readFileSync(__dirname + '/../index.js', 'utf8');
var ast = acorn.parse(str, {locations: true});
walk.simple(ast, {
CallExpression: function (node) {
checkDirectCalls(node);
checkMissingLexerFunction(node);
}
});
if (hadErrors) process.exit(1);
};

function checkDirectCalls (node) {
var callee = node.callee;
if (callee.type !== 'MemberExpression') return;
if (callee.object.type !== 'ThisExpression') return;
var property = callee.property;
var func;
if (callee.computed) {
if (property.type !== 'Literal') return;
func = property.value;
} else {
func = property.name;
}
if (!lexerFunctions[func]) return;
console.log('index.js:' + node.loc.start.line + ':' + node.loc.start.column + ': Lexer function ' + func + ' called directly');
hadErrors = true;
}

function checkMissingLexerFunction (node) {
var callee = node.callee;
if (callee.type !== 'MemberExpression') return;
if (callee.object.type !== 'ThisExpression') return;
var property = callee.property;
var func;
if (callee.computed) {
if (property.type !== 'Literal') return;
func = property.value;
} else {
func = property.name;
}
if (func !== 'callLexerFunction') return;
if (!node.arguments.length) return;
if (node.arguments[0].type !== 'Literal') return;
func = node.arguments[0].value;
if (lexerFunctions[func]) return;
console.log('index.js:' + node.loc.start.line + ':' + node.loc.start.column + ': Lexer function ' + func + ' not in lexerFunctions list');
hadErrors = true;
}
3 changes: 3 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
var fs = require('fs');
var assert = require('assert');
var lex = require('../');
var checkLexerFunctions = require('./check-lexer-functions');

checkLexerFunctions();

var dir = __dirname + '/cases/';
fs.readdirSync(dir).forEach(function (testCase) {
Expand Down

0 comments on commit 40d591d

Please sign in to comment.