Comparing version 0.0.4 to 0.0.5
@@ -13,2 +13,4 @@ 'use strict'; | ||
var _utilsLines = require('../utils/lines'); | ||
var _ElementAssert = require('./ElementAssert'); | ||
@@ -747,2 +749,46 @@ | ||
}, { | ||
key: 'loc', | ||
/** | ||
* Calculates and returns Element loc. | ||
* | ||
* @returns {Object} | ||
*/ | ||
get: function get() { | ||
var prevToken = this.previousToken; | ||
var startColumn = 0; | ||
var startLine = 1; | ||
while (prevToken) { | ||
var lines = prevToken.sourceCodeLines; | ||
startColumn += lines[lines.length - 1].length; | ||
if (lines.length > 1) { | ||
while (prevToken) { | ||
startLine += prevToken.newlineCount; | ||
prevToken = prevToken.previousToken; | ||
} | ||
break; | ||
} | ||
prevToken = prevToken.previousToken; | ||
} | ||
var elementLines = this.sourceCodeLines; | ||
var endLine = startLine + elementLines.length - 1; | ||
var endColumn = elementLines[elementLines.length - 1].length; | ||
if (startLine === endLine) { | ||
endColumn += startColumn; | ||
} | ||
return { | ||
start: { | ||
line: startLine, | ||
column: startColumn | ||
}, | ||
end: { | ||
line: endLine, | ||
column: endColumn | ||
} | ||
}; | ||
} | ||
}, { | ||
key: 'sourceCodeLength', | ||
@@ -783,2 +829,30 @@ | ||
} | ||
}, { | ||
key: 'sourceCodeLines', | ||
/** | ||
* Generated source code lines. | ||
* | ||
* @returns {String[]} | ||
*/ | ||
get: function get() { | ||
return (0, _utilsLines.getLines)(this.sourceCode); | ||
} | ||
}, { | ||
key: 'newlineCount', | ||
/** | ||
* Generated source code line break count. | ||
* | ||
* @returns {Number} | ||
*/ | ||
get: function get() { | ||
var count = 0; | ||
var child = this._firstChild; | ||
while (child) { | ||
count += child.newlineCount; | ||
child = child._nextSibling; | ||
} | ||
return count; | ||
} | ||
}]); | ||
@@ -785,0 +859,0 @@ |
@@ -216,2 +216,17 @@ /** | ||
}, { | ||
key: 'isStatement', | ||
/** | ||
* Checks if the current element is a statement. | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
value: function isStatement() { | ||
var _ref9 = this._currentElement || {}; | ||
var isStatement = _ref9.isStatement; | ||
return isStatement; | ||
} | ||
}, { | ||
key: 'passToken', | ||
@@ -348,6 +363,6 @@ | ||
while (true) { | ||
var _ref9 = this._currentElement || {}; | ||
var _ref10 = this._currentElement || {}; | ||
var isWhitespace = _ref9.isWhitespace; | ||
var isComment = _ref9.isComment; | ||
var isWhitespace = _ref10.isWhitespace; | ||
var isComment = _ref10.isComment; | ||
@@ -368,6 +383,6 @@ if (!isWhitespace && !isComment) { | ||
while (true) { | ||
var _ref10 = this._currentElement || {}; | ||
var _ref11 = this._currentElement || {}; | ||
var isWhitespace = _ref10.isWhitespace; | ||
var isComment = _ref10.isComment; | ||
var isWhitespace = _ref11.isWhitespace; | ||
var isComment = _ref11.isComment; | ||
@@ -378,3 +393,3 @@ if (!isWhitespace && !isComment) { | ||
if (this._currentElement.lineBreakCount > 0) { | ||
if (this._currentElement.newlineCount > 0) { | ||
break; | ||
@@ -381,0 +396,0 @@ } |
@@ -33,7 +33,5 @@ 'use strict'; | ||
case 'LineComment': | ||
code = '//' + code; | ||
isComment = true; | ||
break; | ||
case 'BlockComment': | ||
code = '/*' + code + '*/'; | ||
isComment = true; | ||
@@ -48,2 +46,3 @@ break; | ||
this._sourceCodeLength = code.length; | ||
this._sourceCodeLines = (0, _utilsLines.getLines)(code); | ||
this._isComment = isComment; | ||
@@ -116,9 +115,11 @@ this._isWhitespace = isWhitespace; | ||
}, { | ||
key: 'lineBreakCount', | ||
key: 'sourceCodeLines', | ||
get: function get() { | ||
if (this._newLines === undefined) { | ||
this._newLines = (0, _utilsLines.countLineBreaks)(this._sourceCode); | ||
} | ||
return this._newLines; | ||
return this._sourceCodeLines.concat(); | ||
} | ||
}, { | ||
key: 'newlineCount', | ||
get: function get() { | ||
return this._sourceCodeLines.length - 1; | ||
} | ||
}]); | ||
@@ -125,0 +126,0 @@ |
@@ -44,3 +44,3 @@ 'use strict'; | ||
var body = []; | ||
while (!children.isEnd) { | ||
while (children.isStatement()) { | ||
body.push(children.passStatement()); | ||
@@ -50,2 +50,3 @@ children.skipNonCode(); | ||
children.passToken('EOF'); | ||
children.assertEnd(); | ||
@@ -52,0 +53,0 @@ |
@@ -120,14 +120,10 @@ 'use strict'; | ||
function buildElementTree(ast, tokens) { | ||
if (tokens.length > 0) { | ||
var firstToken = tokens[0]; | ||
ast.start = firstToken.start; | ||
ast.end = tokens[tokens.length - 1].end; | ||
return buildElementTreeItem(ast, { | ||
tokens: tokens, | ||
token: firstToken, | ||
pos: 0 | ||
}); | ||
} else { | ||
return new _elementsElementIndex2['default'].Program([]); | ||
} | ||
var firstToken = tokens[0]; | ||
ast.start = firstToken.start; | ||
ast.end = tokens[tokens.length - 1].end; | ||
return buildElementTreeItem(ast, { | ||
tokens: tokens, | ||
token: firstToken, | ||
pos: 0 | ||
}); | ||
} | ||
@@ -141,6 +137,7 @@ | ||
function buildElementTreeItem(ast, state) { | ||
var childProps = visitorKeys[ast.type]; | ||
var elementType = ast.type; | ||
var childProps = visitorKeys[elementType]; | ||
if (!childProps) { | ||
throw new Error('Cannot iterate using ' + ast.type); | ||
throw new Error('Cannot iterate using ' + elementType); | ||
} | ||
@@ -150,3 +147,3 @@ | ||
// the next case or till the end of the switch statement. | ||
if (ast.type === 'SwitchStatement') { | ||
if (elementType === 'SwitchStatement') { | ||
for (var i = 0; i < ast.cases.length; i++) { | ||
@@ -179,6 +176,6 @@ var switchCase = ast.cases[i]; | ||
var NodeClass = _elementsElementIndex2['default'][ast.type]; | ||
var NodeClass = _elementsElementIndex2['default'][elementType]; | ||
if (!NodeClass) { | ||
throw new Error('Cannot create ' + ast.type + ' instance'); | ||
throw new Error('Cannot create ' + elementType + ' instance'); | ||
} | ||
@@ -195,3 +192,3 @@ | ||
if (!state.token || state.token.start === end) { | ||
if (!state.token || state.token.start === end && (state.token.end !== end || elementType !== 'Program')) { | ||
return new NodeClass(children); | ||
@@ -201,8 +198,9 @@ } | ||
var endOfAstReached = state.token.end === end; | ||
var addedTokenType = state.token.type; | ||
if (endOfAstReached && ast.type === 'Identifier' && state.token.type === 'Keyword') { | ||
state.token.type = 'Identifier'; | ||
if (endOfAstReached && ast.type === 'Identifier' && addedTokenType === 'Keyword') { | ||
addedTokenType = 'Identifier'; | ||
} | ||
children[children.length] = new _elementsToken2['default'](state.token.type, state.token.value); | ||
children[children.length] = new _elementsToken2['default'](addedTokenType, state.token.value); | ||
@@ -212,2 +210,6 @@ state.pos++; | ||
if (elementType === 'Program' && addedTokenType !== 'EOF') { | ||
continue; | ||
} | ||
if (endOfAstReached) { | ||
@@ -244,4 +246,3 @@ return new NodeClass(children); | ||
if (commentToken && codeToken.start > commentToken.start) { | ||
token = commentToken; | ||
token.type += 'Comment'; | ||
token = processCommentToken(commentToken); | ||
commentToken = commentTokens[++commentPtr]; | ||
@@ -255,4 +256,3 @@ } else { | ||
if (commentToken) { | ||
token = commentToken; | ||
token.type += 'Comment'; | ||
token = processCommentToken(commentToken); | ||
commentToken = commentTokens[++commentPtr]; | ||
@@ -333,5 +333,24 @@ } else { | ||
token.value = source.slice(token.start, token.end); | ||
} else if (type === tt.eof) { | ||
token.type = 'EOF'; | ||
token.value = ''; | ||
} | ||
return token; | ||
} | ||
/** | ||
* Babel does not add // and /*..*\/ to the token value. | ||
* Fixing this. | ||
* | ||
* @param {Object} token | ||
*/ | ||
function processCommentToken(token) { | ||
if (token.type === 'Line') { | ||
token.value = '//' + token.value; | ||
} else { | ||
token.value = '/*' + token.value + '*/'; | ||
} | ||
token.type += 'Comment'; | ||
return token; | ||
} |
@@ -55,3 +55,2 @@ 'use strict'; | ||
var ast = (0, _babelCore.parse)(code, opts); | ||
tokens.pop(); | ||
ast.tokens = tokens; | ||
@@ -58,0 +57,0 @@ ast.comments = comments; |
@@ -6,8 +6,7 @@ "use strict"; | ||
}); | ||
exports.countLineBreaks = countLineBreaks; | ||
var lineBreakRegex = /\r\n|\r|\n/g; | ||
exports.getLines = getLines; | ||
var lineBreakRegex = /\r\n|\r|\n/; | ||
function countLineBreaks(input) { | ||
var match = input.match(lineBreakRegex); | ||
return match ? match.length : 0; | ||
function getLines(input) { | ||
return input.split(lineBreakRegex); | ||
} |
{ | ||
"name": "cst", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "JavaScript CST Implementation", | ||
@@ -28,3 +28,3 @@ "author": "Marat Dulin", | ||
"devDependencies": { | ||
"babel": "^5.5.8", | ||
"babel": "5.5.8", | ||
"benchmark": "^1.0.0", | ||
@@ -41,4 +41,4 @@ "chai": "^3.0.0", | ||
"dependencies": { | ||
"babel-core": "^5.5.5" | ||
"babel-core": "5.5.5" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
298390
5103
+ Addedacorn@1.2.2(transitive)
+ Addedacorn-jsx@1.0.3(transitive)
+ Addedast-types@0.7.80.8.15(transitive)
+ Addedbabel-core@5.5.5(transitive)
+ Addedcore-js@0.9.18(transitive)
+ Addedesprima-fb@13001.1.0-dev-harmony-fb(transitive)
+ Addedesquery@0.4.0(transitive)
+ Addedestraverse@4.3.0(transitive)
+ Addedjs-tokens@1.0.0(transitive)
+ Addedleft-pad@0.0.3(transitive)
+ Addedline-numbers@0.2.0(transitive)
+ Addedrecast@0.10.43(transitive)
+ Addedregenerator@0.8.28(transitive)
+ Addedsource-map@0.4.4(transitive)
+ Addedstrip-json-comments@1.0.4(transitive)
- Removedast-types@0.8.12(transitive)
- Removedbabel-core@5.8.38(transitive)
- Removedbabel-plugin-constant-folding@1.0.1(transitive)
- Removedbabel-plugin-dead-code-elimination@1.0.2(transitive)
- Removedbabel-plugin-eval@1.0.1(transitive)
- Removedbabel-plugin-inline-environment-variables@1.0.1(transitive)
- Removedbabel-plugin-jscript@1.0.4(transitive)
- Removedbabel-plugin-member-expression-literals@1.0.1(transitive)
- Removedbabel-plugin-property-literals@1.0.1(transitive)
- Removedbabel-plugin-proto-to-assign@1.0.4(transitive)
- Removedbabel-plugin-react-constant-elements@1.0.3(transitive)
- Removedbabel-plugin-react-display-name@1.0.3(transitive)
- Removedbabel-plugin-remove-console@1.0.1(transitive)
- Removedbabel-plugin-remove-debugger@1.0.1(transitive)
- Removedbabel-plugin-runtime@1.0.7(transitive)
- Removedbabel-plugin-undeclared-variables-check@1.0.2(transitive)
- Removedbabel-plugin-undefined-to-void@1.1.6(transitive)
- Removedbabylon@5.8.38(transitive)
- Removedcore-js@1.2.7(transitive)
- Removedjs-tokens@1.0.1(transitive)
- Removedjson5@0.4.0(transitive)
- Removedpath-exists@1.0.0(transitive)
- Removedrecast@0.10.33(transitive)
- Removedregenerator@0.8.40(transitive)
- Removedtry-resolve@1.0.1(transitive)
Updatedbabel-core@5.5.5