Comparing version 2.5.0 to 2.5.1
@@ -889,2 +889,6 @@ var assert = require('assert'); | ||
for (var key in presetConfig) { | ||
assert(typeof presetConfig[key] !== 'function', 'Preset should be an JSON object'); | ||
} | ||
this._presets[presetName] = presetConfig; | ||
@@ -947,3 +951,3 @@ }; | ||
this.registerRule(require('../rules/disallow-identical-destructuring-names')); | ||
this.registerRule(require('../rules/require-spaces-around-generators')); | ||
this.registerRule(require('../rules/require-spaces-in-generator')); | ||
@@ -950,0 +954,0 @@ /* ES6 only (end) */ |
@@ -40,5 +40,7 @@ var assert = require('assert'); | ||
var hasErrors = false; | ||
try { | ||
this._tree = parseJavaScriptSource(this._source, params.esprima, params.esprimaOptions); | ||
} catch (e) { | ||
hasErrors = true; | ||
this._parseErrors.push(e); | ||
@@ -51,4 +53,4 @@ } | ||
this._tokens = this._buildTokenList(this._tree.tokens, this._tree.comments); | ||
this._addEOFToken(); | ||
this._applyWhitespaceData(this._tokens, this._source); | ||
this._addEOFToken(hasErrors); | ||
this._tokens = this._addWhitespaceTokens(this._tokens, this._source); | ||
@@ -139,2 +141,50 @@ this._setTokenIndexes(); | ||
/** | ||
* Sets whitespace before specified token. | ||
* | ||
* @param {Object} token | ||
* @param {String} whitespace | ||
*/ | ||
setWhitespaceBefore: function(token, whitespace) { | ||
var whitespaceToken = this.getPrevToken(token, {includeWhitespace: true}); | ||
if (whitespaceToken && whitespaceToken.type === 'Whitespace') { | ||
// Modifying already existing token. | ||
if (whitespace === '') { | ||
this.removeToken(whitespaceToken); | ||
} else { | ||
whitespaceToken.value = whitespace; | ||
} | ||
} else if (whitespace !== '') { | ||
var tokenIndex = token._tokenIndex; | ||
// Adding a token before specified one. | ||
this._tokens.splice(tokenIndex, 0, { | ||
type: 'Whitespace', | ||
value: whitespace, | ||
isWhitespace: true | ||
}); | ||
// Quickly updating modified token order | ||
for (var i = tokenIndex; i < this._tokens.length; i++) { | ||
this._tokens[i]._tokenIndex = i; | ||
} | ||
} | ||
}, | ||
/** | ||
* Returns whitespace before specified token. | ||
* | ||
* @param {Object} token | ||
* @returns {String} | ||
*/ | ||
getWhitespaceBefore: function(token) { | ||
var whitespaceToken = this.getPrevToken(token, {includeWhitespace: true}); | ||
if (whitespaceToken && whitespaceToken.type === 'Whitespace') { | ||
return whitespaceToken.value; | ||
} else { | ||
return ''; | ||
} | ||
}, | ||
/** | ||
* Remove some entity (only one) from array with predicate | ||
@@ -156,3 +206,3 @@ * | ||
/** | ||
* Remove token from token list | ||
* Remove token from token list. | ||
* | ||
@@ -250,5 +300,11 @@ * @param {Object} token | ||
token._tokenIndex = i; | ||
if (token.type === 'Whitespace') { | ||
continue; | ||
} | ||
// tokens by range | ||
tokenRangeStartIndex[token.range[0]] = i; | ||
tokenRangeEndIndex[token.range[1]] = i; | ||
tokenRangeStartIndex[token.range[0]] = token; | ||
tokenRangeEndIndex[token.range[1]] = token; | ||
@@ -262,4 +318,2 @@ // tokens by line | ||
tokensByLineIndex[lineNumber].push(token); | ||
token._tokenIndex = i; | ||
} | ||
@@ -280,4 +334,3 @@ | ||
getTokenByRangeStart: function(start) { | ||
var tokenIndex = this._tokenRangeStartIndex[start]; | ||
return tokenIndex === undefined ? null : this._tokens[tokenIndex]; | ||
return this._tokenRangeStartIndex[start] || null; | ||
}, | ||
@@ -291,4 +344,3 @@ | ||
getTokenByRangeEnd: function(end) { | ||
var tokenIndex = this._tokenRangeEndIndex[end]; | ||
return tokenIndex === undefined ? null : this._tokens[tokenIndex]; | ||
return this._tokenRangeEndIndex[end] || null; | ||
}, | ||
@@ -319,6 +371,9 @@ | ||
* | ||
* @param {Option} [options] | ||
* @param {Boolean} [options.includeComments=false] | ||
* @param {Boolean} [options.includeWhitespace=false] | ||
* @returns {Object} | ||
*/ | ||
getFirstToken: function() { | ||
return this._tokens[0]; | ||
getFirstToken: function(options) { | ||
return this._getTokenFromIndex(0, 1, options); | ||
}, | ||
@@ -329,40 +384,54 @@ | ||
* | ||
* @param {Option} [options] | ||
* @param {Boolean} [options.includeComments=false] | ||
* @param {Boolean} [options.includeWhitespace=false] | ||
* @returns {Object} | ||
*/ | ||
getLastToken: function() { | ||
return this._tokens[this._tokens.length - 1]; | ||
getLastToken: function(options) { | ||
return this._getTokenFromIndex(this._tokens.length - 1, -1, options); | ||
}, | ||
/** | ||
* Returns the first token before the given. | ||
* Returns the first token after the given using direction and specified conditions. | ||
* | ||
* @param {Object} token | ||
* @param {Number} index | ||
* @param {Number} direction `1` - forward or `-1` - backwards | ||
* @param {Object} [options] | ||
* @param {Boolean} [options.includeComments=false] | ||
* @param {Boolean} [options.includeWhitespace=false] | ||
* @returns {Object|null} | ||
*/ | ||
getPrevToken: function(token, options) { | ||
var index = token._tokenIndex - 1; | ||
if (index < 0) { | ||
return null; | ||
} | ||
_getTokenFromIndex: function(index, direction, options) { | ||
while (true) { | ||
var followingToken = this._tokens[index]; | ||
if (options && options.includeComments) { | ||
return this._tokens[index] || null; | ||
} | ||
do { | ||
if (!this._tokens[index]) { | ||
if (!followingToken) { | ||
return null; | ||
} | ||
if (!this._tokens[index].isComment) { | ||
return this._tokens[index]; | ||
if ( | ||
(!followingToken.isComment || (options && options.includeComments)) && | ||
(!followingToken.isWhitespace || (options && options.includeWhitespace)) | ||
) { | ||
return followingToken; | ||
} | ||
} while (--index >= 0); | ||
return null; | ||
index += direction; | ||
} | ||
}, | ||
/** | ||
* Returns the first token before the given. | ||
* | ||
* @param {Object} token | ||
* @param {Object} [options] | ||
* @param {Boolean} [options.includeComments=false] | ||
* @param {Boolean} [options.includeWhitespace=false] | ||
* @returns {Object|null} | ||
*/ | ||
getPrevToken: function(token, options) { | ||
return this._getTokenFromIndex(token._tokenIndex - 1, -1, options); | ||
}, | ||
/** | ||
* Returns the first token after the given. | ||
@@ -373,26 +442,7 @@ * | ||
* @param {Boolean} [options.includeComments=false] | ||
* @param {Boolean} [options.includeWhitespace=false] | ||
* @returns {Object|null} | ||
*/ | ||
getNextToken: function(token, options) { | ||
var index = token._tokenIndex + 1; | ||
if (index >= this._tokens.length) { | ||
return null; | ||
} | ||
if (options && options.includeComments) { | ||
return this._tokens[index] || null; | ||
} | ||
do { | ||
if (!this._tokens[index]) { | ||
return null; | ||
} | ||
if (!this._tokens[index].isComment) { | ||
return this._tokens[index]; | ||
} | ||
} while (++index < this._tokens.length); | ||
return null; | ||
return this._getTokenFromIndex(token._tokenIndex + 1, 1, options); | ||
}, | ||
@@ -573,3 +623,3 @@ | ||
this.getTokens().forEach(function(token, index, tokens) { | ||
this._forEachToken(function(token, index, tokens) { | ||
if (typeIndex[token.type]) { | ||
@@ -595,3 +645,3 @@ cb(token, index, tokens); | ||
this.getTokens().forEach(function(token, index, tokens) { | ||
this._forEachToken(function(token, index, tokens) { | ||
if (nameIndex.hasOwnProperty(token.value)) { | ||
@@ -604,2 +654,19 @@ cb(token, index, tokens); | ||
/** | ||
* Executes callback for each token in token list. | ||
* | ||
* @param {Function} cb | ||
* @private | ||
*/ | ||
_forEachToken: function(cb) { | ||
var index = 0; | ||
var tokens = this._tokens; | ||
while (index < tokens.length) { | ||
var token = tokens[index]; | ||
cb(token, index, tokens); | ||
index = token._tokenIndex; | ||
index++; | ||
} | ||
}, | ||
/** | ||
* Iterates tokens by type and value(s) from the token array. | ||
@@ -619,3 +686,3 @@ * Calls passed function for every matched token. | ||
this.getTokens().forEach(function(token, index, tokens) { | ||
this._forEachToken(function(token, index, tokens) { | ||
if (token.type === type && valueIndex[token.value]) { | ||
@@ -774,2 +841,3 @@ cb(token, index, tokens); | ||
ecmaVersion: 6, | ||
ignoreEval: true, | ||
sourceType: 'module' | ||
@@ -824,4 +892,2 @@ }); | ||
result += token.whitespaceBefore; | ||
switch (token.type) { | ||
@@ -883,11 +949,13 @@ // Line-comment: // ... | ||
*/ | ||
_addEOFToken: function() { | ||
var loc = { | ||
line: this._lines.length, | ||
column: this._lines[this._lines.length - 1].length | ||
}; | ||
_addEOFToken: function(hasErrors) { | ||
var loc = hasErrors ? | ||
{line: 0, column: 0} : | ||
{ | ||
line: this._lines.length, | ||
column: this._lines[this._lines.length - 1].length | ||
}; | ||
this._tokens.push({ | ||
type: 'EOF', | ||
value: '', | ||
range: [this._source.length, this._source.length + 1], | ||
range: hasErrors ? [0, 0] : [this._source.length, this._source.length + 1], | ||
loc: {start: loc, end: loc} | ||
@@ -904,4 +972,5 @@ }); | ||
*/ | ||
_applyWhitespaceData: function(tokens, source) { | ||
_addWhitespaceTokens: function(tokens, source) { | ||
var prevPos = 0; | ||
var result = []; | ||
@@ -912,12 +981,17 @@ // For-loop for maximal speed. | ||
var rangeStart = token.range[0]; | ||
var whitespace; | ||
if (rangeStart === prevPos) { | ||
whitespace = ''; | ||
} else { | ||
whitespace = source.substring(prevPos, rangeStart); | ||
if (rangeStart !== prevPos) { | ||
var whitespace = source.substring(prevPos, rangeStart); | ||
result.push({ | ||
type: 'Whitespace', | ||
value: whitespace, | ||
isWhitespace: true | ||
}); | ||
} | ||
token.whitespaceBefore = whitespace; | ||
result.push(token); | ||
prevPos = token.range[1]; | ||
} | ||
return result; | ||
}, | ||
@@ -924,0 +998,0 @@ |
@@ -49,5 +49,9 @@ /** | ||
// Iterate over all tokens (including comments) | ||
file.getTokens().forEach(function(token, index, tokens) { | ||
file.getTokens().forEach(function(token) { | ||
if (token.type === 'Whitespace') { | ||
return; | ||
} | ||
// If there are no trailing tokens, exit early | ||
var nextToken = tokens[index + 1]; | ||
var nextToken = file.getNextToken(token, {includeComments: true}); | ||
if (!nextToken) { | ||
@@ -54,0 +58,0 @@ return; |
@@ -61,5 +61,9 @@ /** | ||
var _this = this; | ||
file.getTokens().forEach(function(token, index, tokens) { | ||
file.getTokens().forEach(function(token) { | ||
if (token.type === 'Whitespace') { | ||
return; | ||
} | ||
// If there are no trailing tokens, exit early | ||
var nextToken = tokens[index + 1]; | ||
var nextToken = file.getNextToken(token, {includeComments: true}); | ||
if (!nextToken) { | ||
@@ -66,0 +70,0 @@ return; |
@@ -144,3 +144,6 @@ /** | ||
file.removeEntity(parent.params, node); | ||
file.removeToken(file.findPrevToken(token, 'Punctuator', ',')); | ||
var comma = file.findPrevToken(token, 'Punctuator', ','); | ||
file.setWhitespaceBefore(comma, ''); | ||
file.removeToken(comma); | ||
file.setWhitespaceBefore(token, ''); | ||
file.removeToken(token); | ||
@@ -155,4 +158,4 @@ | ||
file.removeToken(file.findNextToken(token, 'Punctuator', ',')); | ||
file.setWhitespaceBefore(next, ''); | ||
file.removeToken(token); | ||
next.whitespaceBefore = ''; | ||
@@ -159,0 +162,0 @@ return; |
@@ -52,3 +52,3 @@ /** | ||
check: function(file, errors) { | ||
var firstToken = file.getFirstToken(); | ||
var firstToken = file.getFirstToken({includeComments: true}); | ||
var lines = this._allowComments ? | ||
@@ -55,0 +55,0 @@ file.getLines() : file.getLinesWithCommentsRemoved(); |
@@ -14,3 +14,3 @@ /** | ||
* ```js | ||
* "disallowSpaceBeforeComma": true | ||
* "requireAlignedMultilineParams": true | ||
* ``` | ||
@@ -17,0 +17,0 @@ * |
@@ -21,7 +21,7 @@ /** | ||
* "requireBlocksOnNewline": { | ||
* includeComments: true | ||
* "includeComments": true | ||
* } | ||
* "requireBlocksOnNewline": { | ||
* includeComments: true, | ||
* minLines: 1 | ||
* "includeComments": true, | ||
* "minLines": 1 | ||
* } | ||
@@ -28,0 +28,0 @@ * ``` |
@@ -195,4 +195,4 @@ /** | ||
_isTextBlock: function(comment, comments, index) { | ||
var prevComment = comments[index - 1]; | ||
_isTextBlock: function(comment, file) { | ||
var prevComment = file.getPrevToken(comment, {includeComments: true}); | ||
@@ -231,3 +231,3 @@ if (prevComment) { | ||
file.iterateTokensByType('Line', function(comment, index, comments) { | ||
file.iterateTokensByType('Line', function(comment) { | ||
if (_this._isException(comment)) { | ||
@@ -245,3 +245,3 @@ return; | ||
if (_this._isTextBlock(comment, comments, index)) { | ||
if (_this._isTextBlock(comment, file)) { | ||
return; | ||
@@ -257,3 +257,3 @@ } | ||
file.iterateTokensByType('Block', function(comment, index, comments) { | ||
file.iterateTokensByType('Block', function(comment) { | ||
if (_this._isException(comment)) { | ||
@@ -275,3 +275,3 @@ return; | ||
if (_this._isValid(comment, index, comments)) { | ||
if (_this._isValid(comment)) { | ||
return; | ||
@@ -278,0 +278,0 @@ } |
@@ -33,3 +33,3 @@ /** | ||
check: function(file, errors) { | ||
var lastToken = file.getLastToken(); | ||
var lastToken = file.getLastToken({includeComments: true}); | ||
var prevToken = file.getPrevToken(lastToken, {includeComments: true}); | ||
@@ -36,0 +36,0 @@ errors.assert.differentLine({ |
@@ -134,3 +134,3 @@ /** | ||
// Check if this block is used in call or array expression | ||
if (exceptions[grandpa.type]) { | ||
if (grandpa && exceptions[grandpa.type]) { | ||
return true; | ||
@@ -137,0 +137,0 @@ } |
@@ -122,4 +122,4 @@ /** | ||
token.whitespaceBefore = ';' + token.whitespaceBefore; | ||
file.setWhitespaceBefore(token, ';' + file.getWhitespaceBefore(token)); | ||
} | ||
}; |
@@ -121,3 +121,3 @@ /** | ||
function getOffsetForBlockStatement(enclosingScope, varDecl, commentTokens) { | ||
function getOffsetForBlockStatement(enclosingScope, varDecl, commentTokens, file) { | ||
var offset = 0; | ||
@@ -127,3 +127,3 @@ var parentNode = varDecl.parentNode; | ||
offset += 1; | ||
offset += getCommentOffsetBetweenNodes(parentNode, varDecl, commentTokens); | ||
offset += getCommentOffsetBetweenNodes(parentNode, varDecl, commentTokens, file); | ||
} | ||
@@ -151,3 +151,3 @@ return offset; | ||
function isFirstVarDeclInScope(enclosingScope, varDecl, whitespaceOffsetBeforeVarDecl, commentTokens) { | ||
function isFirstVarDeclInScope(enclosingScope, varDecl, whitespaceOffsetBeforeVarDecl, commentTokens, file) { | ||
var adjustedVarDeclStart = varDecl.range[0]; | ||
@@ -165,3 +165,3 @@ var adjustedScopeStart = enclosingScope.range[0]; | ||
adjustedVarDeclStart -= getOffsetForBlockStatement(enclosingScope, varDecl, commentTokens); | ||
adjustedVarDeclStart -= getOffsetForBlockStatement(enclosingScope, varDecl, commentTokens, file); | ||
@@ -175,3 +175,3 @@ if (adjustedVarDeclStart === adjustedScopeStart) { | ||
function getCommentOffsetBetweenNodes(previousNode, currentNode, commentTokens) { | ||
function getCommentOffsetBetweenNodes(previousNode, currentNode, commentTokens, file) { | ||
var count; | ||
@@ -199,3 +199,3 @@ var comment; | ||
commentLength += comment.range[1] - comment.range[0] + comment.whitespaceBefore.length; | ||
commentLength += comment.range[1] - comment.range[0] + file.getWhitespaceBefore(comment).length; | ||
} | ||
@@ -206,3 +206,3 @@ | ||
function isPreviousNodeAVarDecl(previousNode, varDecl, whitespaceOffsetBeforeVarDecl, commentTokens) { | ||
function isPreviousNodeAVarDecl(previousNode, varDecl, whitespaceOffsetBeforeVarDecl, commentTokens, file) { | ||
var offsetForComments; | ||
@@ -213,3 +213,3 @@ if (varDecl.range[0] === previousNode.range[1]) { | ||
offsetForComments = getCommentOffsetBetweenNodes(previousNode, varDecl, commentTokens); | ||
offsetForComments = getCommentOffsetBetweenNodes(previousNode, varDecl, commentTokens, file); | ||
if (varDecl.range[0] - whitespaceOffsetBeforeVarDecl - offsetForComments === previousNode.range[1]) { | ||
@@ -256,3 +256,3 @@ return true; | ||
var whitespaceOffsetBeforeVarDecl = file.getFirstNodeToken(varDecl).whitespaceBefore.length; | ||
var whitespaceOffsetBeforeVarDecl = file.getWhitespaceBefore(file.getFirstNodeToken(varDecl)).length; | ||
@@ -277,3 +277,4 @@ enclosingScope = getVariableScope(varDecl.parentNode); | ||
isVarDeclFirst = isFirstVarDeclInScope( | ||
enclosingScope, varDecl, whitespaceOffsetBeforeVarDecl, commentTokens, file); | ||
enclosingScope, varDecl, whitespaceOffsetBeforeVarDecl, commentTokens, file | ||
); | ||
} else { | ||
@@ -283,3 +284,4 @@ previousNode = scopeContents.varDecl[scopeContents.varDecl.length - 1]; | ||
isVarDeclFirst = isPreviousNodeAVarDecl( | ||
previousNode, varDecl, whitespaceOffsetBeforeVarDecl, commentTokens); | ||
previousNode, varDecl, whitespaceOffsetBeforeVarDecl, commentTokens, file | ||
); | ||
} | ||
@@ -286,0 +288,0 @@ } |
@@ -102,3 +102,3 @@ var esprima = require('esprima'); | ||
// Do not check empty strings | ||
if (file.getFirstToken().type === 'EOF') { | ||
if (file.getFirstToken({includeComments: true}).type === 'EOF') { | ||
return errors; | ||
@@ -105,0 +105,0 @@ } |
@@ -83,3 +83,3 @@ var utils = require('util'); | ||
if (fixed) { | ||
nextToken.whitespaceBefore = new Array(spaceCount + 1).join(' '); | ||
this._file.setWhitespaceBefore(nextToken, new Array(spaceCount + 1).join(' ')); | ||
} | ||
@@ -181,6 +181,6 @@ | ||
var lineBreak = this._file.getLineBreakStyle(); | ||
var lines = token.whitespaceBefore.split(/\r\n|\r|\n/); | ||
var lines = this._file.getWhitespaceBefore(token).split(/\r\n|\r|\n/); | ||
lines = callback(lines); | ||
token.whitespaceBefore = lines.join(lineBreak); | ||
this._file.setWhitespaceBefore(token, lines.join(lineBreak)); | ||
}; | ||
@@ -396,6 +396,6 @@ | ||
}); | ||
nextToken.whitespaceBefore = token.whitespaceBefore; | ||
this._file.setWhitespaceBefore(nextToken, this._file.getWhitespaceBefore(token)); | ||
} | ||
token.whitespaceBefore = ' '; | ||
this._file.setWhitespaceBefore(token, ' '); | ||
return; | ||
@@ -456,3 +456,3 @@ } | ||
if (fixed) { | ||
token.whitespaceBefore = expectedTokenBefore.value + token.whitespaceBefore; | ||
this._file.setWhitespaceBefore(token, expectedTokenBefore.value + this._file.getWhitespaceBefore(token)); | ||
} | ||
@@ -541,3 +541,3 @@ | ||
if (precendingToken === null) { | ||
targetToken = this._file.getFirstToken(); | ||
targetToken = this._file.getFirstToken({includeComments: true}); | ||
startLineNumber = 1; | ||
@@ -588,3 +588,3 @@ } else { | ||
targetToken.whitespaceBefore = Array(eolCount).join('\n') + targetIndent; | ||
this._file.setWhitespaceBefore(targetToken, new Array(eolCount).join('\n') + targetIndent); | ||
fixed = true; | ||
@@ -591,0 +591,0 @@ } |
@@ -8,3 +8,3 @@ { | ||
"name": "jscs", | ||
"version": "2.5.0", | ||
"version": "2.5.1", | ||
"main": "lib/checker", | ||
@@ -11,0 +11,0 @@ "homepage": "http://jscs.info", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
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
5118907
22013