Quellcode durchsuchen

Merge pull request #440 from phillipj/chore/eslint

eslinting
tags/v2.1.0
David da Silva Contín vor 11 Jahren
Ursprung
Commit
94224959ef
4 geänderte Dateien mit 92 neuen und 76 gelöschten Zeilen
  1. +19
    -0
      .eslintrc
  2. +0
    -6
      .jshintrc
  3. +71
    -68
      mustache.js
  4. +2
    -2
      package.json

+ 19
- 0
.eslintrc Datei anzeigen

@@ -0,0 +1,19 @@
{
"env": {
"browser": true,
"node": true
},
"rules": {
"func-names": 2,
"no-mixed-spaces-and-tabs": 2,
"quotes": [2, "single", "avoid-escape"],
"semi": 2,
"space-after-keywords": 2,
"space-before-function-paren": 2,

"curly": 0,
"consistent-return": 0,
"no-use-before-define": 0,
"strict": 0
}
}

+ 0
- 6
.jshintrc Datei anzeigen

@@ -1,6 +0,0 @@
{
"eqnull": true,
"evil": true,
"unused": true
}


+ 71
- 68
mustache.js Datei anzeigen

@@ -5,52 +5,52 @@


/*global define: false*/ /*global define: false*/


(function (global, factory) {
if (typeof exports === "object" && exports) {
(function defineMustache (global, factory) {
if (typeof exports === 'object' && exports) {
factory(exports); // CommonJS factory(exports); // CommonJS
} else if (typeof define === "function" && define.amd) {
} else if (typeof define === 'function' && define.amd) {
define(['exports'], factory); // AMD define(['exports'], factory); // AMD
} else { } else {
factory(global.Mustache = {}); // <script> factory(global.Mustache = {}); // <script>
} }
}(this, function (mustache) {
}(this, function mustacheFactory (mustache) {


var Object_toString = Object.prototype.toString;
var isArray = Array.isArray || function (object) {
return Object_toString.call(object) === '[object Array]';
var objectToString = Object.prototype.toString;
var isArray = Array.isArray || function isArrayPolyfill (object) {
return objectToString.call(object) === '[object Array]';
}; };


function isFunction(object) {
function isFunction (object) {
return typeof object === 'function'; return typeof object === 'function';
} }


function escapeRegExp(string) {
return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
function escapeRegExp (string) {
return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
} }


// Workaround for https://issues.apache.org/jira/browse/COUCHDB-577 // Workaround for https://issues.apache.org/jira/browse/COUCHDB-577
// See https://github.com/janl/mustache.js/issues/189 // See https://github.com/janl/mustache.js/issues/189
var RegExp_test = RegExp.prototype.test;
function testRegExp(re, string) {
return RegExp_test.call(re, string);
var regExpTest = RegExp.prototype.test;
function testRegExp (re, string) {
return regExpTest.call(re, string);
} }


var nonSpaceRe = /\S/; var nonSpaceRe = /\S/;
function isWhitespace(string) {
function isWhitespace (string) {
return !testRegExp(nonSpaceRe, string); return !testRegExp(nonSpaceRe, string);
} }


var entityMap = { var entityMap = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;', '"': '&quot;',
"'": '&#39;', "'": '&#39;',
"/": '&#x2F;'
'/': '&#x2F;'
}; };


function escapeHtml(string) {
return String(string).replace(/[&<>"'\/]/g, function (s) {
function escapeHtml (string) {
return String(string).replace(/[&<>"'\/]/g, function fromEntityMap (s) {
return entityMap[s]; return entityMap[s];
}); });
} }
@@ -83,7 +83,7 @@
* array of tokens in the subtree and 2) the index in the original template at * array of tokens in the subtree and 2) the index in the original template at
* which the closing tag for that section begins. * which the closing tag for that section begins.
*/ */
function parseTemplate(template, tags) {
function parseTemplate (template, tags) {
if (!template) if (!template)
return []; return [];


@@ -95,7 +95,7 @@


// Strips all whitespace tokens array for the current line // Strips all whitespace tokens array for the current line
// if there was a {{#tag}} on it and otherwise only space. // if there was a {{#tag}} on it and otherwise only space.
function stripSpace() {
function stripSpace () {
if (hasTag && !nonSpace) { if (hasTag && !nonSpace) {
while (spaces.length) while (spaces.length)
delete tokens[spaces.pop()]; delete tokens[spaces.pop()];
@@ -108,16 +108,16 @@
} }


var openingTagRe, closingTagRe, closingCurlyRe; var openingTagRe, closingTagRe, closingCurlyRe;
function compileTags(tags) {
if (typeof tags === 'string')
tags = tags.split(spaceRe, 2);
function compileTags (tagsToCompile) {
if (typeof tagsToCompile === 'string')
tagsToCompile = tagsToCompile.split(spaceRe, 2);


if (!isArray(tags) || tags.length !== 2)
throw new Error('Invalid tags: ' + tags);
if (!isArray(tagsToCompile) || tagsToCompile.length !== 2)
throw new Error('Invalid tags: ' + tagsToCompile);


openingTagRe = new RegExp(escapeRegExp(tags[0]) + '\\s*');
closingTagRe = new RegExp('\\s*' + escapeRegExp(tags[1]));
closingCurlyRe = new RegExp('\\s*' + escapeRegExp('}' + tags[1]));
openingTagRe = new RegExp(escapeRegExp(tagsToCompile[0]) + '\\s*');
closingTagRe = new RegExp('\\s*' + escapeRegExp(tagsToCompile[1]));
closingCurlyRe = new RegExp('\\s*' + escapeRegExp('}' + tagsToCompile[1]));
} }


compileTags(tags || mustache.tags); compileTags(tags || mustache.tags);
@@ -213,7 +213,7 @@
* Combines the values of consecutive text tokens in the given `tokens` array * Combines the values of consecutive text tokens in the given `tokens` array
* to a single token. * to a single token.
*/ */
function squashTokens(tokens) {
function squashTokens (tokens) {
var squashedTokens = []; var squashedTokens = [];


var token, lastToken; var token, lastToken;
@@ -240,7 +240,7 @@
* all tokens that appear in that section and 2) the index in the original * all tokens that appear in that section and 2) the index in the original
* template that represents the end of that section. * template that represents the end of that section.
*/ */
function nestTokens(tokens) {
function nestTokens (tokens) {
var nestedTokens = []; var nestedTokens = [];
var collector = nestedTokens; var collector = nestedTokens;
var sections = []; var sections = [];
@@ -273,7 +273,7 @@
* A simple string scanner that is used by the template parser to find * A simple string scanner that is used by the template parser to find
* tokens in template strings. * tokens in template strings.
*/ */
function Scanner(string) {
function Scanner (string) {
this.string = string; this.string = string;
this.tail = string; this.tail = string;
this.pos = 0; this.pos = 0;
@@ -282,15 +282,15 @@
/** /**
* Returns `true` if the tail is empty (end of string). * Returns `true` if the tail is empty (end of string).
*/ */
Scanner.prototype.eos = function () {
return this.tail === "";
Scanner.prototype.eos = function eos () {
return this.tail === '';
}; };


/** /**
* Tries to match the given regular expression at the current position. * Tries to match the given regular expression at the current position.
* Returns the matched text if it can match, the empty string otherwise. * Returns the matched text if it can match, the empty string otherwise.
*/ */
Scanner.prototype.scan = function (re) {
Scanner.prototype.scan = function scan (re) {
var match = this.tail.match(re); var match = this.tail.match(re);


if (!match || match.index !== 0) if (!match || match.index !== 0)
@@ -308,16 +308,16 @@
* Skips all text until the given regular expression can be matched. Returns * Skips all text until the given regular expression can be matched. Returns
* the skipped string, which is the entire tail if no match can be made. * the skipped string, which is the entire tail if no match can be made.
*/ */
Scanner.prototype.scanUntil = function (re) {
Scanner.prototype.scanUntil = function scanUntil (re) {
var index = this.tail.search(re), match; var index = this.tail.search(re), match;


switch (index) { switch (index) {
case -1: case -1:
match = this.tail; match = this.tail;
this.tail = "";
this.tail = '';
break; break;
case 0: case 0:
match = "";
match = '';
break; break;
default: default:
match = this.tail.substring(0, index); match = this.tail.substring(0, index);
@@ -333,7 +333,7 @@
* Represents a rendering context by wrapping a view object and * Represents a rendering context by wrapping a view object and
* maintaining a reference to the parent context. * maintaining a reference to the parent context.
*/ */
function Context(view, parentContext) {
function Context (view, parentContext) {
this.view = view; this.view = view;
this.cache = { '.': this.view }; this.cache = { '.': this.view };
this.parent = parentContext; this.parent = parentContext;
@@ -343,7 +343,7 @@
* Creates a new context using the given view with this context * Creates a new context using the given view with this context
* as the parent. * as the parent.
*/ */
Context.prototype.push = function (view) {
Context.prototype.push = function push (view) {
return new Context(view, this); return new Context(view, this);
}; };


@@ -351,7 +351,7 @@
* Returns the value of the given name in this context, traversing * Returns the value of the given name in this context, traversing
* up the context hierarchy if the value is absent in this context's view. * up the context hierarchy if the value is absent in this context's view.
*/ */
Context.prototype.lookup = function (name) {
Context.prototype.lookup = function lookup (name) {
var cache = this.cache; var cache = this.cache;


var value; var value;
@@ -408,14 +408,14 @@
* string, given a context. It also maintains a cache of templates to * string, given a context. It also maintains a cache of templates to
* avoid the need to parse the same template twice. * avoid the need to parse the same template twice.
*/ */
function Writer() {
function Writer () {
this.cache = {}; this.cache = {};
} }


/** /**
* Clears all cached templates in this writer. * Clears all cached templates in this writer.
*/ */
Writer.prototype.clearCache = function () {
Writer.prototype.clearCache = function clearCache () {
this.cache = {}; this.cache = {};
}; };


@@ -423,7 +423,7 @@
* Parses and caches the given `template` and returns the array of tokens * Parses and caches the given `template` and returns the array of tokens
* that is generated from the parse. * that is generated from the parse.
*/ */
Writer.prototype.parse = function (template, tags) {
Writer.prototype.parse = function parse (template, tags) {
var cache = this.cache; var cache = this.cache;
var tokens = cache[template]; var tokens = cache[template];


@@ -442,7 +442,7 @@
* also be a function that is used to load partial templates on the fly * also be a function that is used to load partial templates on the fly
* that takes a single argument: the name of the partial. * that takes a single argument: the name of the partial.
*/ */
Writer.prototype.render = function (template, view, partials) {
Writer.prototype.render = function render (template, view, partials) {
var tokens = this.parse(template); var tokens = this.parse(template);
var context = (view instanceof Context) ? view : new Context(view); var context = (view instanceof Context) ? view : new Context(view);
return this.renderTokens(tokens, context, partials, template); return this.renderTokens(tokens, context, partials, template);
@@ -457,7 +457,7 @@
* If the template doesn't use higher-order sections, this argument may * If the template doesn't use higher-order sections, this argument may
* be omitted. * be omitted.
*/ */
Writer.prototype.renderTokens = function (tokens, context, partials, originalTemplate) {
Writer.prototype.renderTokens = function renderTokens (tokens, context, partials, originalTemplate) {
var buffer = ''; var buffer = '';


var token, symbol, value; var token, symbol, value;
@@ -466,12 +466,12 @@
token = tokens[i]; token = tokens[i];
symbol = token[0]; symbol = token[0];


if (symbol === '#') value = this._renderSection(token, context, partials, originalTemplate);
else if (symbol === '^') value = this._renderInverted(token, context, partials, originalTemplate);
else if (symbol === '>') value = this._renderPartial(token, context, partials, originalTemplate);
else if (symbol === '&') value = this._unescapedValue(token, context);
else if (symbol === 'name') value = this._escapedValue(token, context);
else if (symbol === 'text') value = this._rawValue(token);
if (symbol === '#') value = this.renderSection(token, context, partials, originalTemplate);
else if (symbol === '^') value = this.renderInverted(token, context, partials, originalTemplate);
else if (symbol === '>') value = this.renderPartial(token, context, partials, originalTemplate);
else if (symbol === '&') value = this.unescapedValue(token, context);
else if (symbol === 'name') value = this.escapedValue(token, context);
else if (symbol === 'text') value = this.rawValue(token);


if (value !== undefined) if (value !== undefined)
buffer += value; buffer += value;
@@ -480,14 +480,14 @@
return buffer; return buffer;
}; };


Writer.prototype._renderSection = function (token, context, partials, originalTemplate) {
Writer.prototype.renderSection = function renderSection (token, context, partials, originalTemplate) {
var self = this; var self = this;
var buffer = ''; var buffer = '';
var value = context.lookup(token[1]); var value = context.lookup(token[1]);


// This function is used to render an arbitrary template // This function is used to render an arbitrary template
// in the current context by higher-order sections. // in the current context by higher-order sections.
function subRender(template) {
function subRender (template) {
return self.render(template, context, partials); return self.render(template, context, partials);
} }


@@ -514,7 +514,7 @@
return buffer; return buffer;
}; };


Writer.prototype._renderInverted = function(token, context, partials, originalTemplate) {
Writer.prototype.renderInverted = function renderInverted (token, context, partials, originalTemplate) {
var value = context.lookup(token[1]); var value = context.lookup(token[1]);


// Use JavaScript's definition of falsy. Include empty arrays. // Use JavaScript's definition of falsy. Include empty arrays.
@@ -523,7 +523,7 @@
return this.renderTokens(token[4], context, partials, originalTemplate); return this.renderTokens(token[4], context, partials, originalTemplate);
}; };


Writer.prototype._renderPartial = function(token, context, partials) {
Writer.prototype.renderPartial = function renderPartial (token, context, partials) {
if (!partials) return; if (!partials) return;


var value = isFunction(partials) ? partials(token[1]) : partials[token[1]]; var value = isFunction(partials) ? partials(token[1]) : partials[token[1]];
@@ -531,25 +531,25 @@
return this.renderTokens(this.parse(value), context, partials, value); return this.renderTokens(this.parse(value), context, partials, value);
}; };


Writer.prototype._unescapedValue = function(token, context) {
Writer.prototype.unescapedValue = function unescapedValue (token, context) {
var value = context.lookup(token[1]); var value = context.lookup(token[1]);
if (value != null) if (value != null)
return value; return value;
}; };


Writer.prototype._escapedValue = function(token, context) {
Writer.prototype.escapedValue = function escapedValue (token, context) {
var value = context.lookup(token[1]); var value = context.lookup(token[1]);
if (value != null) if (value != null)
return mustache.escape(value); return mustache.escape(value);
}; };


Writer.prototype._rawValue = function(token) {
Writer.prototype.rawValue = function rawValue (token) {
return token[1]; return token[1];
}; };


mustache.name = "mustache.js";
mustache.version = "2.0.0";
mustache.tags = [ "{{", "}}" ];
mustache.name = 'mustache.js';
mustache.version = '2.0.0';
mustache.tags = [ '{{', '}}' ];


// All high-level mustache.* functions use this writer. // All high-level mustache.* functions use this writer.
var defaultWriter = new Writer(); var defaultWriter = new Writer();
@@ -557,7 +557,7 @@
/** /**
* Clears all cached templates in the default writer. * Clears all cached templates in the default writer.
*/ */
mustache.clearCache = function () {
mustache.clearCache = function clearCache () {
return defaultWriter.clearCache(); return defaultWriter.clearCache();
}; };


@@ -566,7 +566,7 @@
* array of tokens it contains. Doing this ahead of time avoids the need to * array of tokens it contains. Doing this ahead of time avoids the need to
* parse templates on the fly as they are rendered. * parse templates on the fly as they are rendered.
*/ */
mustache.parse = function (template, tags) {
mustache.parse = function parse (template, tags) {
return defaultWriter.parse(template, tags); return defaultWriter.parse(template, tags);
}; };


@@ -574,12 +574,15 @@
* Renders the `template` with the given `view` and `partials` using the * Renders the `template` with the given `view` and `partials` using the
* default writer. * default writer.
*/ */
mustache.render = function (template, view, partials) {
mustache.render = function render (template, view, partials) {
return defaultWriter.render(template, view, partials); return defaultWriter.render(template, view, partials);
}; };


// This is here for backwards compatibility with 0.4.x.
mustache.to_html = function (template, view, partials, send) {
// This is here for backwards compatibility with 0.4.x.,
/*eslint-disable */ // eslint wants camel cased function name
mustache.to_html = function to_html (template, view, partials, send) {
/*eslint-enable*/

var result = mustache.render(template, view, partials); var result = mustache.render(template, view, partials);


if (isFunction(send)) { if (isFunction(send)) {


+ 2
- 2
package.json Datei anzeigen

@@ -31,12 +31,12 @@
"npm": ">=1.4.0" "npm": ">=1.4.0"
}, },
"scripts": { "scripts": {
"pretest": "jshint mustache.js",
"pretest": "eslint mustache.js",
"test": "mocha --reporter spec", "test": "mocha --reporter spec",
"test-render": "mocha --reporter spec test/render-test" "test-render": "mocha --reporter spec test/render-test"
}, },
"devDependencies": { "devDependencies": {
"jshint": "~2.4.4",
"eslint": "^0.20.0",
"mocha": "~2.1.0" "mocha": "~2.1.0"
}, },
"spm": { "spm": {


Laden…
Abbrechen
Speichern