@putout/printer 18.39.0 → 18.40.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ChangeLog
CHANGED
package/lib/printer.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {tokenize} from './tokenize/tokenize.js';
|
|
2
2
|
import {printTokens} from './print-tokens/index.js';
|
|
3
3
|
import {maybeJSON} from './json.js';
|
|
4
|
+
import {maybeSQL} from './sql.js';
|
|
4
5
|
|
|
5
6
|
export {maybeVisitor} from './tokenize/maybe/index.js';
|
|
6
7
|
export * as visitors from './tokenize/visitors.js';
|
|
@@ -8,7 +9,11 @@ export * as visitors from './tokenize/visitors.js';
|
|
|
8
9
|
export const print = (ast, overrides = {}) => {
|
|
9
10
|
check(ast);
|
|
10
11
|
|
|
11
|
-
const options =
|
|
12
|
+
const options = {
|
|
13
|
+
...maybeJSON(ast, overrides),
|
|
14
|
+
...maybeSQL(ast, overrides),
|
|
15
|
+
};
|
|
16
|
+
|
|
12
17
|
const tokens = tokenize(ast, options);
|
|
13
18
|
|
|
14
19
|
return printTokens(tokens);
|
package/lib/sql.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {types} from '@putout/babel';
|
|
2
|
+
import {isSQL} from '@putout/operator-json';
|
|
3
|
+
|
|
4
|
+
const {
|
|
5
|
+
isCallExpression,
|
|
6
|
+
isIdentifier,
|
|
7
|
+
} = types;
|
|
8
|
+
|
|
9
|
+
export const maybeSQL = (ast, overrides) => {
|
|
10
|
+
if (isASTSQL(ast))
|
|
11
|
+
return {
|
|
12
|
+
...overrides,
|
|
13
|
+
semantics: {
|
|
14
|
+
...overrides?.semantics,
|
|
15
|
+
maxArgsInOneLine: 1,
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
return overrides;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function isASTSQL(ast) {
|
|
23
|
+
const {program} = ast;
|
|
24
|
+
|
|
25
|
+
if (!program)
|
|
26
|
+
return false;
|
|
27
|
+
|
|
28
|
+
const {body} = ast.program;
|
|
29
|
+
|
|
30
|
+
if (!body.length)
|
|
31
|
+
return false;
|
|
32
|
+
|
|
33
|
+
const {expression} = ast.program.body[0];
|
|
34
|
+
|
|
35
|
+
if (!isCallExpression(expression))
|
|
36
|
+
return false;
|
|
37
|
+
|
|
38
|
+
const {callee} = expression;
|
|
39
|
+
|
|
40
|
+
if (!isIdentifier(callee))
|
|
41
|
+
return false;
|
|
42
|
+
|
|
43
|
+
return isSQL(callee.name);
|
|
44
|
+
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import {types} from '@putout/babel';
|
|
2
|
-
import {
|
|
2
|
+
import {isInsideCall} from '#is';
|
|
3
3
|
import {maybeParens} from '#maybe-parens';
|
|
4
4
|
import {createTypeChecker} from '#type-checker';
|
|
5
|
-
import {CallExpressionWithMaxArgs} from './call-expression-with-max-args.js';
|
|
6
5
|
|
|
7
6
|
const {
|
|
8
7
|
isIdentifier,
|
|
@@ -21,62 +20,64 @@ const parseArgs = (path) => {
|
|
|
21
20
|
return argsPath;
|
|
22
21
|
};
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return args.length >= maxArgsInOneLine;
|
|
33
|
-
}
|
|
23
|
+
const getMaxArgsInOneLine = (path, {maxArgsInOneLine}) => maxArgsInOneLine;
|
|
24
|
+
const firstArgIsArray = (path) => isArrayExpression(path.node?.arguments?.[0]);
|
|
25
|
+
|
|
26
|
+
const isMaxArgs = createTypeChecker([
|
|
27
|
+
['-', firstArgIsArray],
|
|
28
|
+
['+: node.arguments.length', '>=', getMaxArgsInOneLine],
|
|
29
|
+
]);
|
|
34
30
|
|
|
35
|
-
const
|
|
31
|
+
const isMultiline = createTypeChecker([
|
|
36
32
|
['-: -> !', tooLong],
|
|
37
33
|
['+', isInsideCall],
|
|
38
34
|
]);
|
|
39
35
|
|
|
40
36
|
export const CallExpression = maybeParens((path, printer, semantics) => {
|
|
41
|
-
|
|
42
|
-
return CallExpressionWithMaxArgs(path, printer);
|
|
43
|
-
|
|
44
|
-
const {
|
|
45
|
-
indent,
|
|
46
|
-
print,
|
|
47
|
-
maybe,
|
|
48
|
-
traverse,
|
|
49
|
-
} = printer;
|
|
37
|
+
const {print, maybe} = printer;
|
|
50
38
|
|
|
51
39
|
const args = parseArgs(path);
|
|
52
|
-
const needsNewline = isNeedsNewLine(path, semantics);
|
|
53
40
|
|
|
54
|
-
|
|
55
|
-
|
|
41
|
+
print('__callee');
|
|
42
|
+
print('__typeArguments');
|
|
43
|
+
maybe.print(path.node.optional, '?.');
|
|
56
44
|
|
|
57
|
-
|
|
45
|
+
print('(');
|
|
58
46
|
|
|
59
|
-
|
|
60
|
-
|
|
47
|
+
const maxArgs = isMaxArgs(path, semantics);
|
|
48
|
+
const multiline = maxArgs || isMultiline(path, semantics);
|
|
61
49
|
|
|
62
|
-
|
|
63
|
-
|
|
50
|
+
printArgs(args, printer, {
|
|
51
|
+
maxArgs,
|
|
52
|
+
multiline,
|
|
53
|
+
});
|
|
64
54
|
|
|
65
|
-
print('
|
|
55
|
+
print(')');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
export const OptionalCallExpression = CallExpression;
|
|
59
|
+
|
|
60
|
+
function printArgs(args, printer, {maxArgs, multiline}) {
|
|
61
|
+
const {
|
|
62
|
+
indent,
|
|
63
|
+
print,
|
|
64
|
+
maybe,
|
|
65
|
+
} = printer;
|
|
66
66
|
|
|
67
67
|
const n = args.length - 1;
|
|
68
68
|
|
|
69
|
-
maybe.indent.inc(
|
|
69
|
+
maybe.indent.inc(multiline);
|
|
70
70
|
|
|
71
71
|
for (const [i, arg] of args.entries()) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
if (multiline)
|
|
73
|
+
if (maxArgs && isArrayExpression(arg))
|
|
74
|
+
print.space();
|
|
75
|
+
else if (n || maxArgs && !isObjectExpression(arg))
|
|
76
|
+
print.breakline();
|
|
76
77
|
|
|
77
78
|
print(arg);
|
|
78
79
|
|
|
79
|
-
if (
|
|
80
|
+
if (multiline && (n || maxArgs)) {
|
|
80
81
|
print(',');
|
|
81
82
|
continue;
|
|
82
83
|
}
|
|
@@ -87,15 +88,11 @@ export const CallExpression = maybeParens((path, printer, semantics) => {
|
|
|
87
88
|
}
|
|
88
89
|
}
|
|
89
90
|
|
|
90
|
-
if (
|
|
91
|
+
if (multiline) {
|
|
91
92
|
indent.dec();
|
|
92
|
-
maybe.print.breakline(n);
|
|
93
|
+
maybe.print.breakline(n || maxArgs);
|
|
93
94
|
}
|
|
94
|
-
|
|
95
|
-
print(')');
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
export const OptionalCallExpression = CallExpression;
|
|
95
|
+
}
|
|
99
96
|
|
|
100
97
|
function tooLong(path) {
|
|
101
98
|
const args = parseArgs(path);
|
package/package.json
CHANGED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export const CallExpressionWithMaxArgs = (path, {write, indent, traverse}) => {
|
|
2
|
-
const {name} = path.node.callee;
|
|
3
|
-
const args = path.get('arguments');
|
|
4
|
-
|
|
5
|
-
write(name);
|
|
6
|
-
write('(');
|
|
7
|
-
indent.inc();
|
|
8
|
-
|
|
9
|
-
for (const arg of args) {
|
|
10
|
-
write.breakline();
|
|
11
|
-
traverse(arg);
|
|
12
|
-
write(',');
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
indent.dec();
|
|
16
|
-
write.breakline();
|
|
17
|
-
write(')');
|
|
18
|
-
};
|