babel-generator
Advanced tools
Comparing version 6.7.5 to 6.7.7
@@ -36,2 +36,9 @@ "use strict"; | ||
this.last = ""; | ||
this.map = null; | ||
this._sourcePosition = { | ||
line: null, | ||
column: null, | ||
filename: null | ||
}; | ||
} | ||
@@ -241,2 +248,36 @@ | ||
/** | ||
* Sets a given position as the current source location so generated code after this call | ||
* will be given this position in the sourcemap. | ||
*/ | ||
Buffer.prototype.source = function source(prop, loc) { | ||
if (prop && !loc) return; | ||
var pos = loc ? loc[prop] : null; | ||
this._sourcePosition.line = pos ? pos.line : null; | ||
this._sourcePosition.column = pos ? pos.column : null; | ||
this._sourcePosition.filename = loc && loc.filename || null; | ||
}; | ||
/** | ||
* Call a callback with a specific source location and restore on completion. | ||
*/ | ||
Buffer.prototype.withSource = function withSource(prop, loc, cb) { | ||
// Use the call stack to manage a stack of "source location" data. | ||
var originalLine = this._sourcePosition.line; | ||
var originalColumn = this._sourcePosition.column; | ||
var originalFilename = this._sourcePosition.filename; | ||
this.source(prop, loc); | ||
cb(); | ||
this._sourcePosition.line = originalLine; | ||
this._sourcePosition.column = originalColumn; | ||
this._sourcePosition.filename = originalFilename; | ||
}; | ||
/** | ||
* Push a string to the buffer, maintaining indentation and newlines. | ||
@@ -287,2 +328,5 @@ */ | ||
// If there the line is ending, adding a new mapping marker is redundant | ||
if (str[0] !== "\n") this.map.mark(this._sourcePosition); | ||
// | ||
@@ -289,0 +333,0 @@ this.position.push(str); |
@@ -35,4 +35,7 @@ "use strict"; | ||
if (!this.format.retainLines && !this.format.concise) this.removeLast("\n"); | ||
this.source("end", node.loc); | ||
this.rightBrace(); | ||
} else { | ||
this.source("end", node.loc); | ||
this.push("}"); | ||
@@ -39,0 +42,0 @@ } |
@@ -5,2 +5,4 @@ "use strict"; | ||
var _getIterator = require("babel-runtime/core-js/get-iterator")["default"]; | ||
var _interopRequireDefault = require("babel-runtime/helpers/interop-require-default")["default"]; | ||
@@ -29,20 +31,65 @@ | ||
function find(obj, node, parent, printStack) { | ||
if (!obj) return; | ||
var result = undefined; | ||
function expandAliases(obj) { | ||
var newObj = {}; | ||
var types = _Object$keys(obj); | ||
for (var i = 0; i < types.length; i++) { | ||
var type = types[i]; | ||
function add(type, func) { | ||
var fn = newObj[type]; | ||
newObj[type] = fn ? function (node, parent, stack) { | ||
var result = fn(node, parent, stack); | ||
if (t.is(type, node)) { | ||
var fn = obj[type]; | ||
result = fn(node, parent, printStack); | ||
if (result != null) break; | ||
return result == null ? func(node, parent, stack) : result; | ||
} : func; | ||
} | ||
for (var _iterator = _Object$keys(obj), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _getIterator(_iterator);;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var type = _ref; | ||
var aliases = t.FLIPPED_ALIAS_KEYS[type]; | ||
if (aliases) { | ||
for (var _iterator2 = aliases, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _getIterator(_iterator2);;) { | ||
var _ref2; | ||
if (_isArray2) { | ||
if (_i2 >= _iterator2.length) break; | ||
_ref2 = _iterator2[_i2++]; | ||
} else { | ||
_i2 = _iterator2.next(); | ||
if (_i2.done) break; | ||
_ref2 = _i2.value; | ||
} | ||
var alias = _ref2; | ||
add(alias, obj[type]); | ||
} | ||
} else { | ||
add(type, obj[type]); | ||
} | ||
} | ||
return result; | ||
return newObj; | ||
} | ||
// Rather than using `t.is` on each object property, we pre-expand any type aliases | ||
// into concrete types so that the 'find' call below can be as fast as possible. | ||
var expandedParens = expandAliases(parens); | ||
var expandedWhitespaceNodes = expandAliases(_whitespace2["default"].nodes); | ||
var expandedWhitespaceList = expandAliases(_whitespace2["default"].list); | ||
function find(obj, node, parent, printStack) { | ||
var fn = obj[node.type]; | ||
return fn ? fn(node, parent, printStack) : null; | ||
} | ||
function isOrHasCallExpression(node) { | ||
@@ -71,6 +118,6 @@ if (t.isCallExpression(node)) { | ||
var linesInfo = find(_whitespace2["default"].nodes, node, parent); | ||
var linesInfo = find(expandedWhitespaceNodes, node, parent); | ||
if (!linesInfo) { | ||
var items = find(_whitespace2["default"].list, node, parent); | ||
var items = find(expandedWhitespaceList, node, parent); | ||
if (items) { | ||
@@ -102,3 +149,3 @@ for (var i = 0; i < items.length; i++) { | ||
return find(parens, node, parent, printStack); | ||
return find(expandedParens, node, parent, printStack); | ||
} |
@@ -50,2 +50,6 @@ /* eslint max-len: 0 */ | ||
Printer.prototype.print = function print(node, parent) { | ||
// istanbul ignore next | ||
var _this = this; | ||
var opts = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
@@ -90,6 +94,7 @@ | ||
this.map.mark(node); | ||
var loc = t.isProgram(node) || t.isFile(node) ? null : node.loc; | ||
this.withSource("start", loc, function () { | ||
_this._print(node, parent); | ||
}); | ||
this._print(node, parent); | ||
// Check again if any of our children may have left an aux comment on the stack | ||
@@ -104,3 +109,2 @@ if (node.loc) this.printAuxAfterComment(); | ||
this._printStack.pop(); | ||
if (parent) this.map.mark(parent); | ||
if (opts.after) opts.after(); | ||
@@ -164,3 +168,3 @@ | ||
var _this = this; | ||
var _this2 = this; | ||
@@ -186,7 +190,7 @@ var opts = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
if (opts.separator && parent.loc) { | ||
_this.printAuxAfterComment(); | ||
_this2.printAuxAfterComment(); | ||
} | ||
if (opts.separator && i < len - 1) { | ||
_this.push(opts.separator); | ||
_this2.push(opts.separator); | ||
} | ||
@@ -232,7 +236,7 @@ } | ||
Printer.prototype.printTrailingComments = function printTrailingComments(node, parent) { | ||
this.printComments(this.getComments("trailingComments", node, parent)); | ||
this.printComments(this.getComments(false, node, parent)); | ||
}; | ||
Printer.prototype.printLeadingComments = function printLeadingComments(node, parent) { | ||
this.printComments(this.getComments("leadingComments", node, parent)); | ||
this.printComments(this.getComments(true, node, parent)); | ||
}; | ||
@@ -268,2 +272,5 @@ | ||
Printer.prototype._printNewline = function _printNewline(leading, node, parent, opts) { | ||
// Fast path since 'this.newline' does nothing when not tracking lines. | ||
if (this.format.retainLines || this.format.compact) return; | ||
if (!opts.statement && !n.isUserWhitespacable(node, parent)) { | ||
@@ -273,2 +280,9 @@ return; | ||
// Fast path for concise since 'this.newline' just inserts a space when | ||
// concise formatting is in use. | ||
if (this.format.concise) { | ||
this.space(); | ||
return; | ||
} | ||
var lines = 0; | ||
@@ -299,4 +313,6 @@ | ||
Printer.prototype.getComments = function getComments(key, node) { | ||
return node && node[key] || []; | ||
Printer.prototype.getComments = function getComments(leading, node) { | ||
// Note, we use a boolean flag here instead of passing in the attribute name as it is faster | ||
// because this is called extremely frequently. | ||
return node && (leading ? node.leadingComments : node.trailingComments) || []; | ||
}; | ||
@@ -317,2 +333,6 @@ | ||
Printer.prototype.printComment = function printComment(comment) { | ||
// istanbul ignore next | ||
var _this3 = this; | ||
if (!this.shouldPrintComment(comment)) return; | ||
@@ -328,42 +348,45 @@ | ||
this.catchUp(comment); | ||
// Exclude comments from source mappings since they will only clutter things. | ||
this.withSource(null, null, function () { | ||
_this3.catchUp(comment); | ||
// whitespace before | ||
this.newline(this.whitespace.getNewlinesBefore(comment)); | ||
// whitespace before | ||
_this3.newline(_this3.whitespace.getNewlinesBefore(comment)); | ||
var column = this.position.column; | ||
var val = this.generateComment(comment); | ||
var column = _this3.position.column; | ||
var val = _this3.generateComment(comment); | ||
if (column && !this.isLast(["\n", " ", "[", "{"])) { | ||
this._push(" "); | ||
column++; | ||
} | ||
if (column && !_this3.isLast(["\n", " ", "[", "{"])) { | ||
_this3._push(" "); | ||
column++; | ||
} | ||
// | ||
if (comment.type === "CommentBlock" && this.format.indent.adjustMultilineComment) { | ||
var offset = comment.loc && comment.loc.start.column; | ||
if (offset) { | ||
var newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g"); | ||
val = val.replace(newlineRegex, "\n"); | ||
// | ||
if (comment.type === "CommentBlock" && _this3.format.indent.adjustMultilineComment) { | ||
var offset = comment.loc && comment.loc.start.column; | ||
if (offset) { | ||
var newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g"); | ||
val = val.replace(newlineRegex, "\n"); | ||
} | ||
var indent = Math.max(_this3.indentSize(), column); | ||
val = val.replace(/\n/g, "\n" + _repeating2["default"](" ", indent)); | ||
} | ||
var indent = Math.max(this.indentSize(), column); | ||
val = val.replace(/\n/g, "\n" + _repeating2["default"](" ", indent)); | ||
} | ||
if (column === 0) { | ||
val = _this3.getIndent() + val; | ||
} | ||
if (column === 0) { | ||
val = this.getIndent() + val; | ||
} | ||
// force a newline for line comments when retainLines is set in case the next printed node | ||
// doesn't catch up | ||
if ((_this3.format.compact || _this3.format.concise || _this3.format.retainLines) && comment.type === "CommentLine") { | ||
val += "\n"; | ||
} | ||
// force a newline for line comments when retainLines is set in case the next printed node | ||
// doesn't catch up | ||
if ((this.format.compact || this.format.concise || this.format.retainLines) && comment.type === "CommentLine") { | ||
val += "\n"; | ||
} | ||
// | ||
_this3._push(val); | ||
// | ||
this._push(val); | ||
// whitespace after | ||
this.newline(this.whitespace.getNewlinesAfter(comment)); | ||
// whitespace after | ||
_this3.newline(_this3.whitespace.getNewlinesAfter(comment)); | ||
}); | ||
}; | ||
@@ -370,0 +393,0 @@ |
@@ -9,4 +9,2 @@ "use strict"; | ||
var _interopRequireWildcard = require("babel-runtime/helpers/interop-require-wildcard")["default"]; | ||
exports.__esModule = true; | ||
@@ -18,6 +16,2 @@ | ||
var _babelTypes = require("babel-types"); | ||
var t = _interopRequireWildcard(_babelTypes); | ||
/** | ||
@@ -71,38 +65,36 @@ * Build a sourcemap. | ||
/** | ||
* Mark a node's generated position, and add it to the sourcemap. | ||
* Mark the current generated position with a source position. May also be passed null line/column | ||
* values to insert a mapping to nothing. | ||
*/ | ||
SourceMap.prototype.mark = function mark(node) { | ||
var loc = node.loc; | ||
if (!loc) return; // no location info | ||
SourceMap.prototype.mark = function mark(sourcePos) { | ||
var map = this.map; | ||
if (!map) return; // no source map | ||
if (t.isProgram(node) || t.isFile(node)) return; // illegal mapping nodes | ||
var position = this.position; | ||
var generated = { | ||
line: position.line, | ||
column: position.column | ||
}; | ||
// Adding an empty mapping at the start of a generated line just clutters the map. | ||
if (this._lastGenLine !== position.line && sourcePos.line === null) return; | ||
var original = loc.start; | ||
// Avoid emitting duplicates on either side. Duplicated | ||
// original values creates unnecesssarily large source maps | ||
// and increases compile time. Duplicates on the generated | ||
// side can lead to incorrect mappings. | ||
if (comparePosition(original, this.last.original) || comparePosition(generated, this.last.generated)) { | ||
// If this mapping points to the same source location as the last one, we can ignore it since | ||
// the previous one covers it. | ||
if (this._lastGenLine === position.line && this._lastSourceLine === sourcePos.line && this._lastSourceColumn === sourcePos.column) { | ||
return; | ||
} | ||
this.last = { | ||
source: loc.filename || this.opts.sourceFileName, | ||
generated: generated, | ||
original: original | ||
}; | ||
this._lastGenLine = position.line; | ||
this._lastSourceLine = sourcePos.line; | ||
this._lastSourceColumn = sourcePos.column; | ||
map.addMapping(this.last); | ||
map.addMapping({ | ||
generated: { | ||
line: position.line, | ||
column: position.column | ||
}, | ||
source: sourcePos.line == null ? null : sourcePos.filename || this.opts.sourceFileName, | ||
original: sourcePos.line == null ? null : { | ||
line: sourcePos.line, | ||
column: sourcePos.column | ||
} | ||
}); | ||
}; | ||
@@ -114,6 +106,2 @@ | ||
exports["default"] = SourceMap; | ||
function comparePosition(a, b) { | ||
return a.line === b.line && a.column === b.column; | ||
} | ||
module.exports = exports["default"]; |
{ | ||
"name": "babel-generator", | ||
"version": "6.7.5", | ||
"version": "6.7.7", | ||
"description": "Turns an AST into code.", | ||
@@ -16,3 +16,3 @@ "author": "Sebastian McKenzie <sebmck@gmail.com>", | ||
"babel-runtime": "^5.0.0", | ||
"babel-types": "^6.7.2", | ||
"babel-types": "^6.7.7", | ||
"detect-indent": "^3.0.1", | ||
@@ -19,0 +19,0 @@ "is-integer": "^1.0.4", |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
90898
2704
0
Updatedbabel-types@^6.7.7