Socket
Socket
Sign inDemoInstall

@babel/generator

Package Overview
Dependencies
10
Maintainers
4
Versions
170
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 7.19.3 to 7.19.4

98

lib/buffer.js

@@ -8,11 +8,2 @@ "use strict";

function SourcePos() {
return {
identifierName: undefined,
line: undefined,
column: undefined,
filename: undefined
};
}
class Buffer {

@@ -31,9 +22,7 @@ constructor(map) {

};
this._sourcePosition = SourcePos();
this._disallowedPop = {
this._sourcePosition = {
identifierName: undefined,
line: undefined,
column: undefined,
filename: undefined,
objectReusable: true
filename: undefined
};

@@ -186,2 +175,3 @@ this._map = map;

const len = str.length;
const position = this._position;
this._last = str.charCodeAt(len - 1);

@@ -199,3 +189,3 @@

if (!maybeNewline && !this._map) {
this._position.column += len;
position.column += len;
return;

@@ -218,7 +208,7 @@ }

while (i !== -1) {
this._position.line++;
this._position.column = 0;
position.line++;
position.column = 0;
last = i + 1;
if (last < str.length) {
if (last < len) {
this._mark(++line, 0, identifierName, filename);

@@ -230,3 +220,3 @@ }

this._position.column += str.length - last;
position.column += len - last;
}

@@ -261,2 +251,18 @@

getNewlineCount() {
const queueCursor = this._queueCursor;
let count = 0;
if (queueCursor === 0) return this._last === 10 ? 1 : 0;
for (let i = queueCursor - 1; i >= 0; i--) {
if (this._queue[i].char !== 10) {
break;
}
count++;
}
return count === queueCursor && this._last === 10 ? count + 1 : count;
}
endsWithCharAndNewline() {

@@ -287,51 +293,31 @@ const queue = this._queue;

this.source("end", loc);
this._disallowPop("start", loc);
}
source(prop, loc) {
if (!loc) return;
if (!this._map) return;
this._normalizePosition(prop, loc, this._sourcePosition);
this._normalizePosition(prop, loc, 0, 0);
}
sourceWithOffset(prop, loc, lineOffset, columnOffset) {
if (!this._map) return;
this._normalizePosition(prop, loc, lineOffset, columnOffset);
}
withSource(prop, loc, cb) {
if (!this._map) return cb();
const originalLine = this._sourcePosition.line;
const originalColumn = this._sourcePosition.column;
const originalFilename = this._sourcePosition.filename;
const originalIdentifierName = this._sourcePosition.identifierName;
this.source(prop, loc);
cb();
if (this._disallowedPop.objectReusable || this._disallowedPop.line !== originalLine || this._disallowedPop.column !== originalColumn || this._disallowedPop.filename !== originalFilename) {
this._sourcePosition.line = originalLine;
this._sourcePosition.column = originalColumn;
this._sourcePosition.filename = originalFilename;
this._sourcePosition.identifierName = originalIdentifierName;
this._disallowedPop.objectReusable = true;
}
}
_disallowPop(prop, loc) {
if (!loc) return;
const disallowedPop = this._disallowedPop;
this._normalizePosition(prop, loc, disallowedPop);
disallowedPop.objectReusable = false;
}
_normalizePosition(prop, loc, targetObj) {
_normalizePosition(prop, loc, lineOffset, columnOffset) {
const pos = loc[prop];
targetObj.identifierName = prop === "start" && loc.identifierName || undefined;
const target = this._sourcePosition;
target.identifierName = prop === "start" && loc.identifierName || undefined;
if (pos) {
targetObj.line = pos.line;
targetObj.column = pos.column;
targetObj.filename = loc.filename;
} else {
targetObj.line = null;
targetObj.column = null;
targetObj.filename = null;
target.line = pos.line + lineOffset;
target.column = pos.column + columnOffset;
target.filename = loc.filename;
}

@@ -342,12 +328,14 @@ }

const queue = this._queue;
const queueCursor = this._queueCursor;
let lastIndex = -1;
let len = 0;
for (let i = 0; i < this._queueCursor; i++) {
for (let i = 0; i < queueCursor; i++) {
const item = queue[i];
if (item.char === 10) {
lastIndex = i;
len += item.repeat;
lastIndex = len;
}
len += item.repeat;
}

@@ -354,0 +342,0 @@

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

function Program(node) {
var _node$directives;
this.printInnerComments(node, false);
this.printSequence(node.directives, node);
if (node.directives && node.directives.length) this.newline();
const directivesLen = (_node$directives = node.directives) == null ? void 0 : _node$directives.length;
if (directivesLen) {
var _node$directives$trai;
const newline = node.body.length ? 2 : 1;
this.printSequence(node.directives, node, {
trailingCommentsLineOffset: newline
});
if (!((_node$directives$trai = node.directives[directivesLen - 1].trailingComments) != null && _node$directives$trai.length)) {
this.newline(newline);
}
}
this.printSequence(node.body, node);

@@ -31,25 +46,27 @@ }

function BlockStatement(node) {
var _node$directives;
var _node$directives2;
this.tokenChar(123);
this.printInnerComments(node);
const hasDirectives = (_node$directives = node.directives) == null ? void 0 : _node$directives.length;
const directivesLen = (_node$directives2 = node.directives) == null ? void 0 : _node$directives2.length;
if (node.body.length || hasDirectives) {
this.newline();
if (directivesLen) {
var _node$directives$trai2;
const newline = node.body.length ? 2 : 1;
this.printSequence(node.directives, node, {
indent: true
indent: true,
trailingCommentsLineOffset: newline
});
if (hasDirectives) this.newline();
this.printSequence(node.body, node, {
indent: true
});
this.removeTrailingNewline();
this.source("end", node.loc);
if (!this.endsWith(10)) this.newline();
this.rightBrace();
} else {
this.source("end", node.loc);
this.tokenChar(125);
if (!((_node$directives$trai2 = node.directives[directivesLen - 1].trailingComments) != null && _node$directives$trai2.length)) {
this.newline(newline);
}
}
this.printSequence(node.body, node, {
indent: true
});
this.sourceWithOffset("end", node.loc, 0, -1);
this.rightBrace();
}

@@ -87,3 +104,4 @@

function InterpreterDirective(node) {
this.token(`#!${node.value}\n`, true);
this.token(`#!${node.value}`);
this.newline(1, true);
}

@@ -90,0 +108,0 @@

@@ -81,2 +81,3 @@ "use strict";

if (!this.endsWith(10)) this.newline();
this.sourceWithOffset("end", node.loc, 0, -1);
this.rightBrace();

@@ -87,4 +88,7 @@ }

function ClassProperty(node) {
var _node$key$loc, _node$key$loc$end;
this.printJoin(node.decorators, node);
this.source("end", node.key.loc);
const endLine = (_node$key$loc = node.key.loc) == null ? void 0 : (_node$key$loc$end = _node$key$loc.end) == null ? void 0 : _node$key$loc$end.line;
if (endLine) this.catchUp(endLine);
this.tsPrintClassMemberModifiers(node);

@@ -123,4 +127,7 @@

function ClassAccessorProperty(node) {
var _node$key$loc2, _node$key$loc2$end;
this.printJoin(node.decorators, node);
this.source("end", node.key.loc);
const endLine = (_node$key$loc2 = node.key.loc) == null ? void 0 : (_node$key$loc2$end = _node$key$loc2.end) == null ? void 0 : _node$key$loc2$end.line;
if (endLine) this.catchUp(endLine);
this.tsPrintClassMemberModifiers(node);

@@ -197,4 +204,7 @@ this.word("accessor");

function _classMethodHead(node) {
var _node$key$loc3, _node$key$loc3$end;
this.printJoin(node.decorators, node);
this.source("end", node.key.loc);
const endLine = (_node$key$loc3 = node.key.loc) == null ? void 0 : (_node$key$loc3$end = _node$key$loc3.end) == null ? void 0 : _node$key$loc3$end.line;
if (endLine) this.catchUp(endLine);
this.tsPrintClassMemberModifiers(node);

@@ -217,2 +227,3 @@

});
this.sourceWithOffset("end", node.loc, 0, -1);
this.rightBrace();

@@ -219,0 +230,0 @@ }

@@ -336,16 +336,22 @@ "use strict";

this.word("module");
this.space();
this.ensureNoLineTerminator(() => {
this.printInnerComments(node);
this.space();
});
this.tokenChar(123);
this.indent();
const {
body
} = node;
if (node.body.body.length === 0) {
this.tokenChar(125);
} else {
if (body.body.length || body.directives.length) {
this.newline();
this.printSequence(node.body.body, node, {
indent: true
});
this.rightBrace();
}
this.print(body, node);
this.dedent();
this.sourceWithOffset("end", node.loc, 0, -1);
this.rightBrace();
}
//# sourceMappingURL=expressions.js.map

@@ -614,2 +614,3 @@ "use strict";

if (props.length) {
this.newline();
this.space();

@@ -616,0 +617,0 @@ this.printJoin(props, node, {

@@ -272,20 +272,2 @@ "use strict";

function variableDeclarationIndent() {
this.tokenChar(44);
this.newline();
if (this.endsWith(10)) {
for (let i = 0; i < 4; i++) this.space(true);
}
}
function constDeclarationIndent() {
this.tokenChar(44);
this.newline();
if (this.endsWith(10)) {
for (let i = 0; i < 6; i++) this.space(true);
}
}
function VariableDeclaration(node, parent) {

@@ -309,10 +291,8 @@ if (node.declare) {

let separator;
if (hasInits) {
separator = node.kind === "const" ? constDeclarationIndent : variableDeclarationIndent;
}
this.printList(node.declarations, node, {
separator
separator: hasInits ? function () {
this.tokenChar(44);
this.newline();
} : undefined,
indent: node.declarations.length > 1 ? true : false
});

@@ -319,0 +299,0 @@

@@ -37,5 +37,3 @@ "use strict";

function Identifier(node) {
this.exactSource(node.loc, () => {
this.word(node.name);
});
this.word(node.name);
}

@@ -66,2 +64,3 @@

this.sourceWithOffset("end", node.loc, 0, -1);
this.tokenChar(125);

@@ -68,0 +67,0 @@ }

@@ -391,6 +391,6 @@ "use strict";

printer.dedent();
printer.rightBrace();
} else {
printer.token("}");
}
printer.sourceWithOffset("end", node.loc, 0, -1);
printer.rightBrace();
}

@@ -397,0 +397,0 @@

@@ -19,5 +19,3 @@ "use strict";

const {
needsParens,
needsWhitespaceAfter,
needsWhitespaceBefore
needsParens
} = n;

@@ -39,2 +37,3 @@

this._endsWithWord = false;
this._lastCommentLine = 0;
this.format = format;

@@ -138,18 +137,16 @@ this._buf = new _buffer.default(map);

newline(i = 1) {
if (this.format.retainLines || this.format.compact) return;
newline(i = 1, force) {
if (i <= 0) return;
if (this.format.concise) {
this.space();
return;
}
if (!force) {
if (this.format.retainLines || this.format.compact) return;
const charBeforeNewline = this.endsWithCharAndNewline();
if (charBeforeNewline === 10) return;
if (charBeforeNewline === 123 || charBeforeNewline === 58) {
i--;
if (this.format.concise) {
this.space();
return;
}
}
if (i <= 0) return;
if (i > 2) i = 2;
i -= this._buf.getNewlineCount();

@@ -159,2 +156,4 @@ for (let j = 0; j < i; j++) {

}
return;
}

@@ -179,2 +178,4 @@

exactSource(loc, cb) {
if (!loc) return cb();
this._catchUp("start", loc);

@@ -186,2 +187,4 @@

source(prop, loc) {
if (!loc) return;
this._catchUp(prop, loc);

@@ -192,3 +195,13 @@

sourceWithOffset(prop, loc, lineOffset, columnOffset) {
if (!loc) return;
this._catchUp(prop, loc);
this._buf.sourceWithOffset(prop, loc, lineOffset, columnOffset);
}
withSource(prop, loc, cb) {
if (!loc) return cb();
this._catchUp(prop, loc);

@@ -246,2 +259,8 @@

_shouldIndent(firstChar) {
if (this._indent && firstChar !== 10 && this.endsWith(10)) {
return true;
}
}
_maybeAddParenChar(char) {

@@ -302,2 +321,12 @@ const parenPushNewlineState = this._parenPushNewlineState;

catchUp(line) {
if (!this.format.retainLines) return;
const count = line - this._buf.getCurrentLine();
for (let i = 0; i < count; i++) {
this._newline();
}
}
_catchUp(prop, loc) {

@@ -320,7 +349,16 @@ if (!this.format.retainLines) return;

ensureNoLineTerminator(fn) {
const {
_noLineTerminator
} = this;
this._noLineTerminator = true;
fn();
this._noLineTerminator = _noLineTerminator;
}
printTerminatorless(node, parent, isLabel) {
if (isLabel) {
this._noLineTerminator = true;
this.print(node, parent);
this._noLineTerminator = false;
this.ensureNoLineTerminator(() => {
this.print(node, parent);
});
} else {

@@ -341,3 +379,3 @@ const terminatorState = {

print(node, parent, noLineTerminator) {
print(node, parent, noLineTerminator, trailingCommentsLineOffset) {
if (!node) return;

@@ -365,3 +403,3 @@ const nodeType = node.type;

let shouldPrintParens;
let shouldPrintParens = false;

@@ -375,2 +413,3 @@ if (format.retainFunctionParens && nodeType === "FunctionExpression" && node.extra && node.extra.parenthesized) {

if (shouldPrintParens) this.tokenChar(40);
this._lastCommentLine = 0;

@@ -380,3 +419,3 @@ this._printLeadingComments(node);

const loc = nodeType === "Program" || nodeType === "File" ? null : node.loc;
this.withSource("start", loc, printMethod.bind(this, node, parent));
this.exactSource(loc, printMethod.bind(this, node, parent));

@@ -390,3 +429,3 @@ if (noLineTerminator && !this._noLineTerminator) {

} else {
this._printTrailingComments(node);
this._printTrailingComments(node, trailingCommentsLineOffset);
}

@@ -416,3 +455,3 @@

value: comment
});
}, 0);
}

@@ -430,3 +469,3 @@ }

value: comment
});
}, 0);
}

@@ -447,4 +486,6 @@ }

const newlineOpts = {
addNewlines: opts.addNewlines
addNewlines: opts.addNewlines,
nextNodeStartLine: 0
};
const separator = opts.separator ? opts.separator.bind(this) : null;
const len = nodes.length;

@@ -455,14 +496,19 @@

if (!node) continue;
if (opts.statement) this._printNewline(true, node, parent, newlineOpts);
this.print(node, parent);
if (opts.statement) this._printNewline(i === 0, newlineOpts);
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();
if (opts.iterator) {
opts.iterator(node, i);
}
if (opts.statement) {
if (i + 1 === len) {
this.newline(1);
} else {
var _nextNode$loc;
if (opts.separator && i < len - 1) {
opts.separator.call(this);
const nextNode = nodes[i + 1];
newlineOpts.nextNodeStartLine = ((_nextNode$loc = nextNode.loc) == null ? void 0 : _nextNode$loc.start.line) || 0;
this._printNewline(true, newlineOpts);
}
}
if (opts.statement) this._printNewline(false, node, parent, newlineOpts);
}

@@ -490,8 +536,16 @@

_printTrailingComments(node) {
this._printComments(this._getComments(false, node));
_printTrailingComments(node, lineOffset) {
const comments = this._getComments(false, node);
if (!(comments != null && comments.length)) return;
this._printComments(2, comments, node, lineOffset);
}
_printLeadingComments(node) {
this._printComments(this._getComments(true, node), true);
const comments = this._getComments(true, node);
if (!(comments != null && comments.length)) return;
this._printComments(0, comments, node);
}

@@ -505,3 +559,3 @@

this._printComments(node.innerComments);
this._printComments(1, node.innerComments, node);

@@ -524,3 +578,3 @@ if (indent) this.dedent();

_printNewline(leading, node, parent, opts) {
_printNewline(newLine, opts) {
if (this.format.retainLines || this.format.compact) return;

@@ -533,12 +587,21 @@

let lines = 0;
if (!newLine) {
return;
}
const startLine = opts.nextNodeStartLine;
const lastCommentLine = this._lastCommentLine;
if (startLine > 0 && lastCommentLine > 0) {
const offset = startLine - lastCommentLine;
if (offset >= 0) {
this.newline(offset || 1);
return;
}
}
if (this._buf.hasContent()) {
if (!leading) lines++;
if (opts.addNewlines) lines += opts.addNewlines(leading, node) || 0;
const needs = leading ? needsWhitespaceBefore : needsWhitespaceAfter;
if (needs(node, parent)) lines++;
this.newline(1);
}
this.newline(Math.min(2, lines));
}

@@ -558,4 +621,8 @@

const isBlockComment = comment.type === "CommentBlock";
const printNewLines = isBlockComment && !skipNewLines && !this._noLineTerminator;
if (printNewLines && this._buf.hasContent()) this.newline(1);
const printNewLines = isBlockComment && skipNewLines !== 1 && !this._noLineTerminator;
if (printNewLines && this._buf.hasContent() && skipNewLines !== 2) {
this.newline(1);
}
const lastCharCode = this.getLastChar();

@@ -568,3 +635,2 @@

let val;
let maybeNewline = false;

@@ -584,9 +650,12 @@ if (isBlockComment) {

const indentSize = Math.max(this._getIndent(), this.format.retainLines ? 0 : this._buf.getCurrentColumn());
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)}`);
maybeNewline = true;
}
} else if (!this._noLineTerminator) {
val = `//${comment.value}\n`;
maybeNewline = true;
val = `//${comment.value}`;
} else {

@@ -597,15 +666,86 @@ val = `/*${comment.value}*/`;

if (this.endsWith(47)) this._space();
this.withSource("start", comment.loc, this._append.bind(this, val, maybeNewline));
if (printNewLines) this.newline(1);
this.source("start", comment.loc);
this._append(val, isBlockComment);
if (!isBlockComment && !this._noLineTerminator) {
this.newline(1, true);
}
if (printNewLines && skipNewLines !== 3) {
this.newline(1);
}
}
_printComments(comments, inlinePureAnnotation) {
if (!(comments != null && comments.length)) return;
_printComments(type, comments, node, lineOffset = 0) {
{
const nodeLoc = node.loc;
const len = comments.length;
let hasLoc = !!nodeLoc;
const nodeStartLine = hasLoc ? nodeLoc.start.line : 0;
const nodeEndLine = hasLoc ? nodeLoc.end.line : 0;
let lastLine = 0;
let leadingCommentNewline = 0;
if (inlinePureAnnotation && comments.length === 1 && PURE_ANNOTATION_RE.test(comments[0].value)) {
this._printComment(comments[0], this._buf.hasContent() && !this.endsWith(10));
} else {
for (const comment of comments) {
this._printComment(comment);
for (let i = 0; i < len; i++) {
const comment = comments[i];
if (hasLoc && "loc" in comment && !this._printedComments.has(comment)) {
const commentStartLine = comment.loc.start.line;
const commentEndLine = comment.loc.end.line;
if (type === 0) {
let offset = 0;
if (i === 0) {
if (this._buf.hasContent() && (comment.type === "CommentLine" || commentStartLine != commentEndLine)) {
offset = leadingCommentNewline = 1;
}
} else {
offset = commentStartLine - lastLine;
}
lastLine = commentEndLine;
this.newline(offset);
this._printComment(comment, 1);
if (i + 1 === len) {
this.newline(Math.max(nodeStartLine - lastLine, leadingCommentNewline));
lastLine = nodeStartLine;
}
} else if (type === 1) {
const offset = commentStartLine - (i === 0 ? nodeStartLine : lastLine);
lastLine = commentEndLine;
this.newline(offset);
this._printComment(comment, 1);
if (i + 1 === len) {
this.newline(Math.min(1, nodeEndLine - lastLine));
lastLine = nodeEndLine;
}
} else {
const offset = commentStartLine - (i === 0 ? nodeEndLine - lineOffset : lastLine);
lastLine = commentEndLine;
this.newline(offset);
this._printComment(comment, 1);
}
} else {
hasLoc = false;
if (len === 1) {
this._printComment(comment, 1);
} else if (type === 1 && !(node.type === "ObjectExpression" && node.properties.length > 1) && node.type !== "ClassBody") {
this._printComment(comment, i === 0 ? 2 : i === len - 1 ? 3 : 0);
} else {
this._printComment(comment, 0);
}
}
}
if (type === 2 && hasLoc && lastLine) {
this._lastCommentLine = lastLine;
}
}

@@ -612,0 +752,0 @@ }

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

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

"dependencies": {
"@babel/types": "^7.19.3",
"@babel/types": "^7.19.4",
"@jridgewell/gen-mapping": "^0.3.2",

@@ -28,4 +28,4 @@ "jsesc": "^2.5.1"

"devDependencies": {
"@babel/helper-fixtures": "^7.18.6",
"@babel/parser": "^7.19.3",
"@babel/helper-fixtures": "^7.19.4",
"@babel/parser": "^7.19.4",
"@jridgewell/trace-mapping": "^0.3.8",

@@ -32,0 +32,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

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