decaffeinate-parser
Advanced tools
Comparing version 2.0.0 to 2.0.1
import * as CoffeeScript from 'coffee-script'; | ||
import LinesAndColumns from 'lines-and-columns'; | ||
import lex, { RELATION, OPERATOR, HERECOMMENT, COMMENT, NEWLINE, IF, RBRACKET, LBRACKET } from 'coffee-lex'; | ||
@@ -8,2 +9,3 @@ import { inspect } from 'util'; | ||
* @returns {function(number, number): number} | ||
* @deprecated | ||
*/ | ||
@@ -63,2 +65,16 @@ function lineColumnMapper(source) { | ||
var _extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
var inherits = function (subClass, superClass) { | ||
@@ -162,3 +178,9 @@ if (typeof superClass !== "function" && superClass !== null) { | ||
this.source = source; | ||
/** | ||
* Use `linesAndColumns` instead. | ||
* | ||
* @deprecated | ||
*/ | ||
this.lineMap = lineColumnMapper(source); | ||
this.linesAndColumns = new LinesAndColumns(source); | ||
this.ast = ast; | ||
@@ -182,3 +204,3 @@ this.sourceTokens = sourceTokens; | ||
} else { | ||
return [this.lineMap(locatable.first_line, locatable.first_column), this.lineMap(locatable.last_line, locatable.last_column) + 1]; | ||
return [this.linesAndColumns.indexForLocation({ line: locatable.first_line, column: locatable.first_column }), this.linesAndColumns.indexForLocation({ line: locatable.last_line, column: locatable.last_column })]; | ||
} | ||
@@ -303,2 +325,41 @@ } | ||
/** | ||
* Assumes first_line/first_column are correct. | ||
*/ | ||
function fixInvalidLocationData(locationData, linesAndColumns) { | ||
var last_line = locationData.last_line; | ||
var last_column = locationData.last_column; | ||
var indexForLocation = linesAndColumns.indexForLocation({ line: last_line, column: last_column }); | ||
if (indexForLocation !== null) { | ||
return locationData; | ||
} else { | ||
var offset = 1; | ||
for (;;) { | ||
var index = linesAndColumns.indexForLocation({ line: last_line, column: last_column - offset }); | ||
offset++; | ||
if (index !== null) { | ||
var location = linesAndColumns.locationForIndex(index + offset); | ||
if (!location) { | ||
throw new Error('Unable to determine adjustment offset for incorrect location data: ' + (JSON.stringify(locationData) + '. No valid location found for index: ') + ('' + (index + offset))); | ||
} | ||
last_line = location.line; | ||
last_column = location.column; | ||
break; | ||
} | ||
} | ||
return _extends({}, locationData, { | ||
last_line: last_line, | ||
last_column: last_column | ||
}); | ||
} | ||
} | ||
/** | ||
* @param {Object} first | ||
@@ -618,52 +679,2 @@ * @param {Object} second | ||
/** | ||
* @param {string} source | ||
* @param {{first_line: number, first_column: number, last_line: number, last_column: number}} loc | ||
* @param {function(number, number): number} mapper | ||
*/ | ||
function trimNonMatchingParentheses(source, loc, mapper) { | ||
var first = mapper(loc.first_line, loc.first_column); | ||
var last = mapper(loc.last_line, loc.last_column); | ||
// Ensure first & last are in bounds. | ||
if (first < 0) { | ||
first = 0; | ||
var firstLoc = mapper.invert(first); | ||
loc.first_line = firstLoc.line; | ||
loc.first_column = firstLoc.column; | ||
} | ||
if (last >= source.length) { | ||
last = source.length - 1; | ||
var lastLoc = mapper.invert(last); | ||
loc.last_line = lastLoc.line; | ||
loc.last_column = lastLoc.column; | ||
} | ||
var level = 0; | ||
var lastBalancedIndex = first; | ||
for (var index = first; index <= last; index++) { | ||
if (source[index] === '(') { | ||
level++; | ||
} else if (source[index] === ')') { | ||
level--; | ||
if (level < 0) { | ||
break; | ||
} | ||
} | ||
if (level === 0) { | ||
lastBalancedIndex = index; | ||
} | ||
} | ||
if (level !== 0 && lastBalancedIndex !== last) { | ||
last = lastBalancedIndex; | ||
var _lastLoc = mapper.invert(last); | ||
loc.last_line = _lastLoc.line; | ||
loc.last_column = _lastLoc.column; | ||
} | ||
} | ||
function patchCoffeeScript(_ref) { | ||
@@ -791,3 +802,3 @@ var parse = _ref.nodes; | ||
var source = context.source; | ||
var mapper = context.lineMap; | ||
var linesAndColumns = context.linesAndColumns; | ||
@@ -809,2 +820,5 @@ fixLocations(context.ast); | ||
}); | ||
node.locationData = fixInvalidLocationData(node.locationData, context.linesAndColumns); | ||
switch (type(node)) { | ||
@@ -825,5 +839,5 @@ case 'Value': | ||
var lbracket = context.sourceTokens.tokenAtIndex(rangeOfBrackets[0]); | ||
var lbracketLoc = mapper.invert(lbracket.start); | ||
var lbracketLoc = linesAndColumns.locationForIndex(lbracket.start); | ||
var rbracket = context.sourceTokens.tokenAtIndex(rangeOfBrackets[1].previous()); | ||
var rbracketLoc = mapper.invert(rbracket.start); | ||
var rbracketLoc = linesAndColumns.locationForIndex(rbracket.start); | ||
node.locationData = { | ||
@@ -857,3 +871,3 @@ first_line: lbracketLoc.line, | ||
var loc = node.locationData; | ||
var start = mapper(loc.first_line, loc.first_column); | ||
var start = linesAndColumns.indexForLocation({ line: loc.first_line, column: loc.first_column }); | ||
var isImplicitObject = source[start] !== '{'; | ||
@@ -895,3 +909,3 @@ if (isImplicitObject) { | ||
var calleeLoc = node.variable.locationData; | ||
var calleeEnd = mapper(calleeLoc.last_line, calleeLoc.last_column) + 1; | ||
var calleeEnd = linesAndColumns.indexForLocation({ line: calleeLoc.last_line, column: calleeLoc.last_column }) + 1; | ||
// Account for soaked calls, e.g. `a?()`. | ||
@@ -987,3 +1001,3 @@ if (source[calleeEnd] === '?') { | ||
function rangeOfBracketTokensForIndexNode(indexNode) { | ||
var start = mapper(indexNode.locationData.first_line, indexNode.locationData.first_column); | ||
var start = linesAndColumns.indexForLocation({ line: indexNode.locationData.first_line, column: indexNode.locationData.first_column }); | ||
var startTokenIndex = context.sourceTokens.indexOfTokenStartingAtSourceIndex(start); | ||
@@ -1023,7 +1037,2 @@ var range = context.sourceTokens.rangeOfMatchingTokensContainingTokenIndex(LBRACKET, RBRACKET, startTokenIndex); | ||
if (node.locationData && type(node) !== 'Literal') { | ||
// should't trim Literal, i.e. "(". | ||
trimNonMatchingParentheses(source, node.locationData, mapper); | ||
} | ||
var _ret = function () { | ||
@@ -1060,4 +1069,4 @@ switch (type(node)) { | ||
} else { | ||
var start = mapper(node.locationData.first_line, node.locationData.first_column); | ||
var end = mapper(node.locationData.last_line, node.locationData.last_column) + 1; | ||
var start = linesAndColumns.indexForLocation({ line: node.locationData.first_line, column: node.locationData.first_column }); | ||
var end = linesAndColumns.indexForLocation({ line: node.locationData.last_line, column: node.locationData.last_column }) + 1; | ||
var raw = source.slice(start, end); | ||
@@ -1663,4 +1672,4 @@ var literal = parseLiteral(raw, start); | ||
if (loc) { | ||
var start = mapper(loc.first_line, loc.first_column); | ||
var end = mapper(loc.last_line, loc.last_column) + 1; | ||
var start = linesAndColumns.indexForLocation({ line: loc.first_line, column: loc.first_column }); | ||
var end = linesAndColumns.indexForLocation({ line: loc.last_line, column: loc.last_column }) + 1; | ||
result.line = loc.first_line + 1; | ||
@@ -1765,3 +1774,3 @@ result.column = loc.first_column + 1; | ||
function buildQuasi(range) { | ||
var loc = mapper.invert(range[0]); | ||
var loc = linesAndColumns.locationForIndex(range[0]); | ||
return { | ||
@@ -1778,3 +1787,3 @@ type: 'String', | ||
function buildQuasiWithString(range, raw) { | ||
var loc = mapper.invert(range[0]); | ||
var loc = linesAndColumns.locationForIndex(range[0]); | ||
return { | ||
@@ -2073,3 +2082,3 @@ type: 'String', | ||
function expandLocationLeftThrough(loc, string) { | ||
var offset = mapper(loc.first_line, loc.first_column); | ||
var offset = linesAndColumns.indexForLocation({ line: loc.first_line, column: loc.first_column }); | ||
offset = source.lastIndexOf(string, offset); | ||
@@ -2081,3 +2090,3 @@ | ||
var newLoc = mapper.invert(offset); | ||
var newLoc = linesAndColumns.locationForIndex(offset); | ||
@@ -2084,0 +2093,0 @@ return { |
@@ -25,14 +25,20 @@ { | ||
"coffee-lex": "^1.4.1", | ||
"coffee-script": "^1.10.0" | ||
"coffee-script": "^1.10.0", | ||
"lines-and-columns": "^1.1.5" | ||
}, | ||
"devDependencies": { | ||
"babel": "^6.5.2", | ||
"babel-plugin-object-rest-spread": "0.0.0", | ||
"babel-plugin-syntax-flow": "^6.8.0", | ||
"babel-plugin-syntax-object-rest-spread": "^6.8.0", | ||
"babel-plugin-transform-flow-strip-types": "^6.8.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.8.0", | ||
"babel-preset-es2015": "^6.6.0", | ||
"babel-preset-es2015-rollup": "^1.1.1", | ||
"babel-register": "^6.7.2", | ||
"babelrc-rollup": "^1.1.0", | ||
"babelrc-rollup": "^2.0.0", | ||
"coffee-script-redux": "^2.0.0-beta8", | ||
"cz-conventional-changelog": "^1.1.6", | ||
"mocha": "^2.4.5", | ||
"rollup": "^0.32.0", | ||
"rollup": "^0.34.1", | ||
"rollup-plugin-babel": "^2.4.0", | ||
@@ -54,3 +60,3 @@ "semantic-release": "^4.3.5", | ||
}, | ||
"version": "2.0.0" | ||
"version": "2.0.1" | ||
} |
Sorry, the diff of this file is too big to display
136402
3613
3
17
+ Addedlines-and-columns@^1.1.5
+ Addedlines-and-columns@1.2.4(transitive)