Comparing version 1.3.3 to 1.3.4
@@ -25,2 +25,4 @@ var esprima = require('esprima'), | ||
this.options = options || {}; | ||
this.code = null; | ||
this.idHash = {}; | ||
@@ -119,3 +121,6 @@ this.scores = {}; | ||
var ast = esprima.parse(code); | ||
this.code = code; | ||
var ast = esprima.parse(code, { | ||
loc: true | ||
}); | ||
assert.equal(ast.type, 'Program'); | ||
@@ -150,2 +155,32 @@ | ||
Compiler.prototype.getNodeLine = function getNodeLine(node) { | ||
var loc = node.loc.start; | ||
var lines = this.code.split(/\r\n|\r|\n/g); | ||
var line = lines[loc.line - 1]; | ||
var arrow = ''; | ||
for (var i = 0; i < loc.column; i++) | ||
arrow += ' '; | ||
arrow += '^'; | ||
return ' at ' + loc.line + ':' + loc.column + '\n' + line + '\n' + arrow; | ||
}; | ||
Compiler.prototype.assert = function assert(node, cond, text) { | ||
if (cond) | ||
return; | ||
if (!text) | ||
text = 'Assertion failed'; | ||
throw new Error(text + this.getNodeLine(node)); | ||
}; | ||
Compiler.prototype.assertEqual = function assertEqual(node, lhs, rhs, text) { | ||
if (lhs === rhs) | ||
return; | ||
if (!text) | ||
text = 'Expected ' + rhs + ', but got ' + lhs; | ||
throw new Error(text + this.getNodeLine(node)); | ||
}; | ||
// Run compiler in phases to translate AST to AST | ||
@@ -344,15 +379,20 @@ Compiler.prototype.translate = function translate(ast) { | ||
node.callee.type === 'Identifier') { | ||
if (node.callee.name !== '__$$fetch' && | ||
node.callee.name !== '__$$set') { | ||
var name = node.callee.name; | ||
if (name !== '__$$fetch' && name !== '__$$set') | ||
return; | ||
} | ||
assert(node.arguments.length >= 1); | ||
assert.equal(node.arguments[0].type, 'Literal'); | ||
if (name === '__$$fetch') | ||
self.assertEqual(node, node.arguments.length, 1, 'Invalid arg cnt'); | ||
else | ||
self.assertEqual(node, node.arguments.length, 2, 'Invalid arg cnt'); | ||
self.assertEqual(node.arguments[0], node.arguments[0].type, 'Literal'); | ||
self.assertEqual(node.arguments[0], | ||
typeof node.arguments[0].value, | ||
'string'); | ||
var id = self.fetchGlobal(node.arguments[0].value); | ||
if (node.callee.name === '__$$fetch') { | ||
if (name === '__$$fetch') { | ||
return id; | ||
} else { | ||
assert.equal(node.arguments.length, 2); | ||
return { | ||
@@ -594,6 +634,7 @@ type: 'AssignmentExpression', | ||
assert(body.type === 'FunctionExpression' || | ||
body.type === 'Literal' || | ||
body.type === 'Identifier' && body.name === 'undefined', | ||
'Only literal or function is allowed in template\'s body'); | ||
this.assert(body, | ||
body.type === 'FunctionExpression' || | ||
body.type === 'Literal' || | ||
body.type === 'Identifier' && body.name === 'undefined', | ||
'Only literal or function is allowed in template\'s body'); | ||
@@ -600,0 +641,0 @@ function isConst(val) { |
@@ -1,2 +0,1 @@ | ||
var assert = require('assert'); | ||
var util = require('util'); | ||
@@ -148,8 +147,8 @@ var estraverse = require('estraverse'); | ||
} else if (name === '__$$fetch') { | ||
assert.equal(node.arguments.length, 1); | ||
assert.equal(node.arguments[0].type, 'Literal'); | ||
this.compiler.assertEqual(node, node.arguments.length, 1); | ||
this.compiler.assertEqual(node, node.arguments[0].type, 'Literal'); | ||
return this.rollOutFetch(node.arguments[0].value); | ||
} else if (name === '__$$set') { | ||
assert.equal(node.arguments.length, 2); | ||
assert.equal(node.arguments[0].type, 'Literal'); | ||
this.compiler.assertEqual(node, node.arguments.length, 2); | ||
this.compiler.assertEqual(node, node.arguments[0].type, 'Literal'); | ||
return this.rollOutSet(node.arguments[0].value, | ||
@@ -254,5 +253,7 @@ node.arguments[1]); | ||
return; | ||
assert.equal(change.type, | ||
'ObjectExpression', | ||
'apply() and local() accepts only object literals'); | ||
this.compiler.assertEqual( | ||
change, | ||
change.type, | ||
'ObjectExpression', | ||
'apply() and local() accepts only object literals'); | ||
change.properties.forEach(function(property) { | ||
@@ -259,0 +260,0 @@ var keys = (property.key.name || property.key.value).split('.'); |
{ | ||
"name": "xjst", | ||
"description": "XSLT inspired JavaScript templates (with spices)", | ||
"version": "1.3.3", | ||
"version": "1.3.4", | ||
"homepage": "http://github.com/veged/xjst", | ||
@@ -24,5 +24,5 @@ "author": "Sergey Berezhnoy <veged@mail.ru> (http://github.com/veged)", | ||
"q": "~ 0.9.7", | ||
"esprima": "~1.0.4", | ||
"esprima": "~1.1.1", | ||
"uglify-js": "~2.4.0", | ||
"estraverse": "~1.3.1" | ||
"estraverse": "~1.5.0" | ||
}, | ||
@@ -29,0 +29,0 @@ "devDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
108752
2479
+ Addedesprima@1.1.1(transitive)
+ Addedestraverse@1.5.1(transitive)
- Removedesprima@1.0.4(transitive)
- Removedestraverse@1.3.2(transitive)
Updatedesprima@~1.1.1
Updatedestraverse@~1.5.0