Permalink
Browse files

Merge pull request #3 from paularmstrong/master

JSLint!
  • Loading branch information...
2 parents f34158f + 36b90fc commit 933e99b0a2242ad0c636add4f76c15070d49e2d9 @skid committed Aug 6, 2011
Showing with 789 additions and 639 deletions.
  1. +3 −0 .gitignore
  2. +10 −0 Makefile
  3. +4 −4 README.md
  4. +103 −90 helpers.js
  5. +110 −96 index.js
  6. +17 −11 package.json
  7. +150 −145 parser.js
  8. +41 −0 scripts/config-lint.js
  9. +34 −0 scripts/runlint.js
  10. +222 −215 tags.js
  11. +24 −24 tests/tests.js
  12. +71 −54 widgets.js
View
@@ -0,0 +1,3 @@
+.DS_Store
+npm-debug.log
+node_modules/*
View
@@ -0,0 +1,10 @@
+all:
+ @echo ''
+
+test:
+ @node tests/tests.js
+
+lint:
+ @node scripts/runlint.js
+
+.PHONY: all test lint
View
@@ -23,7 +23,7 @@ Node-T is a templating engine inspired by the django syntax. It has a few extens
var template = require('node-t');
var tmpl = template.fromFile("/path/to/template.html");
- console.log( tmpl.render({names: ["Duke", "Django", "Louis"]}) );
+ console.log(tmpl.render({names: ["Duke", "Django", "Louis"]}));
### How it works
@@ -139,13 +139,13 @@ Node.js code
}
context.widgets = {
- analytics: function(context){
+ analytics: function (context) {
// this inside widget functions is bound to the widget object
return "<script>..." + this.uaCode + "...</script>";
},
- navigation: function(context){
+ navigation: function (context) {
var i, html = "";
- for( i=0; i<this.links; i++ )
+ for (i=0; i<this.links; i++)
html += "<a href='" + links[i] + "'>" + links[i] + "</a>";
return html;
}
View
@@ -14,119 +14,132 @@ var KEYWORDS = /^(Array|RegExpt|Object|String|Number|Math|Error|break|continue|d
var VALID_BLOCK_NAME = /^[A-Za-z]+[A-Za-z_0-9]*$/;
// Returns TRUE if the passed string is a valid javascript number or string literal
-function isLiteral( string ){
- var literal = false;
- // Check if it's a number literal
- if( NUMBER_LITERAL.test( string ) ){
- literal = true;
- }
-
- // Check if it's a valid string literal (throw exception otherwise)
- else if( (string[0] === string[string.length-1]) && (string[0] === "'" || string[0] === '"') ) {
- var teststr = string.substr( 1, string.length-2 ).split("").reverse().join("");
- if( string[0] === "'" && UNESCAPED_QUOTE.test( teststr ) || string[1] === '"' && UNESCAPED_DQUOTE.test( teststr ) ){
- throw new Error("Invalid string literal. Unescaped quote (" + string[0] + ") found.");
+function isLiteral(string) {
+ var literal = false,
+ teststr;
+
+ // Check if it's a number literal
+ if (NUMBER_LITERAL.test(string)) {
+ literal = true;
+ } else if ((string[0] === string[string.length - 1]) && (string[0] === "'" || string[0] === '"')) {
+ // Check if it's a valid string literal (throw exception otherwise)
+ teststr = string.substr(1, string.length - 2).split("").reverse().join("");
+
+ if (string[0] === "'" && UNESCAPED_QUOTE.test(teststr) || string[1] === '"' && UNESCAPED_DQUOTE.test(teststr)) {
+ throw new Error("Invalid string literal. Unescaped quote (" + string[0] + ") found.");
+ }
+
+ literal = true;
}
- literal = true;
- }
- return literal;
+ return literal;
}
// Returns TRUE if the passed string is a valid javascript string literal
-function isStringLiteral( string ){
- // Check if it's a valid string literal (throw exception otherwise)
- if( (string[0] === string[string.length-1]) && (string[0] === "'" || string[0] === '"') ) {
- var teststr = string.substr( 1, string.length-2 ).split("").reverse().join("");
- if( string[0] === "'" && UNESCAPED_QUOTE.test( teststr ) || string[1] === '"' && UNESCAPED_DQUOTE.test( teststr ) ){
- throw new Error("Invalid string literal. Unescaped quote (" + string[0] + ") found.");
+function isStringLiteral(string) {
+ // Check if it's a valid string literal (throw exception otherwise)
+ if ((string[0] === string[string.length - 1]) && (string[0] === "'" || string[0] === '"')) {
+ var teststr = string.substr(1, string.length - 2).split("").reverse().join("");
+
+ if (string[0] === "'" && UNESCAPED_QUOTE.test(teststr) || string[1] === '"' && UNESCAPED_DQUOTE.test(teststr)) {
+ throw new Error("Invalid string literal. Unescaped quote (" + string[0] + ") found.");
+ }
+
+ return true;
}
- return true;
- }
- return false;
+
+ return false;
}
// Variable names starting with __ are reserved.
-function isValidName( string ){
- return VALID_NAME.test(string) && !KEYWORDS.test(string) && string.substr(0,2) !== "__";
+function isValidName(string) {
+ return VALID_NAME.test(string) && !KEYWORDS.test(string) && string.substr(0, 2) !== "__";
}
// Variable names starting with __ are reserved.
-function isValidShortName( string ){
- return VALID_SHORT_NAME.test(string) && !KEYWORDS.test(string) && string.substr(0,2) !== "__";
+function isValidShortName(string) {
+ return VALID_SHORT_NAME.test(string) && !KEYWORDS.test(string) && string.substr(0, 2) !== "__";
}
// Checks if a name is a vlaid block name
-function isValidBlockName( string ){
- return VALID_BLOCK_NAME.test(string);
+function isValidBlockName(string) {
+ return VALID_BLOCK_NAME.test(string);
}
/**
- * Returns a valid javascript code that will
- * check if a variable (or property chain) exists
- * in the evaled context. For example:
- * check( "foo.bar.baz" )
- * will return the following string:
- * "typeof foo !== 'undefined' && typeof foo.bar !== 'undefined' && typeof foo.bar.baz !== 'undefined'"
- */
-exports.check = function( variable, context ){
- /* 'this' inside of the render function is bound to the tag closure which is meaningless, so we can't use it.
- * '__this' is bound to the original template whose render function we called.
- * Using 'this' in the HTML templates will result in '__this.__currentContext'. This is an additional context
- * for binding data to a specific template - e.g. binding widget data.
- */
- variable = variable.replace(/^this/, '__this.__currentContext');
-
- if( isLiteral( variable ) )
- return "(true)";
-
- var props = variable.split("."), chain = "", output = [];
-
- if( typeof context === 'string' && context.length )
- props.unshift( context );
-
- props.forEach(function(prop){
- chain += (chain ? (isNaN(prop) ? "." + prop : "[" + prop + "]") : prop);
- output.push( "typeof " + chain + " !== 'undefined'" )
- });
- return "(" + output.join(" && ") + ")";
-}
+* Returns a valid javascript code that will
+* check if a variable (or property chain) exists
+* in the evaled context. For example:
+* check("foo.bar.baz")
+* will return the following string:
+* "typeof foo !== 'undefined' && typeof foo.bar !== 'undefined' && typeof foo.bar.baz !== 'undefined'"
+*/
+exports.check = function (variable, context) {
+ /* 'this' inside of the render function is bound to the tag closure which is meaningless, so we can't use it.
+ * '__this' is bound to the original template whose render function we called.
+ * Using 'this' in the HTML templates will result in '__this.__currentContext'. This is an additional context
+ * for binding data to a specific template - e.g. binding widget data.
+ */
+ variable = variable.replace(/^this/, '__this.__currentContext');
+
+ if (isLiteral(variable)) {
+ return "(true)";
+ }
+
+ var props = variable.split("."), chain = "", output = [];
+
+ if (typeof context === 'string' && context.length) {
+ props.unshift(context);
+ }
+
+ props.forEach(function (prop) {
+ chain += (chain ? (isNaN(prop) ? "." + prop : "[" + prop + "]") : prop);
+ output.push("typeof " + chain + " !== 'undefined'");
+ });
+ return "(" + output.join(" && ") + ")";
+};
/**
- * Returns an escaped string (safe for evaling). If context is passed
- * then returns a concatenation of context and the escaped variable name.
- */
-exports.escape = function( variable, context ){
- /* 'this' inside of the render function is bound to the tag closure which is meaningless, so we can't use it.
- * '__this' is bound to the original template whose render function we called.
- * Using 'this' in the HTML templates will result in '__this.__currentContext'. This is an additional context
- * for binding data to a specific template - e.g. binding widget data.
- */
- variable = variable.replace(/^this/, '__this.__currentContext');
-
- if( isLiteral( variable ) )
- variable = "(" + variable + ")";
-
- else if( typeof context === 'string' && context.length )
- variable = context + '.' + variable;
-
- var chain = "", props = variable.split(".");
- props.forEach(function(prop){
- chain += (chain ? (isNaN(prop) ? "." + prop : "[" + prop + "]") : prop);
- });
-
- return chain.replace(/\n/g, '\\n').replace(/\r/g, '\\r');
-}
+* Returns an escaped string (safe for evaling). If context is passed
+* then returns a concatenation of context and the escaped variable name.
+*/
+exports.escape = function (variable, context) {
+ /* 'this' inside of the render function is bound to the tag closure which is meaningless, so we can't use it.
+ * '__this' is bound to the original template whose render function we called.
+ * Using 'this' in the HTML templates will result in '__this.__currentContext'. This is an additional context
+ * for binding data to a specific template - e.g. binding widget data.
+ */
+ variable = variable.replace(/^this/, '__this.__currentContext');
+
+ if (isLiteral(variable)) {
+ variable = "(" + variable + ")";
+ } else if (typeof context === 'string' && context.length) {
+ variable = context + '.' + variable;
+ }
+
+ var chain = "", props = variable.split(".");
+ props.forEach(function (prop) {
+ chain += (chain ? (isNaN(prop) ? "." + prop : "[" + prop + "]") : prop);
+ });
+
+ return chain.replace(/\n/g, '\\n').replace(/\r/g, '\\r');
+};
/**
- * Merges b into a and returns a
- */
-exports.merge = function(a, b){
- if (a && b)
- for (var key in b) {
- a[key] = b[key];
+* Merges b into a and returns a
+*/
+exports.merge = function (a, b) {
+ var key;
+
+ if (a && b) {
+ for (key in b) {
+ if (b.hasOwnProperty(key)) {
+ a[key] = b[key];
+ }
+ }
}
- return a;
+
+ return a;
};
exports.isLiteral = isLiteral;
Oops, something went wrong.

0 comments on commit 933e99b

Please sign in to comment.