summaryrefslogtreecommitdiff
path: root/tools/node_modules/eslint/node_modules/@babel/generator/lib/printer.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/node_modules/eslint/node_modules/@babel/generator/lib/printer.js')
-rw-r--r--tools/node_modules/eslint/node_modules/@babel/generator/lib/printer.js221
1 files changed, 148 insertions, 73 deletions
diff --git a/tools/node_modules/eslint/node_modules/@babel/generator/lib/printer.js b/tools/node_modules/eslint/node_modules/@babel/generator/lib/printer.js
index 4ece9b30fd..8d33734620 100644
--- a/tools/node_modules/eslint/node_modules/@babel/generator/lib/printer.js
+++ b/tools/node_modules/eslint/node_modules/@babel/generator/lib/printer.js
@@ -9,15 +9,8 @@ var _buffer = require("./buffer");
var n = require("./node");
-var _t = require("@babel/types");
-
var generatorFunctions = require("./generators");
-const {
- isProgram,
- isFile,
- isEmptyStatement
-} = _t;
const SCIENTIFIC_NOTATION = /e/i;
const ZERO_DECIMAL_INTEGER = /\.0+$/;
const NON_DECIMAL_LITERAL = /^0[box]/;
@@ -33,15 +26,19 @@ class Printer {
this.inForStatementInitCounter = 0;
this._printStack = [];
this._indent = 0;
+ this._indentChar = 0;
+ this._indentRepeat = 0;
this._insideAux = false;
this._parenPushNewlineState = null;
this._noLineTerminator = false;
this._printAuxAfterOnNextUserNode = false;
- this._printedComments = new WeakSet();
+ this._printedComments = new Set();
this._endsWithInteger = false;
this._endsWithWord = false;
this.format = format;
this._buf = new _buffer.default(map);
+ this._indentChar = format.indent.style.charCodeAt(0);
+ this._indentRepeat = format.indent.style.length;
}
generate(ast) {
@@ -65,7 +62,11 @@ class Printer {
semicolon(force = false) {
this._maybeAddAuxComment();
- this._append(";", !force);
+ if (force) {
+ this._appendChar(59);
+ } else {
+ this._queue(59);
+ }
}
rightBrace() {
@@ -73,7 +74,7 @@ class Printer {
this._buf.removeLastSemicolon();
}
- this.token("}");
+ this.tokenChar(125);
}
space(force = false) {
@@ -91,13 +92,13 @@ class Printer {
}
word(str) {
- if (this._endsWithWord || this.endsWith(47) && str.charCodeAt(0) === 47) {
+ if (this._endsWithWord || str.charCodeAt(0) === 47 && this.endsWith(47)) {
this._space();
}
this._maybeAddAuxComment();
- this._append(str);
+ this._append(str, false);
this._endsWithWord = true;
}
@@ -107,17 +108,29 @@ class Printer {
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;
}
- token(str) {
+ token(str, maybeNewline = false) {
const lastChar = this.getLastChar();
const strFirst = str.charCodeAt(0);
- if (str === "--" && lastChar === 33 || strFirst === 43 && lastChar === 43 || strFirst === 45 && lastChar === 45 || strFirst === 46 && this._endsWithInteger) {
+ if (lastChar === 33 && str === "--" || strFirst === 43 && lastChar === 43 || strFirst === 45 && lastChar === 45 || strFirst === 46 && this._endsWithInteger) {
+ this._space();
+ }
+
+ this._maybeAddAuxComment();
+
+ this._append(str, maybeNewline);
+ }
+
+ tokenChar(char) {
+ const lastChar = this.getLastChar();
+
+ if (char === 43 && lastChar === 43 || char === 45 && lastChar === 45 || char === 46 && this._endsWithInteger) {
this._space();
}
this._maybeAddAuxComment();
- this._append(str);
+ this._appendChar(char);
}
newline(i = 1) {
@@ -177,61 +190,103 @@ class Printer {
}
_space() {
- this._append(" ", true);
+ this._queue(32);
}
_newline() {
- this._append("\n", true);
+ this._queue(10);
}
- _append(str, queue = false) {
+ _append(str, maybeNewline) {
this._maybeAddParen(str);
- this._maybeIndent(str);
+ this._maybeIndent(str.charCodeAt(0));
+
+ this._buf.append(str, maybeNewline);
+
+ this._endsWithWord = false;
+ this._endsWithInteger = false;
+ }
+
+ _appendChar(char) {
+ this._maybeAddParenChar(char);
+
+ this._maybeIndent(char);
+
+ this._buf.appendChar(char);
+
+ this._endsWithWord = false;
+ this._endsWithInteger = false;
+ }
+
+ _queue(char) {
+ this._maybeAddParenChar(char);
+
+ this._maybeIndent(char);
+
+ this._buf.queue(char);
- if (queue) this._buf.queue(str);else this._buf.append(str);
this._endsWithWord = false;
this._endsWithInteger = false;
}
- _maybeIndent(str) {
- if (this._indent && this.endsWith(10) && str.charCodeAt(0) !== 10) {
- this._buf.queueIndentation(this._getIndent());
+ _maybeIndent(firstChar) {
+ if (this._indent && firstChar !== 10 && this.endsWith(10)) {
+ this._buf.queueIndentation(this._indentChar, this._getIndent());
+ }
+ }
+
+ _maybeAddParenChar(char) {
+ const parenPushNewlineState = this._parenPushNewlineState;
+ if (!parenPushNewlineState) return;
+
+ if (char === 32) {
+ return;
+ }
+
+ if (char !== 10) {
+ this._parenPushNewlineState = null;
+ return;
}
+
+ this.tokenChar(40);
+ this.indent();
+ parenPushNewlineState.printed = true;
}
_maybeAddParen(str) {
const parenPushNewlineState = this._parenPushNewlineState;
if (!parenPushNewlineState) return;
+ const len = str.length;
let i;
- for (i = 0; i < str.length && str[i] === " "; i++) continue;
+ for (i = 0; i < len && str.charCodeAt(i) === 32; i++) continue;
- if (i === str.length) {
+ if (i === len) {
return;
}
- const cha = str[i];
+ const cha = str.charCodeAt(i);
- if (cha !== "\n") {
- if (cha !== "/" || i + 1 === str.length) {
+ if (cha !== 10) {
+ if (cha !== 47 || i + 1 === len) {
this._parenPushNewlineState = null;
return;
}
- const chaPost = str[i + 1];
+ const chaPost = str.charCodeAt(i + 1);
- if (chaPost === "*") {
- if (PURE_ANNOTATION_RE.test(str.slice(i + 2, str.length - 2))) {
+ if (chaPost === 42) {
+ if (PURE_ANNOTATION_RE.test(str.slice(i + 2, len - 2))) {
return;
}
- } else if (chaPost !== "/") {
+ } else if (chaPost !== 47) {
this._parenPushNewlineState = null;
return;
}
}
- this.token("(");
+ this.tokenChar(40);
this.indent();
parenPushNewlineState.printed = true;
}
@@ -250,7 +305,7 @@ class Printer {
}
_getIndent() {
- return this.format.indent.style.repeat(this._indent);
+ return this._indentRepeat * this._indent;
}
printTerminatorless(node, parent, isLabel) {
@@ -268,54 +323,64 @@ class Printer {
if (terminatorState.printed) {
this.dedent();
this.newline();
- this.token(")");
+ this.tokenChar(41);
}
}
}
- print(node, parent) {
+ print(node, parent, noLineTerminator) {
if (!node) return;
- const oldConcise = this.format.concise;
+ const nodeType = node.type;
+ const format = this.format;
+ const oldConcise = format.concise;
if (node._compact) {
- this.format.concise = true;
+ format.concise = true;
}
- const printMethod = this[node.type];
+ const printMethod = this[nodeType];
- if (!printMethod) {
- throw new ReferenceError(`unknown node of type ${JSON.stringify(node.type)} with constructor ${JSON.stringify(node == null ? void 0 : node.constructor.name)}`);
+ if (printMethod === undefined) {
+ throw new ReferenceError(`unknown node of type ${JSON.stringify(nodeType)} with constructor ${JSON.stringify(node.constructor.name)}`);
}
this._printStack.push(node);
const oldInAux = this._insideAux;
- this._insideAux = !node.loc;
+ this._insideAux = node.loc == undefined;
this._maybeAddAuxComment(this._insideAux && !oldInAux);
- let shouldPrintParens = needsParens(node, parent, this._printStack);
+ let shouldPrintParens;
- if (this.format.retainFunctionParens && node.type === "FunctionExpression" && node.extra && node.extra.parenthesized) {
+ if (format.retainFunctionParens && nodeType === "FunctionExpression" && node.extra && node.extra.parenthesized) {
shouldPrintParens = true;
+ } else {
+ shouldPrintParens = needsParens(node, parent, this._printStack);
}
- if (shouldPrintParens) this.token("(");
+ if (shouldPrintParens) this.tokenChar(40);
this._printLeadingComments(node);
- const loc = isProgram(node) || isFile(node) ? null : node.loc;
- this.withSource("start", loc, () => {
- printMethod.call(this, node, parent);
- });
+ const loc = nodeType === "Program" || nodeType === "File" ? null : node.loc;
+ this.withSource("start", loc, printMethod.bind(this, node, parent));
+
+ if (noLineTerminator && !this._noLineTerminator) {
+ this._noLineTerminator = true;
+
+ this._printTrailingComments(node);
- this._printTrailingComments(node);
+ this._noLineTerminator = false;
+ } else {
+ this._printTrailingComments(node);
+ }
- if (shouldPrintParens) this.token(")");
+ if (shouldPrintParens) this.tokenChar(41);
this._printStack.pop();
- this.format.concise = oldConcise;
+ format.concise = oldConcise;
this._insideAux = oldInAux;
}
@@ -364,8 +429,9 @@ class Printer {
const newlineOpts = {
addNewlines: opts.addNewlines
};
+ const len = nodes.length;
- for (let i = 0; i < nodes.length; i++) {
+ for (let i = 0; i < len; i++) {
const node = nodes[i];
if (!node) continue;
if (opts.statement) this._printNewline(true, node, parent, newlineOpts);
@@ -375,7 +441,7 @@ class Printer {
opts.iterator(node, i);
}
- if (opts.separator && i < nodes.length - 1) {
+ if (opts.separator && i < len - 1) {
opts.separator.call(this);
}
@@ -395,7 +461,7 @@ class Printer {
printBlock(parent) {
const node = parent.body;
- if (!isEmptyStatement(node)) {
+ if (node.type !== "EmptyStatement") {
this.space();
}
@@ -455,13 +521,13 @@ class Printer {
}
_getComments(leading, node) {
- return node && (leading ? node.leadingComments : node.trailingComments) || [];
+ return node && (leading ? node.leadingComments : node.trailingComments) || null;
}
_printComment(comment, skipNewLines) {
- if (!this.format.shouldPrintComment(comment.value)) return;
if (comment.ignore) return;
if (this._printedComments.has(comment)) return;
+ if (!this.format.shouldPrintComment(comment.value)) return;
this._printedComments.add(comment);
@@ -474,26 +540,35 @@ class Printer {
this.space();
}
- let val = !isBlockComment && !this._noLineTerminator ? `//${comment.value}\n` : `/*${comment.value}*/`;
+ let val;
+ let maybeNewline = false;
- if (isBlockComment && this.format.indent.adjustMultilineComment) {
- var _comment$loc;
+ if (isBlockComment) {
+ val = `/*${comment.value}*/`;
- const offset = (_comment$loc = comment.loc) == null ? void 0 : _comment$loc.start.column;
+ if (this.format.indent.adjustMultilineComment) {
+ var _comment$loc;
- if (offset) {
- const newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
- val = val.replace(newlineRegex, "\n");
- }
+ const offset = (_comment$loc = comment.loc) == null ? void 0 : _comment$loc.start.column;
- const indentSize = Math.max(this._getIndent().length, this.format.retainLines ? 0 : this._buf.getCurrentColumn());
- val = val.replace(/\n(?!$)/g, `\n${" ".repeat(indentSize)}`);
+ if (offset) {
+ const newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
+ val = val.replace(newlineRegex, "\n");
+ }
+
+ const indentSize = Math.max(this._getIndent(), this.format.retainLines ? 0 : this._buf.getCurrentColumn());
+ val = val.replace(/\n(?!$)/g, `\n${" ".repeat(indentSize)}`);
+ maybeNewline = true;
+ }
+ } else if (!this._noLineTerminator) {
+ val = `//${comment.value}\n`;
+ maybeNewline = true;
+ } else {
+ val = `/*${comment.value}*/`;
}
if (this.endsWith(47)) this._space();
- this.withSource("start", comment.loc, () => {
- this._append(val);
- });
+ this.withSource("start", comment.loc, this._append.bind(this, val, maybeNewline));
if (printNewLines) this.newline(1);
}
@@ -516,11 +591,11 @@ class Printer {
this.space();
this.word("assert");
this.space();
- this.token("{");
+ this.tokenChar(123);
this.space();
this.printList(node.assertions, node);
this.space();
- this.token("}");
+ this.tokenChar(125);
}
}
@@ -534,6 +609,6 @@ var _default = Printer;
exports.default = _default;
function commaSeparator() {
- this.token(",");
+ this.tokenChar(44);
this.space();
} \ No newline at end of file