Socket
Socket
Sign inDemoInstall

@babel/generator

Package Overview
Dependencies
10
Maintainers
4
Versions
165
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 7.23.0 to 7.23.3

26

lib/buffer.js

@@ -8,3 +8,3 @@ "use strict";

class Buffer {
constructor(map) {
constructor(map, indentChar) {
this._map = null;

@@ -18,2 +18,4 @@ this._buf = "";

this._canMarkIdName = true;
this._indentChar = "";
this._fastIndentations = [];
this._position = {

@@ -31,2 +33,6 @@ line: 1,

this._map = map;
this._indentChar = indentChar;
for (let i = 0; i < 64; i++) {
this._fastIndentations.push(indentChar.repeat(i));
}
this._allocQueue();

@@ -122,4 +128,5 @@ }

}
queueIndentation(char, repeat) {
this._pushQueue(char, repeat, undefined, undefined, undefined);
queueIndentation(repeat) {
if (repeat === 0) return;
this._pushQueue(-1, repeat, undefined, undefined, undefined);
}

@@ -137,3 +144,12 @@ _flush() {

this._last = char;
this._str += repeat > 1 ? String.fromCharCode(char).repeat(repeat) : String.fromCharCode(char);
if (char === -1) {
const fastIndentation = this._fastIndentations[repeat];
if (fastIndentation !== undefined) {
this._str += fastIndentation;
} else {
this._str += repeat > 1 ? this._indentChar.repeat(repeat) : this._indentChar;
}
} else {
this._str += repeat > 1 ? String.fromCharCode(char).repeat(repeat) : String.fromCharCode(char);
}
if (char !== 10) {

@@ -196,3 +212,3 @@ this._mark(sourcePos.line, sourcePos.column, sourcePos.identifierName, sourcePos.identifierNamePos, sourcePos.filename);

var _this$_map;
(_this$_map = this._map) == null ? void 0 : _this$_map.mark(this._position, line, column, identifierName, identifierNamePos, filename);
(_this$_map = this._map) == null || _this$_map.mark(this._position, line, column, identifierName, identifierNamePos, filename);
}

@@ -199,0 +215,0 @@ removeTrailingNewline() {

6

lib/generators/statements.js

@@ -120,6 +120,4 @@ "use strict";

}
const ForInStatement = ForXStatement;
exports.ForInStatement = ForInStatement;
const ForOfStatement = ForXStatement;
exports.ForOfStatement = ForOfStatement;
const ForInStatement = exports.ForInStatement = ForXStatement;
const ForOfStatement = exports.ForOfStatement = ForXStatement;
function DoWhileStatement(node) {

@@ -126,0 +124,0 @@ this.word("do");

@@ -161,11 +161,12 @@ "use strict";

const opts = this.format.jsescOption;
const value = node.value + "";
const value = node.value;
const str = value + "";
if (opts.numbers) {
this.number(_jsesc(node.value, opts));
this.number(_jsesc(value, opts), value);
} else if (raw == null) {
this.number(value);
this.number(str, value);
} else if (this.format.minified) {
this.number(raw.length < value.length ? raw : value);
this.number(raw.length < str.length ? raw : str, value);
} else {
this.number(raw);
this.number(raw, value);
}

@@ -172,0 +173,0 @@ }

@@ -21,9 +21,9 @@ "use strict";

function expandAliases(obj) {
const newObj = {};
const map = new Map();
function add(type, func) {
const fn = newObj[type];
newObj[type] = fn ? function (node, parent, stack) {
const result = fn(node, parent, stack);
return result == null ? func(node, parent, stack) : result;
} : func;
const fn = map.get(type);
map.set(type, fn ? function (node, parent, stack) {
var _fn;
return (_fn = fn(node, parent, stack)) != null ? _fn : func(node, parent, stack);
} : func);
}

@@ -40,10 +40,6 @@ for (const type of Object.keys(obj)) {

}
return newObj;
return map;
}
const expandedParens = expandAliases(parens);
const expandedWhitespaceNodes = expandAliases(whitespace.nodes);
function find(obj, node, parent, printStack) {
const fn = obj[node.type];
return fn ? fn(node, parent, printStack) : null;
}
function isOrHasCallExpression(node) {

@@ -56,2 +52,3 @@ if (isCallExpression(node)) {

function needsWhitespace(node, parent, type) {
var _expandedWhitespaceNo;
if (!node) return false;

@@ -61,3 +58,3 @@ if (isExpressionStatement(node)) {

}
const flag = find(expandedWhitespaceNodes, node, parent);
const flag = (_expandedWhitespaceNo = expandedWhitespaceNodes.get(node.type)) == null ? void 0 : _expandedWhitespaceNo(node, parent);
if (typeof flag === "number") {

@@ -75,2 +72,3 @@ return (flag & type) !== 0;

function needsParens(node, parent, printStack) {
var _expandedParens$get;
if (!parent) return false;

@@ -80,5 +78,5 @@ if (isNewExpression(parent) && parent.callee === node) {

}
return find(expandedParens, node, parent, printStack);
return (_expandedParens$get = expandedParens.get(node.type)) == null ? void 0 : _expandedParens$get(node, parent, printStack);
}
//# sourceMappingURL=index.js.map

@@ -34,87 +34,24 @@ "use strict";

isArrowFunctionExpression,
isAssignmentExpression,
isAwaitExpression,
isBinary,
isBinaryExpression,
isUpdateExpression,
isCallExpression,
isClass,
isClassExpression,
isConditional,
isConditionalExpression,
isExportDeclaration,
isExportDefaultDeclaration,
isExpressionStatement,
isFor,
isForInStatement,
isForOfStatement,
isForStatement,
isFunctionExpression,
isIfStatement,
isIndexedAccessType,
isIntersectionTypeAnnotation,
isLogicalExpression,
isMemberExpression,
isNewExpression,
isNullableTypeAnnotation,
isObjectPattern,
isOptionalCallExpression,
isOptionalMemberExpression,
isReturnStatement,
isSequenceExpression,
isSwitchStatement,
isTSArrayType,
isTSAsExpression,
isTSInstantiationExpression,
isTSIntersectionType,
isTSNonNullExpression,
isTSOptionalType,
isTSRestType,
isTSTypeAssertion,
isTSUnionType,
isTaggedTemplateExpression,
isThrowStatement,
isTypeAnnotation,
isUnaryLike,
isUnionTypeAnnotation,
isVariableDeclarator,
isWhileStatement,
isYieldExpression,
isTSSatisfiesExpression
isYieldExpression
} = _t;
const PRECEDENCE = {
"||": 0,
"??": 0,
"|>": 0,
"&&": 1,
"|": 2,
"^": 3,
"&": 4,
"==": 5,
"===": 5,
"!=": 5,
"!==": 5,
"<": 6,
">": 6,
"<=": 6,
">=": 6,
in: 6,
instanceof: 6,
">>": 7,
"<<": 7,
">>>": 7,
"+": 8,
"-": 8,
"*": 9,
"/": 9,
"%": 9,
"**": 10
const PRECEDENCE = new Map([["||", 0], ["??", 0], ["|>", 0], ["&&", 1], ["|", 2], ["^", 3], ["&", 4], ["==", 5], ["===", 5], ["!=", 5], ["!==", 5], ["<", 6], [">", 6], ["<=", 6], [">=", 6], ["in", 6], ["instanceof", 6], [">>", 7], ["<<", 7], [">>>", 7], ["+", 8], ["-", 8], ["*", 9], ["/", 9], ["%", 9], ["**", 10]]);
function isTSTypeExpression(nodeType) {
return nodeType === "TSAsExpression" || nodeType === "TSSatisfiesExpression" || nodeType === "TSTypeAssertion";
}
const isClassExtendsClause = (node, parent) => {
const parentType = parent.type;
return (parentType === "ClassDeclaration" || parentType === "ClassExpression") && parent.superClass === node;
};
function isTSTypeExpression(node) {
return isTSAsExpression(node) || isTSSatisfiesExpression(node) || isTSTypeAssertion(node);
}
const isClassExtendsClause = (node, parent) => isClass(parent, {
superClass: node
});
const hasPostfixPart = (node, parent) => (isMemberExpression(parent) || isOptionalMemberExpression(parent)) && parent.object === node || (isCallExpression(parent) || isOptionalCallExpression(parent) || isNewExpression(parent)) && parent.callee === node || isTaggedTemplateExpression(parent) && parent.tag === node || isTSNonNullExpression(parent);
const hasPostfixPart = (node, parent) => {
const parentType = parent.type;
return (parentType === "MemberExpression" || parentType === "OptionalMemberExpression") && parent.object === node || (parentType === "CallExpression" || parentType === "OptionalCallExpression" || parentType === "NewExpression") && parent.callee === node || parentType === "TaggedTemplateExpression" && parent.tag === node || parentType === "TSNonNullExpression";
};
function NullableTypeAnnotation(node, parent) {

@@ -125,3 +62,4 @@ return isArrayTypeAnnotation(parent);

if (printStack.length < 3) return;
return isUnionTypeAnnotation(parent) || isIntersectionTypeAnnotation(parent) || isArrayTypeAnnotation(parent) || isTypeAnnotation(parent) && isArrowFunctionExpression(printStack[printStack.length - 3]);
const parentType = parent.type;
return parentType === "UnionTypeAnnotation" || parentType === "IntersectionTypeAnnotation" || parentType === "ArrayTypeAnnotation" || parentType === "TypeAnnotation" && isArrowFunctionExpression(printStack[printStack.length - 3]);
}

@@ -138,5 +76,4 @@ function UpdateExpression(node, parent) {

function Binary(node, parent) {
if (node.operator === "**" && isBinaryExpression(parent, {
operator: "**"
})) {
const parentType = parent.type;
if (node.operator === "**" && parentType === "BinaryExpression" && parent.operator === "**") {
return parent.left === node;

@@ -147,22 +84,20 @@ }

}
if (hasPostfixPart(node, parent) || isUnaryLike(parent) || isAwaitExpression(parent)) {
if (hasPostfixPart(node, parent) || parentType === "UnaryExpression" || parentType === "SpreadElement" || parentType === "AwaitExpression") {
return true;
}
if (isBinary(parent)) {
const parentOp = parent.operator;
const parentPos = PRECEDENCE[parentOp];
const nodeOp = node.operator;
const nodePos = PRECEDENCE[nodeOp];
if (parentPos === nodePos && parent.right === node && !isLogicalExpression(parent) || parentPos > nodePos) {
if (parentType === "BinaryExpression" || parentType === "LogicalExpression") {
const parentPos = PRECEDENCE.get(parent.operator);
const nodePos = PRECEDENCE.get(node.operator);
if (parentPos === nodePos && parent.right === node && parentType !== "LogicalExpression" || parentPos > nodePos) {
return true;
}
}
return undefined;
}
function UnionTypeAnnotation(node, parent) {
return isArrayTypeAnnotation(parent) || isNullableTypeAnnotation(parent) || isIntersectionTypeAnnotation(parent) || isUnionTypeAnnotation(parent);
const parentType = parent.type;
return parentType === "ArrayTypeAnnotation" || parentType === "NullableTypeAnnotation" || parentType === "IntersectionTypeAnnotation" || parentType === "UnionTypeAnnotation";
}
function OptionalIndexedAccessType(node, parent) {
return isIndexedAccessType(parent, {
objectType: node
});
return isIndexedAccessType(parent) && parent.objectType === node;
}

@@ -173,15 +108,23 @@ function TSAsExpression() {

function TSUnionType(node, parent) {
return isTSArrayType(parent) || isTSOptionalType(parent) || isTSIntersectionType(parent) || isTSUnionType(parent) || isTSRestType(parent);
const parentType = parent.type;
return parentType === "TSArrayType" || parentType === "TSOptionalType" || parentType === "TSIntersectionType" || parentType === "TSUnionType" || parentType === "TSRestType";
}
function TSInferType(node, parent) {
return isTSArrayType(parent) || isTSOptionalType(parent);
const parentType = parent.type;
return parentType === "TSArrayType" || parentType === "TSOptionalType";
}
function TSInstantiationExpression(node, parent) {
return (isCallExpression(parent) || isOptionalCallExpression(parent) || isNewExpression(parent) || isTSInstantiationExpression(parent)) && !!parent.typeParameters;
const parentType = parent.type;
return (parentType === "CallExpression" || parentType === "OptionalCallExpression" || parentType === "NewExpression" || parentType === "TSInstantiationExpression") && !!parent.typeParameters;
}
function BinaryExpression(node, parent) {
return node.operator === "in" && (isVariableDeclarator(parent) || isFor(parent));
if (node.operator === "in") {
const parentType = parent.type;
return parentType === "VariableDeclarator" || parentType === "ForStatement" || parentType === "ForInStatement" || parentType === "ForOfStatement";
}
return false;
}
function SequenceExpression(node, parent) {
if (isForStatement(parent) || isThrowStatement(parent) || isReturnStatement(parent) || isIfStatement(parent) && parent.test === node || isWhileStatement(parent) && parent.test === node || isForInStatement(parent) && parent.right === node || isSwitchStatement(parent) && parent.discriminant === node || isExpressionStatement(parent) && parent.expression === node) {
const parentType = parent.type;
if (parentType === "ForStatement" || parentType === "ThrowStatement" || parentType === "ReturnStatement" || parentType === "IfStatement" && parent.test === node || parentType === "WhileStatement" && parent.test === node || parentType === "ForInStatement" && parent.right === node || parentType === "SwitchStatement" && parent.discriminant === node || parentType === "ExpressionStatement" && parent.expression === node) {
return false;

@@ -192,3 +135,4 @@ }

function YieldExpression(node, parent) {
return isBinary(parent) || isUnaryLike(parent) || hasPostfixPart(node, parent) || isAwaitExpression(parent) && isYieldExpression(node) || isConditionalExpression(parent) && node === parent.test || isClassExtendsClause(node, parent);
const parentType = parent.type;
return parentType === "BinaryExpression" || parentType === "LogicalExpression" || parentType === "UnaryExpression" || parentType === "SpreadElement" || hasPostfixPart(node, parent) || parentType === "AwaitExpression" && isYieldExpression(node) || parentType === "ConditionalExpression" && node === parent.test || isClassExtendsClause(node, parent);
}

@@ -199,6 +143,3 @@ function ClassExpression(node, parent, printStack) {

function UnaryLike(node, parent) {
return hasPostfixPart(node, parent) || isBinaryExpression(parent, {
operator: "**",
left: node
}) || isClassExtendsClause(node, parent);
return hasPostfixPart(node, parent) || isBinaryExpression(parent) && parent.operator === "**" && parent.left === node || isClassExtendsClause(node, parent);
}

@@ -212,5 +153,4 @@ function FunctionExpression(node, parent, printStack) {

function ConditionalExpression(node, parent) {
if (isUnaryLike(parent) || isBinary(parent) || isConditionalExpression(parent, {
test: node
}) || isAwaitExpression(parent) || isTSTypeExpression(parent)) {
const parentType = parent.type;
if (parentType === "UnaryExpression" || parentType === "SpreadElement" || parentType === "BinaryExpression" || parentType === "LogicalExpression" || parentType === "ConditionalExpression" && parent.test === node || parentType === "AwaitExpression" || isTSTypeExpression(parentType)) {
return true;

@@ -221,7 +161,3 @@ }

function OptionalMemberExpression(node, parent) {
return isCallExpression(parent, {
callee: node
}) || isMemberExpression(parent, {
object: node
});
return isCallExpression(parent) && parent.callee === node || isMemberExpression(parent) && parent.object === node;
}

@@ -236,13 +172,12 @@ function AssignmentExpression(node, parent) {

function LogicalExpression(node, parent) {
if (isTSTypeExpression(parent)) return true;
const parentType = parent.type;
if (isTSTypeExpression(parentType)) return true;
if (parentType !== "LogicalExpression") return false;
switch (node.operator) {
case "||":
if (!isLogicalExpression(parent)) return false;
return parent.operator === "??" || parent.operator === "&&";
case "&&":
return isLogicalExpression(parent, {
operator: "??"
});
return parent.operator === "??";
case "??":
return isLogicalExpression(parent) && parent.operator !== "??";
return parent.operator !== "??";
}

@@ -252,6 +187,8 @@ }

var _node$extra;
if ((_node$extra = node.extra) != null && _node$extra.parenthesized && isAssignmentExpression(parent, {
left: node
}) && (isFunctionExpression(parent.right) || isClassExpression(parent.right)) && parent.right.id == null) {
return true;
const parentType = parent.type;
if ((_node$extra = node.extra) != null && _node$extra.parenthesized && parentType === "AssignmentExpression" && parent.left === node) {
const rightType = parent.right.type;
if ((rightType === "FunctionExpression" || rightType === "ClassExpression") && parent.right.id == null) {
return true;
}
}

@@ -284,24 +221,7 @@ if (node.name === "let") {

while (i >= 0) {
if (expressionStatement && isExpressionStatement(parent, {
expression: node
}) || exportDefault && isExportDefaultDeclaration(parent, {
declaration: node
}) || arrowBody && isArrowFunctionExpression(parent, {
body: node
}) || forHead && isForStatement(parent, {
init: node
}) || forInHead && isForInStatement(parent, {
left: node
}) || forOfHead && isForOfStatement(parent, {
left: node
})) {
const parentType = parent.type;
if (expressionStatement && parentType === "ExpressionStatement" && parent.expression === node || exportDefault && parentType === "ExportDefaultDeclaration" && node === parent.declaration || arrowBody && parentType === "ArrowFunctionExpression" && parent.body === node || forHead && parentType === "ForStatement" && parent.init === node || forInHead && parentType === "ForInStatement" && parent.left === node || forOfHead && parentType === "ForOfStatement" && parent.left === node) {
return true;
}
if (i > 0 && (hasPostfixPart(node, parent) && !isNewExpression(parent) || isSequenceExpression(parent) && parent.expressions[0] === node || isUpdateExpression(parent) && !parent.prefix || isConditional(parent, {
test: node
}) || isBinary(parent, {
left: node
}) || isAssignmentExpression(parent, {
left: node
}))) {
if (i > 0 && (hasPostfixPart(node, parent) && parentType !== "NewExpression" || parentType === "SequenceExpression" && parent.expressions[0] === node || parentType === "UpdateExpression" && !parent.prefix || parentType === "ConditionalExpression" && parent.test === node || (parentType === "BinaryExpression" || parentType === "LogicalExpression") && parent.left === node || parentType === "AssignmentExpression" && parent.left === node)) {
node = parent;

@@ -308,0 +228,0 @@ i--;

@@ -66,3 +66,3 @@ "use strict";

}
const nodes = {
const nodes = exports.nodes = {
AssignmentExpression(node) {

@@ -116,3 +116,2 @@ const state = crawl(node.right);

};
exports.nodes = nodes;
nodes.ObjectProperty = nodes.ObjectTypeProperty = nodes.ObjectMethod = function (node, parent) {

@@ -119,0 +118,0 @@ if (parent.properties[0] === node) {

@@ -20,6 +20,5 @@ "use strict";

const ZERO_DECIMAL_INTEGER = /\.0+$/;
const NON_DECIMAL_LITERAL = /^0[box]/;
const PURE_ANNOTATION_RE = /^\s*[@#]__PURE__\s*$/;
const HAS_NEWLINE = /[\n\r\u2028\u2029]/;
const HAS_BlOCK_COMMENT_END = /\*\//;
const HAS_NEWLINE_OR_BlOCK_COMMENT_END = /[\n\r\u2028\u2029]|\*\//;
const {

@@ -33,3 +32,2 @@ needsParens

this._indent = 0;
this._indentChar = 0;
this._indentRepeat = 0;

@@ -47,6 +45,5 @@ this._insideAux = false;

this.format = format;
this._buf = new _buffer.default(map);
this._indentChar = format.indent.style.charCodeAt(0);
this._indentRepeat = format.indent.style.length;
this._inputMap = map == null ? void 0 : map._inputMap;
this._buf = new _buffer.default(map, format.indent.style[0]);
}

@@ -107,5 +104,12 @@ generate(ast) {

}
number(str) {
number(str, number) {
function isNonDecimalLiteral(str) {
if (str.length > 2 && str.charCodeAt(0) === 48) {
const secondChar = str.charCodeAt(1);
return secondChar === 98 || secondChar === 111 || secondChar === 120;
}
return false;
}
this.word(str);
this._endsWithInteger = Number.isInteger(+str) && !NON_DECIMAL_LITERAL.test(str) && !SCIENTIFIC_NOTATION.test(str) && !ZERO_DECIMAL_INTEGER.test(str) && str.charCodeAt(str.length - 1) !== 46;
this._endsWithInteger = Number.isInteger(number) && !isNonDecimalLiteral(str) && !SCIENTIFIC_NOTATION.test(str) && !ZERO_DECIMAL_INTEGER.test(str) && str.charCodeAt(str.length - 1) !== 46;
}

@@ -222,3 +226,3 @@ token(str, maybeNewline = false) {

if (this._indent && firstChar !== 10 && this.endsWith(10)) {
this._buf.queueIndentation(this._indentChar, this._getIndent());
this._buf.queueIndentation(this._getIndent());
}

@@ -410,4 +414,4 @@ }

this.print(node, parent, undefined, opts.trailingCommentsLineOffset || 0);
opts.iterator == null ? void 0 : opts.iterator(node, i);
if (i < len - 1) separator == null ? void 0 : separator();
opts.iterator == null || opts.iterator(node, i);
if (i < len - 1) separator == null || separator();
if (opts.statement) {

@@ -516,3 +520,3 @@ if (i + 1 === len) {

if (this._printedComments.has(comment)) return 0;
if (this._noLineTerminator && (HAS_NEWLINE.test(comment.value) || HAS_BlOCK_COMMENT_END.test(comment.value))) {
if (this._noLineTerminator && HAS_NEWLINE_OR_BlOCK_COMMENT_END.test(comment.value)) {
return 2;

@@ -547,7 +551,11 @@ }

}
let indentSize = this.format.retainLines ? 0 : this._buf.getCurrentColumn();
if (this._shouldIndent(47) || this.format.retainLines) {
indentSize += this._getIndent();
if (this.format.concise) {
val = val.replace(/\n(?!$)/g, `\n`);
} else {
let indentSize = this.format.retainLines ? 0 : this._buf.getCurrentColumn();
if (this._shouldIndent(47) || this.format.retainLines) {
indentSize += this._getIndent();
}
val = val.replace(/\n(?!$)/g, `\n${" ".repeat(indentSize)}`);
}
val = val.replace(/\n(?!$)/g, `\n${" ".repeat(indentSize)}`);
}

@@ -652,4 +660,3 @@ } else if (!noLineTerminator) {

}
var _default = Printer;
exports.default = _default;
var _default = exports.default = Printer;
function commaSeparator() {

@@ -656,0 +663,0 @@ this.tokenChar(44);

{
"name": "@babel/generator",
"version": "7.23.0",
"version": "7.23.3",
"description": "Turns an AST into code.",

@@ -22,3 +22,3 @@ "author": "The Babel Team (https://babel.dev/team)",

"dependencies": {
"@babel/types": "^7.23.0",
"@babel/types": "^7.23.3",
"@jridgewell/gen-mapping": "^0.3.2",

@@ -30,3 +30,3 @@ "@jridgewell/trace-mapping": "^0.3.17",

"@babel/helper-fixtures": "^7.22.19",
"@babel/parser": "^7.23.0",
"@babel/parser": "^7.23.3",
"@jridgewell/sourcemap-codec": "^1.4.15",

@@ -33,0 +33,0 @@ "@types/jsesc": "^2.5.0",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc