babel-generator
Advanced tools
Comparing version 7.0.0-alpha.12 to 7.0.0-alpha.14
@@ -76,5 +76,9 @@ "use strict"; | ||
Buffer.prototype.queue = function queue(str) { | ||
if (str === "\n") while (this._queue.length > 0 && SPACES_RE.test(this._queue[0][0])) { | ||
this._queue.shift(); | ||
}var _sourcePosition2 = this._sourcePosition, | ||
if (str === "\n") { | ||
while (this._queue.length > 0 && SPACES_RE.test(this._queue[0][0])) { | ||
this._queue.shift(); | ||
} | ||
} | ||
var _sourcePosition2 = this._sourcePosition, | ||
line = _sourcePosition2.line, | ||
@@ -114,7 +118,11 @@ column = _sourcePosition2.column, | ||
Buffer.prototype.removeTrailingNewline = function removeTrailingNewline() { | ||
if (this._queue.length > 0 && this._queue[0][0] === "\n") this._queue.shift(); | ||
if (this._queue.length > 0 && this._queue[0][0] === "\n") { | ||
this._queue.shift(); | ||
} | ||
}; | ||
Buffer.prototype.removeLastSemicolon = function removeLastSemicolon() { | ||
if (this._queue.length > 0 && this._queue[0][0] === ";") this._queue.shift(); | ||
if (this._queue.length > 0 && this._queue[0][0] === ";") { | ||
this._queue.shift(); | ||
} | ||
}; | ||
@@ -121,0 +129,0 @@ |
@@ -84,4 +84,9 @@ "use strict"; | ||
this.print(node.callee, node); | ||
if (node.arguments.length === 0 && this.format.minified && !t.isCallExpression(parent, { callee: node }) && !t.isMemberExpression(parent) && !t.isNewExpression(parent)) return; | ||
if (this.format.minified && node.arguments.length === 0 && !node.optional && !t.isCallExpression(parent, { callee: node }) && !t.isMemberExpression(parent) && !t.isNewExpression(parent)) { | ||
return; | ||
} | ||
if (node.optional) { | ||
this.token("?."); | ||
} | ||
this.token("("); | ||
@@ -120,2 +125,5 @@ this.printList(node.arguments, node); | ||
if (node.optional) { | ||
this.token("?."); | ||
} | ||
this.token("("); | ||
@@ -229,2 +237,5 @@ | ||
if (node.optional) { | ||
this.token("?."); | ||
} | ||
if (computed) { | ||
@@ -235,3 +246,5 @@ this.token("["); | ||
} else { | ||
this.token("."); | ||
if (!node.optional) { | ||
this.token("."); | ||
} | ||
this.print(node.property, node); | ||
@@ -238,0 +251,0 @@ } |
"use strict"; | ||
exports.__esModule = true; | ||
exports.TypeParameterDeclaration = exports.StringLiteralTypeAnnotation = exports.NumberLiteralTypeAnnotation = exports.GenericTypeAnnotation = exports.ClassImplements = undefined; | ||
exports.AnyTypeAnnotation = AnyTypeAnnotation; | ||
@@ -16,2 +17,4 @@ exports.ArrayTypeAnnotation = ArrayTypeAnnotation; | ||
exports.DeclareVariable = DeclareVariable; | ||
exports.DeclareExportDeclaration = DeclareExportDeclaration; | ||
exports.DeclareExportAllDeclaration = DeclareExportAllDeclaration; | ||
exports.ExistsTypeAnnotation = ExistsTypeAnnotation; | ||
@@ -61,2 +64,9 @@ exports.FunctionTypeAnnotation = FunctionTypeAnnotation; | ||
exports.VoidTypeAnnotation = VoidTypeAnnotation; | ||
var _babelTypes = require("babel-types"); | ||
var t = _interopRequireWildcard(_babelTypes); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function AnyTypeAnnotation() { | ||
@@ -84,5 +94,7 @@ this.word("any"); | ||
function DeclareClass(node) { | ||
this.word("declare"); | ||
this.space(); | ||
function DeclareClass(node, parent) { | ||
if (!t.isDeclareExportDeclaration(parent)) { | ||
this.word("declare"); | ||
this.space(); | ||
} | ||
this.word("class"); | ||
@@ -93,5 +105,7 @@ this.space(); | ||
function DeclareFunction(node) { | ||
this.word("declare"); | ||
this.space(); | ||
function DeclareFunction(node, parent) { | ||
if (!t.isDeclareExportDeclaration(parent)) { | ||
this.word("declare"); | ||
this.space(); | ||
} | ||
this.word("function"); | ||
@@ -135,5 +149,7 @@ this.space(); | ||
function DeclareVariable(node) { | ||
this.word("declare"); | ||
this.space(); | ||
function DeclareVariable(node, parent) { | ||
if (!t.isDeclareExportDeclaration(parent)) { | ||
this.word("declare"); | ||
this.space(); | ||
} | ||
this.word("var"); | ||
@@ -146,2 +162,53 @@ this.space(); | ||
function DeclareExportDeclaration(node) { | ||
this.word("declare"); | ||
this.space(); | ||
this.word("export"); | ||
this.space(); | ||
if (node.default) { | ||
this.word("default"); | ||
this.space(); | ||
} | ||
FlowExportDeclaration.apply(this, arguments); | ||
} | ||
function DeclareExportAllDeclaration(node) { | ||
this.word("declare"); | ||
this.space(); | ||
this.word("export"); | ||
this.space(); | ||
this.token("*"); | ||
this.space(); | ||
this.word("from"); | ||
this.space(); | ||
this.print(node.source, node); | ||
this.semicolon(); | ||
} | ||
function FlowExportDeclaration(node) { | ||
if (node.declaration) { | ||
var declar = node.declaration; | ||
this.print(declar, node); | ||
if (!t.isStatement(declar)) this.semicolon(); | ||
} else { | ||
this.token("{"); | ||
if (node.specifiers.length) { | ||
this.space(); | ||
this.printList(node.specifiers, node); | ||
this.space(); | ||
} | ||
this.token("}"); | ||
if (node.source) { | ||
this.space(); | ||
this.word("from"); | ||
this.space(); | ||
this.print(node.source, node); | ||
} | ||
this.semicolon(); | ||
} | ||
} | ||
function ExistsTypeAnnotation() { | ||
@@ -327,3 +394,3 @@ this.token("*"); | ||
var props = node.properties.concat(node.callProperties, node.indexers); | ||
var props = node.properties.concat(node.callProperties || [], node.indexers || []); | ||
@@ -330,0 +397,0 @@ if (props.length) { |
@@ -11,6 +11,2 @@ "use strict"; | ||
var _detectIndent = require("detect-indent"); | ||
var _detectIndent2 = _interopRequireDefault(_detectIndent); | ||
var _sourceMap = require("./source-map"); | ||
@@ -47,7 +43,6 @@ | ||
var tokens = ast.tokens || []; | ||
var format = normalizeOptions(code, opts, tokens); | ||
var format = normalizeOptions(code, opts); | ||
var map = opts.sourceMaps ? new _sourceMap2.default(opts, code) : null; | ||
var _this = _possibleConstructorReturn(this, _Printer.call(this, format, map, tokens)); | ||
var _this = _possibleConstructorReturn(this, _Printer.call(this, format, map)); | ||
@@ -65,9 +60,3 @@ _this.ast = ast; | ||
function normalizeOptions(code, opts, tokens) { | ||
var style = " "; | ||
if (code && typeof code === "string") { | ||
var indent = (0, _detectIndent2.default)(code).indent; | ||
if (indent && indent !== " ") style = indent; | ||
} | ||
function normalizeOptions(code, opts) { | ||
var format = { | ||
@@ -83,7 +72,7 @@ auxiliaryCommentBefore: opts.auxiliaryCommentBefore, | ||
concise: opts.concise, | ||
quotes: findCommonStringDelimiter(code, tokens), | ||
quotes: "double", | ||
jsonCompatibleStrings: opts.jsonCompatibleStrings, | ||
indent: { | ||
adjustMultilineComment: true, | ||
style: style, | ||
style: " ", | ||
base: 0 | ||
@@ -120,36 +109,2 @@ } | ||
function findCommonStringDelimiter(code, tokens) { | ||
var DEFAULT_STRING_DELIMITER = "double"; | ||
if (!code) { | ||
return DEFAULT_STRING_DELIMITER; | ||
} | ||
var occurences = { | ||
single: 0, | ||
double: 0 | ||
}; | ||
var checked = 0; | ||
for (var i = 0; i < tokens.length; i++) { | ||
var token = tokens[i]; | ||
if (token.type.label !== "string") continue; | ||
var raw = code.slice(token.start, token.end); | ||
if (raw[0] === "'") { | ||
occurences.single++; | ||
} else { | ||
occurences.double++; | ||
} | ||
checked++; | ||
if (checked >= 3) break; | ||
} | ||
if (occurences.single > occurences.double) { | ||
return "single"; | ||
} else { | ||
return "double"; | ||
} | ||
} | ||
var CodeGenerator = exports.CodeGenerator = function () { | ||
@@ -156,0 +111,0 @@ function CodeGenerator(ast, opts, code) { |
@@ -49,3 +49,2 @@ "use strict"; | ||
var aliases = t.FLIPPED_ALIAS_KEYS[type]; | ||
@@ -52,0 +51,0 @@ if (aliases) { |
@@ -71,2 +71,6 @@ "use strict"; | ||
function Binary(node, parent) { | ||
if (node.operator === "**" && t.isBinaryExpression(parent, { operator: "**" })) { | ||
return parent.left === node; | ||
} | ||
if ((t.isCallExpression(parent) || t.isNewExpression(parent)) && parent.callee === node || t.isUnaryLike(parent) || t.isMemberExpression(parent) && parent.object === node || t.isAwaitExpression(parent)) { | ||
@@ -96,3 +100,2 @@ return true; | ||
function SequenceExpression(node, parent) { | ||
if (t.isForStatement(parent) || t.isThrowStatement(parent) || t.isReturnStatement(parent) || t.isIfStatement(parent) && parent.test === node || t.isWhileStatement(parent) && parent.test === node || t.isForInStatement(parent) && parent.right === node || t.isSwitchStatement(parent) && parent.discriminant === node || t.isExpressionStatement(parent) && parent.expression === node) { | ||
@@ -115,3 +118,3 @@ return false; | ||
function UnaryLike(node, parent) { | ||
return t.isMemberExpression(parent, { object: node }) || t.isCallExpression(parent, { callee: node }) || t.isNewExpression(parent, { callee: node }); | ||
return t.isMemberExpression(parent, { object: node }) || t.isCallExpression(parent, { callee: node }) || t.isNewExpression(parent, { callee: node }) || t.isBinaryExpression(parent, { operator: "**", left: node }); | ||
} | ||
@@ -124,11 +127,7 @@ | ||
function ArrowFunctionExpression(node, parent) { | ||
if (t.isExportDeclaration(parent) || t.isBinaryExpression(parent) || t.isLogicalExpression(parent) || t.isUnaryExpression(parent) || t.isTaggedTemplateExpression(parent)) { | ||
return true; | ||
} | ||
return UnaryLike(node, parent); | ||
return t.isExportDeclaration(parent) || ConditionalExpression(node, parent); | ||
} | ||
function ConditionalExpression(node, parent) { | ||
if (t.isUnaryLike(parent) || t.isBinary(parent) || t.isConditionalExpression(parent, { test: node }) || t.isAwaitExpression(parent)) { | ||
if (t.isUnaryLike(parent) || t.isBinary(parent) || t.isConditionalExpression(parent, { test: node }) || t.isAwaitExpression(parent) || t.isTaggedTemplateExpression(parent)) { | ||
return true; | ||
@@ -135,0 +134,0 @@ } |
@@ -69,3 +69,4 @@ "use strict"; | ||
return { | ||
before: node.consequent.length || parent.cases[0] === node | ||
before: node.consequent.length || parent.cases[0] === node, | ||
after: !node.consequent.length && parent.cases[parent.cases.length - 1] === node | ||
}; | ||
@@ -131,2 +132,18 @@ }, | ||
nodes.ObjectTypeCallProperty = function (node, parent) { | ||
if (parent.callProperties[0] === node && (!parent.properties || !parent.properties.length)) { | ||
return { | ||
before: true | ||
}; | ||
} | ||
}; | ||
nodes.ObjectTypeIndexer = function (node, parent) { | ||
if (parent.indexers[0] === node && (!parent.properties || !parent.properties.length) && (!parent.callProperties || !parent.callProperties.length)) { | ||
return { | ||
before: true | ||
}; | ||
} | ||
}; | ||
var list = exports.list = { | ||
@@ -133,0 +150,0 @@ VariableDeclaration: function VariableDeclaration(node) { |
@@ -6,10 +6,2 @@ "use strict"; | ||
var _find = require("lodash/find"); | ||
var _find2 = _interopRequireDefault(_find); | ||
var _findLast = require("lodash/findLast"); | ||
var _findLast2 = _interopRequireDefault(_findLast); | ||
var _isInteger = require("lodash/isInteger"); | ||
@@ -31,6 +23,2 @@ | ||
var _whitespace = require("./whitespace"); | ||
var _whitespace2 = _interopRequireDefault(_whitespace); | ||
var _babelTypes = require("babel-types"); | ||
@@ -55,3 +43,3 @@ | ||
var Printer = function () { | ||
function Printer(format, map, tokens) { | ||
function Printer(format, map) { | ||
_classCallCheck(this, Printer); | ||
@@ -72,3 +60,2 @@ | ||
this._buf = new _buffer2.default(map); | ||
this._whitespace = tokens.length > 0 ? new _whitespace2.default(tokens) : null; | ||
} | ||
@@ -426,4 +413,2 @@ | ||
Printer.prototype._printNewline = function _printNewline(leading, node, parent, opts) { | ||
var _this2 = this; | ||
if (this.format.retainLines || this.format.compact) return; | ||
@@ -438,27 +423,8 @@ | ||
if (node.start != null && !node._ignoreUserWhitespace && this._whitespace) { | ||
if (leading) { | ||
var _comments = node.leadingComments; | ||
var _comment = _comments && (0, _find2.default)(_comments, function (comment) { | ||
return !!comment.loc && _this2.format.shouldPrintComment(comment.value); | ||
}); | ||
lines = this._whitespace.getNewlinesBefore(_comment || node); | ||
} else { | ||
var _comments2 = node.trailingComments; | ||
var _comment2 = _comments2 && (0, _findLast2.default)(_comments2, function (comment) { | ||
return !!comment.loc && _this2.format.shouldPrintComment(comment.value); | ||
}); | ||
lines = this._whitespace.getNewlinesAfter(_comment2 || node); | ||
} | ||
} else { | ||
if (this._buf.hasContent()) { | ||
if (!leading) lines++; | ||
if (opts.addNewlines) lines += opts.addNewlines(leading, node) || 0; | ||
var needs = n.needsWhitespaceAfter; | ||
if (leading) needs = n.needsWhitespaceBefore; | ||
var needs = leading ? n.needsWhitespaceBefore : n.needsWhitespaceAfter; | ||
if (needs(node, parent)) lines++; | ||
if (!this._buf.hasContent()) lines = 0; | ||
} | ||
@@ -474,3 +440,3 @@ | ||
Printer.prototype._printComment = function _printComment(comment) { | ||
var _this3 = this; | ||
var _this2 = this; | ||
@@ -489,9 +455,11 @@ if (!this.format.shouldPrintComment(comment.value)) return; | ||
this.newline(this._whitespace ? this._whitespace.getNewlinesBefore(comment) : 0); | ||
var isBlockComment = comment.type === "CommentBlock"; | ||
this.newline(this._buf.hasContent() && isBlockComment ? 1 : 0); | ||
if (!this.endsWith("[") && !this.endsWith("{")) this.space(); | ||
var val = comment.type === "CommentLine" ? "//" + comment.value + "\n" : "/*" + comment.value + "*/"; | ||
var val = !isBlockComment ? "//" + comment.value + "\n" : "/*" + comment.value + "*/"; | ||
if (comment.type === "CommentBlock" && this.format.indent.adjustMultilineComment) { | ||
if (isBlockComment && this.format.indent.adjustMultilineComment) { | ||
var offset = comment.loc && comment.loc.start.column; | ||
@@ -510,6 +478,6 @@ if (offset) { | ||
this.withSource("start", comment.loc, function () { | ||
_this3._append(val); | ||
_this2._append(val); | ||
}); | ||
this.newline((this._whitespace ? this._whitespace.getNewlinesAfter(comment) : 0) + (comment.type === "CommentLine" ? -1 : 0)); | ||
this.newline(isBlockComment ? 1 : 0); | ||
}; | ||
@@ -532,5 +500,5 @@ | ||
var _comment3 = _ref; | ||
var _comment = _ref; | ||
this._printComment(_comment3); | ||
this._printComment(_comment); | ||
} | ||
@@ -537,0 +505,0 @@ }; |
{ | ||
"name": "babel-generator", | ||
"version": "7.0.0-alpha.12", | ||
"version": "7.0.0-alpha.14", | ||
"description": "Turns an AST into code.", | ||
@@ -14,5 +14,4 @@ "author": "Sebastian McKenzie <sebmck@gmail.com>", | ||
"dependencies": { | ||
"babel-messages": "7.0.0-alpha.12", | ||
"babel-types": "7.0.0-alpha.12", | ||
"detect-indent": "^4.0.0", | ||
"babel-messages": "7.0.0-alpha.14", | ||
"babel-types": "7.0.0-alpha.14", | ||
"jsesc": "^1.3.0", | ||
@@ -24,5 +23,5 @@ "lodash": "^4.2.0", | ||
"devDependencies": { | ||
"babel-helper-fixtures": "7.0.0-alpha.12", | ||
"babylon": "^7.0.0-beta.12" | ||
"babel-helper-fixtures": "7.0.0-alpha.14", | ||
"babylon": "^7.0.0-beta.15" | ||
} | ||
} |
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
6
90521
20
2662
+ Addedbabel-messages@7.0.0-alpha.14(transitive)
+ Addedbabel-types@7.0.0-alpha.14(transitive)
- Removeddetect-indent@^4.0.0
- Removedbabel-messages@7.0.0-alpha.12(transitive)
- Removedbabel-types@7.0.0-alpha.12(transitive)
- Removeddetect-indent@4.0.0(transitive)
- Removedis-finite@1.1.0(transitive)
- Removedrepeating@2.0.1(transitive)
Updatedbabel-types@7.0.0-alpha.14