nirguna 1.2.1 → 1.2.3
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 +12 -0
- package/package.json +3 -3
- package/packages/compiler-wasm/compiler.js +2 -1
- package/packages/printer-wasm/printer/maybe-type-annotation.js +0 -15
- package/packages/printer-wasm/printer/params.js +0 -72
- package/packages/printer-wasm/printer/printer.js +0 -32
- package/packages/printer-wasm/printer/visitors/block-statement.js +0 -33
- package/packages/printer-wasm/printer/visitors/call-expression/call-expression.js +0 -53
- package/packages/printer-wasm/printer/visitors/call-expression/is-wasm-type.js +0 -6
- package/packages/printer-wasm/printer/visitors/export-named-declaration.js +0 -3
- package/packages/printer-wasm/printer/visitors/expression-statement/expression-statement.js +0 -38
- package/packages/printer-wasm/printer/visitors/expression-statement/print-wasm-import.js +0 -81
- package/packages/printer-wasm/printer/visitors/expression-statement/print-wasm-memory.js +0 -31
- package/packages/printer-wasm/printer/visitors/function-declaration.js +0 -91
- package/packages/printer-wasm/printer/visitors/identifier.js +0 -11
- package/packages/printer-wasm/printer/visitors/if-statement.js +0 -128
- package/packages/printer-wasm/printer/visitors/program.js +0 -26
- package/packages/printer-wasm/printer/visitors/return.js +0 -16
- package/packages/printer-wasm/printer.js +0 -10
package/ChangeLog
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
2026.03.04, v1.2.3
|
|
2
|
+
|
|
3
|
+
feature:
|
|
4
|
+
- 54ff033 @nirguna/printer-wasm: publish
|
|
5
|
+
|
|
6
|
+
2026.03.04, v1.2.2
|
|
7
|
+
|
|
8
|
+
feature:
|
|
9
|
+
- 2414385 nirguna: @putout/printer v18.0.0
|
|
10
|
+
- 87763bb @nirguna/printer-fasm: @putout/printer v18.0.0
|
|
11
|
+
- 5e6e0bb nemesis: get-char: do not print Control/Option
|
|
12
|
+
|
|
1
13
|
2026.03.03, v1.2.1
|
|
2
14
|
|
|
3
15
|
fix:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nirguna",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "Compile JavaScript to WASM and FASM",
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
"@nirguna/plugin-optimizer-wasm": "^1.0.0",
|
|
41
41
|
"@nirguna/plugin-wasm": "^1.0.0",
|
|
42
42
|
"@nirguna/printer-fasm": "^1.0.0",
|
|
43
|
+
"@nirguna/printer-wasm": "^1.0.0",
|
|
43
44
|
"@nirguna/transformer-fasm": "^1.0.0",
|
|
44
45
|
"@nirguna/transformer-wasm": "^1.0.0",
|
|
45
46
|
"@putout/babel": "^5.2.12",
|
|
@@ -47,7 +48,7 @@
|
|
|
47
48
|
"@putout/plugin-esm": "^10.0.1",
|
|
48
49
|
"@putout/plugin-remove-nested-blocks": "^9.0.0",
|
|
49
50
|
"@putout/plugin-remove-useless-operand": "^3.0.0",
|
|
50
|
-
"@putout/printer": "^
|
|
51
|
+
"@putout/printer": "^18.0.0",
|
|
51
52
|
"@typescript-eslint/typescript-estree": "^8.32.1",
|
|
52
53
|
"chalk": "^5.4.1",
|
|
53
54
|
"fasm.js": "^1.0.7",
|
|
@@ -89,7 +90,6 @@
|
|
|
89
90
|
"#compiler-fasm": "./packages/compiler-fasm/compiler.js",
|
|
90
91
|
"#optimizer-fasm": "./packages/optimizer-fasm/optimizer.js",
|
|
91
92
|
"#optimizer-wasm": "./packages/optimizer-wasm/optimizer.js",
|
|
92
|
-
"#printer-wasm": "./packages/printer-wasm/printer.js",
|
|
93
93
|
"#translator-wasm": "./packages/translator-wasm/translator.js",
|
|
94
94
|
"#test-wasm": "./packages/test-wasm/test.js",
|
|
95
95
|
"#translator-fasm": "./packages/translator-fasm/translator.js",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {transform} from '@nirguna/transformer-wasm';
|
|
2
|
+
import {print} from '@nirguna/printer-wasm';
|
|
2
3
|
import {translate} from '#translator-wasm';
|
|
3
|
-
import {print} from '#printer-wasm';
|
|
4
4
|
import {optimize} from '#optimizer-wasm';
|
|
5
5
|
|
|
6
6
|
const noop = () => {};
|
|
@@ -60,3 +60,4 @@ export const compile = async (source, options = {}) => {
|
|
|
60
60
|
emitLastStateChange('translate', places);
|
|
61
61
|
return [binary, places];
|
|
62
62
|
};
|
|
63
|
+
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export const maybeTypeAnnotation = (visit) => (path, printer, semantics) => {
|
|
2
|
-
visit(path, printer, semantics);
|
|
3
|
-
|
|
4
|
-
maybePrintTypeAnnotation(path, printer);
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
export function maybePrintTypeAnnotation(path, printer) {
|
|
8
|
-
const {typeAnnotation} = path.node;
|
|
9
|
-
const {write, traverse} = printer;
|
|
10
|
-
|
|
11
|
-
if (typeAnnotation) {
|
|
12
|
-
write.space();
|
|
13
|
-
traverse(path.get('typeAnnotation'));
|
|
14
|
-
}
|
|
15
|
-
}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
const parseParams = (path) => path.get('params');
|
|
2
|
-
|
|
3
|
-
export const printParams = (path, printer, semantics, customization = {}) => {
|
|
4
|
-
const {extra, typeParameters} = path.node;
|
|
5
|
-
const {
|
|
6
|
-
print,
|
|
7
|
-
maybe,
|
|
8
|
-
traverse,
|
|
9
|
-
} = printer;
|
|
10
|
-
|
|
11
|
-
const {
|
|
12
|
-
params = parseParams(path),
|
|
13
|
-
braceOpen = '(',
|
|
14
|
-
braceClose = ')',
|
|
15
|
-
printSpace = print.space,
|
|
16
|
-
} = customization;
|
|
17
|
-
|
|
18
|
-
if (typeParameters)
|
|
19
|
-
traverse(path.get('typeParameters'));
|
|
20
|
-
|
|
21
|
-
const n = params.length - 1;
|
|
22
|
-
|
|
23
|
-
for (let i = 0; i <= n; i++) {
|
|
24
|
-
printBraceOpen(path, {
|
|
25
|
-
print,
|
|
26
|
-
braceOpen,
|
|
27
|
-
}, semantics);
|
|
28
|
-
|
|
29
|
-
const isLast = i === n;
|
|
30
|
-
const current = params[i];
|
|
31
|
-
|
|
32
|
-
print('$');
|
|
33
|
-
traverse(current);
|
|
34
|
-
|
|
35
|
-
printBraceClose(path, {
|
|
36
|
-
print,
|
|
37
|
-
braceClose,
|
|
38
|
-
}, semantics);
|
|
39
|
-
|
|
40
|
-
if (!isLast)
|
|
41
|
-
printSpace();
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
maybe.print(extra?.trailingComma, ',');
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
function printBraceOpen(path, {print, braceOpen}, semantics) {
|
|
48
|
-
if (isOneArgArrow(path) && !semantics.roundBraces.arrow)
|
|
49
|
-
return;
|
|
50
|
-
|
|
51
|
-
return print(braceOpen);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function printBraceClose(path, {print, braceClose}, semantics) {
|
|
55
|
-
if (isOneArgArrow(path) && !semantics.roundBraces.arrow)
|
|
56
|
-
return;
|
|
57
|
-
|
|
58
|
-
print(braceClose);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function isOneArgArrow(path) {
|
|
62
|
-
if (path.type !== 'ArrowFunctionExpression')
|
|
63
|
-
return false;
|
|
64
|
-
|
|
65
|
-
const {params} = path.node;
|
|
66
|
-
const [param] = params;
|
|
67
|
-
|
|
68
|
-
if (params.length !== 1)
|
|
69
|
-
return false;
|
|
70
|
-
|
|
71
|
-
return param.type === 'Identifier';
|
|
72
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import {print as putoutPrint} from '@putout/printer';
|
|
2
|
-
import {CallExpression} from './visitors/call-expression/call-expression.js';
|
|
3
|
-
import {BlockStatement} from './visitors/block-statement.js';
|
|
4
|
-
import {ExpressionStatement} from './visitors/expression-statement/expression-statement.js';
|
|
5
|
-
import {ExportNamedDeclaration} from './visitors/export-named-declaration.js';
|
|
6
|
-
import {FunctionDeclaration} from './visitors/function-declaration.js';
|
|
7
|
-
import {Program} from './visitors/program.js';
|
|
8
|
-
import {Identifier} from './visitors/identifier.js';
|
|
9
|
-
import {ReturnStatement} from './visitors/return.js';
|
|
10
|
-
import {IfStatement} from './visitors/if-statement.js';
|
|
11
|
-
|
|
12
|
-
export const print = (ast) => {
|
|
13
|
-
return putoutPrint(ast, {
|
|
14
|
-
format: {
|
|
15
|
-
quote: '"',
|
|
16
|
-
},
|
|
17
|
-
semantics: {
|
|
18
|
-
comments: false,
|
|
19
|
-
},
|
|
20
|
-
visitors: {
|
|
21
|
-
ReturnStatement,
|
|
22
|
-
CallExpression,
|
|
23
|
-
BlockStatement,
|
|
24
|
-
IfStatement,
|
|
25
|
-
ExpressionStatement,
|
|
26
|
-
ExportNamedDeclaration,
|
|
27
|
-
FunctionDeclaration,
|
|
28
|
-
Program,
|
|
29
|
-
Identifier,
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
};
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
const isFirstStatement = (path) => path.node.body[0];
|
|
2
|
-
const isFirstDirective = (path) => path.node.directives?.[0];
|
|
3
|
-
|
|
4
|
-
export const BlockStatement = {
|
|
5
|
-
print(path, printer) {
|
|
6
|
-
const {
|
|
7
|
-
indent,
|
|
8
|
-
maybe,
|
|
9
|
-
write,
|
|
10
|
-
traverse,
|
|
11
|
-
} = printer;
|
|
12
|
-
|
|
13
|
-
const body = path.get('body');
|
|
14
|
-
|
|
15
|
-
if (path.parentPath.isBlockStatement())
|
|
16
|
-
indent();
|
|
17
|
-
|
|
18
|
-
indent.inc();
|
|
19
|
-
|
|
20
|
-
if (isFirstStatement(path) || isFirstDirective(path))
|
|
21
|
-
write.newline();
|
|
22
|
-
|
|
23
|
-
for (const element of body) {
|
|
24
|
-
traverse(element);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
indent.dec();
|
|
28
|
-
maybe.indent(body.length);
|
|
29
|
-
|
|
30
|
-
if (path.parentPath.isObjectMethod())
|
|
31
|
-
write(',');
|
|
32
|
-
},
|
|
33
|
-
};
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import {types} from '@putout/babel';
|
|
2
|
-
import {isWasmType} from './is-wasm-type.js';
|
|
3
|
-
|
|
4
|
-
const {isIdentifier} = types;
|
|
5
|
-
const {isArray} = Array;
|
|
6
|
-
|
|
7
|
-
const parseArgs = (path) => {
|
|
8
|
-
const argsPath = path.get('arguments');
|
|
9
|
-
|
|
10
|
-
if (!isArray(argsPath))
|
|
11
|
-
return [];
|
|
12
|
-
|
|
13
|
-
return argsPath;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export function CallExpression(path, {indent, print, maybe, traverse}) {
|
|
17
|
-
const args = parseArgs(path);
|
|
18
|
-
const n = args.length - 1;
|
|
19
|
-
const isParentCall = path.parentPath.isCallExpression();
|
|
20
|
-
const callee = path.get('callee');
|
|
21
|
-
|
|
22
|
-
print('(');
|
|
23
|
-
|
|
24
|
-
traverse(callee);
|
|
25
|
-
|
|
26
|
-
maybe.print.space(args.length);
|
|
27
|
-
maybe.indent.inc(isParentCall);
|
|
28
|
-
|
|
29
|
-
for (const [i, arg] of args.entries()) {
|
|
30
|
-
const isObject = arg.isObjectExpression();
|
|
31
|
-
|
|
32
|
-
if (isParentCall && !isObject && n)
|
|
33
|
-
print.breakline();
|
|
34
|
-
|
|
35
|
-
if (isIdentifier(arg) && !isWasmType(arg.node.name))
|
|
36
|
-
print('$');
|
|
37
|
-
|
|
38
|
-
print(arg);
|
|
39
|
-
|
|
40
|
-
if (isParentCall && n)
|
|
41
|
-
continue;
|
|
42
|
-
|
|
43
|
-
if (i < n)
|
|
44
|
-
print(' ');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (isParentCall) {
|
|
48
|
-
indent.dec();
|
|
49
|
-
maybe.print.breakline(n);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
print(')');
|
|
53
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import {isNext, isPrev} from '@putout/printer/is';
|
|
2
|
-
import {types} from 'putout';
|
|
3
|
-
import {
|
|
4
|
-
isWastImport,
|
|
5
|
-
printWasmImport,
|
|
6
|
-
} from './print-wasm-import.js';
|
|
7
|
-
import {
|
|
8
|
-
isWastMemory,
|
|
9
|
-
printWasmMemory,
|
|
10
|
-
} from './print-wasm-memory.js';
|
|
11
|
-
|
|
12
|
-
const {isFunction} = types;
|
|
13
|
-
|
|
14
|
-
export const ExpressionStatement = (path, printer) => {
|
|
15
|
-
const {print, maybe} = printer;
|
|
16
|
-
|
|
17
|
-
const expression = path.get('expression');
|
|
18
|
-
|
|
19
|
-
if (isWastImport(expression)) {
|
|
20
|
-
printWasmImport(expression, printer);
|
|
21
|
-
maybe.print.breakline(isNext(path));
|
|
22
|
-
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (isWastMemory(expression)) {
|
|
27
|
-
printWasmMemory(expression, printer);
|
|
28
|
-
maybe.print.newline(isNext(path));
|
|
29
|
-
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const surrounded = isNext(path) || isFunction(path.parentPath.parentPath);
|
|
34
|
-
|
|
35
|
-
maybe.indent(surrounded || isPrev(path));
|
|
36
|
-
print('__expression');
|
|
37
|
-
maybe.print.newline(surrounded);
|
|
38
|
-
};
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import {types} from '@putout/babel';
|
|
2
|
-
|
|
3
|
-
const {
|
|
4
|
-
isCallExpression,
|
|
5
|
-
isReturnStatement,
|
|
6
|
-
} = types;
|
|
7
|
-
|
|
8
|
-
export const isWastImport = (expression) => {
|
|
9
|
-
if (!isCallExpression(expression))
|
|
10
|
-
return;
|
|
11
|
-
|
|
12
|
-
const {name} = expression.node.callee;
|
|
13
|
-
|
|
14
|
-
return name === '__nirguna_wasm_import';
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export function printWasmImport(path, printer) {
|
|
18
|
-
const {print, maybe} = printer;
|
|
19
|
-
|
|
20
|
-
const [first, second, func] = path.get('arguments');
|
|
21
|
-
|
|
22
|
-
print('(import ');
|
|
23
|
-
|
|
24
|
-
print(first);
|
|
25
|
-
print(' ');
|
|
26
|
-
print(second);
|
|
27
|
-
print(' ');
|
|
28
|
-
print('(func ');
|
|
29
|
-
|
|
30
|
-
const {name: funcName} = func.node.id;
|
|
31
|
-
const funcArgs = func.get('params');
|
|
32
|
-
const returnType = getReturnType(func);
|
|
33
|
-
|
|
34
|
-
print(`$${funcName} `);
|
|
35
|
-
|
|
36
|
-
const n = funcArgs.length - 1;
|
|
37
|
-
|
|
38
|
-
for (const [i, funcArg] of funcArgs.entries()) {
|
|
39
|
-
printParam(funcArg, printer);
|
|
40
|
-
maybe.print.space(i < n);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (returnType) {
|
|
44
|
-
maybe.print.space(funcArgs.length);
|
|
45
|
-
print('(result ');
|
|
46
|
-
print(returnType);
|
|
47
|
-
print(')');
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
print(')');
|
|
51
|
-
print(')');
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function getReturnType(func) {
|
|
55
|
-
const [first] = func.get('body.body');
|
|
56
|
-
|
|
57
|
-
if (!first)
|
|
58
|
-
return null;
|
|
59
|
-
|
|
60
|
-
if (isReturnStatement(first))
|
|
61
|
-
return first.get('argument');
|
|
62
|
-
|
|
63
|
-
return first.get('expression');
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function printParam(funcArg, {print}) {
|
|
67
|
-
const {name, typeAnnotation} = funcArg.node;
|
|
68
|
-
|
|
69
|
-
if (!typeAnnotation) {
|
|
70
|
-
print(`(param `);
|
|
71
|
-
print(name);
|
|
72
|
-
print(`)`);
|
|
73
|
-
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
print(`(param `);
|
|
78
|
-
print(`$${name} `);
|
|
79
|
-
print(funcArg.get('typeAnnotation.typeAnnotation'));
|
|
80
|
-
print(`)`);
|
|
81
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import {types} from '@putout/babel';
|
|
2
|
-
|
|
3
|
-
const {isCallExpression} = types;
|
|
4
|
-
|
|
5
|
-
export const isWastMemory = (expression) => {
|
|
6
|
-
if (!isCallExpression(expression))
|
|
7
|
-
return;
|
|
8
|
-
|
|
9
|
-
const {name} = expression.node.callee;
|
|
10
|
-
|
|
11
|
-
return name === '__nirguna_wasm_memory';
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export function printWasmMemory(path, printer) {
|
|
15
|
-
const {print} = printer;
|
|
16
|
-
const [first, second] = path.get('arguments');
|
|
17
|
-
|
|
18
|
-
print('(memory ');
|
|
19
|
-
|
|
20
|
-
if (!second) {
|
|
21
|
-
print(first);
|
|
22
|
-
} else {
|
|
23
|
-
print('(export ');
|
|
24
|
-
print(first);
|
|
25
|
-
print(')');
|
|
26
|
-
print.space();
|
|
27
|
-
print(second);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
print(')');
|
|
31
|
-
}
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import {types} from '@putout/babel';
|
|
2
|
-
import {isNext, isNextParent} from '@putout/printer/is';
|
|
3
|
-
import {printParams} from '../params.js';
|
|
4
|
-
|
|
5
|
-
const {
|
|
6
|
-
isAssignmentExpression,
|
|
7
|
-
isTSModuleBlock,
|
|
8
|
-
isBlockStatement,
|
|
9
|
-
isExpressionStatement,
|
|
10
|
-
isFunctionDeclaration,
|
|
11
|
-
} = types;
|
|
12
|
-
|
|
13
|
-
export const FunctionDeclaration = {
|
|
14
|
-
print(path, printer, semantics) {
|
|
15
|
-
const {print} = printer;
|
|
16
|
-
const {generator, returnType} = path.node;
|
|
17
|
-
|
|
18
|
-
print('(');
|
|
19
|
-
print('func');
|
|
20
|
-
|
|
21
|
-
if (!generator) {
|
|
22
|
-
print(' ');
|
|
23
|
-
} else {
|
|
24
|
-
print('*');
|
|
25
|
-
print.space();
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
print('$');
|
|
29
|
-
print('__id');
|
|
30
|
-
|
|
31
|
-
if (path.parentPath.isExportNamedDeclaration()) {
|
|
32
|
-
print(' ');
|
|
33
|
-
print('(');
|
|
34
|
-
print('export ');
|
|
35
|
-
print('"');
|
|
36
|
-
print('__id');
|
|
37
|
-
print('"');
|
|
38
|
-
print(')');
|
|
39
|
-
print(' ');
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
printParams(path, printer, semantics, {
|
|
43
|
-
braceOpen: '(param ',
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
if (returnType) {
|
|
47
|
-
print.space();
|
|
48
|
-
print('(');
|
|
49
|
-
print('result ');
|
|
50
|
-
print('__returnType');
|
|
51
|
-
print(')');
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
print('__body');
|
|
55
|
-
print(')');
|
|
56
|
-
},
|
|
57
|
-
afterSatisfy: () => [isNext, isNextParent, isInsideBlockStatement],
|
|
58
|
-
after(path, {indent, write}) {
|
|
59
|
-
if (isNextAssign(path) || isNextFunction(path) || isNext(path))
|
|
60
|
-
indent();
|
|
61
|
-
|
|
62
|
-
write.breakline();
|
|
63
|
-
//maybe.write.newline(notInsideExportDefaultWithBody(path));
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const isNextFunction = (path) => {
|
|
68
|
-
const next = path.getNextSibling();
|
|
69
|
-
return isFunctionDeclaration(next);
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
const isNextAssign = (path) => {
|
|
73
|
-
const next = path.getNextSibling();
|
|
74
|
-
|
|
75
|
-
if (!isExpressionStatement(next))
|
|
76
|
-
return false;
|
|
77
|
-
|
|
78
|
-
return isAssignmentExpression(next.node.expression);
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
function isInsideBlockStatement(path) {
|
|
82
|
-
const {parentPath} = path;
|
|
83
|
-
|
|
84
|
-
if (isTSModuleBlock(parentPath.parentPath))
|
|
85
|
-
return true;
|
|
86
|
-
|
|
87
|
-
if (!isBlockStatement(parentPath))
|
|
88
|
-
return false;
|
|
89
|
-
|
|
90
|
-
return !path.node.body.body.length;
|
|
91
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import {maybeTypeAnnotation} from '../maybe-type-annotation.js';
|
|
2
|
-
|
|
3
|
-
export const Identifier = maybeTypeAnnotation((path, printer) => {
|
|
4
|
-
const {write, maybe} = printer;
|
|
5
|
-
|
|
6
|
-
const {node} = path;
|
|
7
|
-
const {name, optional} = node;
|
|
8
|
-
|
|
9
|
-
write(name);
|
|
10
|
-
maybe.write(optional, '?');
|
|
11
|
-
});
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import {types} from '@putout/babel';
|
|
2
|
-
import {exists, isInsideIf} from '@putout/printer/is';
|
|
3
|
-
|
|
4
|
-
const {
|
|
5
|
-
isBlockStatement,
|
|
6
|
-
isFunctionDeclaration,
|
|
7
|
-
isStatement,
|
|
8
|
-
isReturnStatement,
|
|
9
|
-
} = types;
|
|
10
|
-
|
|
11
|
-
const isTopLevel = ({parentPath}) => parentPath.parentPath.isProgram();
|
|
12
|
-
|
|
13
|
-
const isInsideNestedBody = ({parentPath}) => {
|
|
14
|
-
if (parentPath.type !== 'BlockStatement')
|
|
15
|
-
return false;
|
|
16
|
-
|
|
17
|
-
return parentPath.parentPath.type === 'BlockStatement';
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const isEmptyBody = (path) => !path.node.body.length;
|
|
21
|
-
|
|
22
|
-
const isLastEmptyInsideBody = (path) => {
|
|
23
|
-
const {parentPath} = path;
|
|
24
|
-
|
|
25
|
-
if (!isBlockStatement(parentPath))
|
|
26
|
-
return false;
|
|
27
|
-
|
|
28
|
-
if (!isBlockStatement(path.node.consequent))
|
|
29
|
-
return false;
|
|
30
|
-
|
|
31
|
-
if (path.node.consequent.body.length)
|
|
32
|
-
return false;
|
|
33
|
-
|
|
34
|
-
return isFunctionDeclaration(path.parentPath.parentPath);
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export const IfStatement = (path, {indent, print, maybe, write, traverse}) => {
|
|
38
|
-
const {parentPath} = path;
|
|
39
|
-
const partOfAlternate = parentPath.get('alternate');
|
|
40
|
-
|
|
41
|
-
if (path !== partOfAlternate)
|
|
42
|
-
indent();
|
|
43
|
-
|
|
44
|
-
print('(if');
|
|
45
|
-
|
|
46
|
-
if (path.node.test.typeArguments) {
|
|
47
|
-
print.space();
|
|
48
|
-
print('(result ');
|
|
49
|
-
print(path.get('test.typeArguments.params.0.typeName'));
|
|
50
|
-
print(')');
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
indent.inc();
|
|
54
|
-
print.breakline();
|
|
55
|
-
|
|
56
|
-
print('__test');
|
|
57
|
-
print.breakline();
|
|
58
|
-
print('(then');
|
|
59
|
-
|
|
60
|
-
const consequent = path.get('consequent');
|
|
61
|
-
const alternate = path.get('alternate');
|
|
62
|
-
const isConsequentBlock = consequent.isBlockStatement();
|
|
63
|
-
const isVar = consequent.isVariableDeclaration();
|
|
64
|
-
|
|
65
|
-
if (isConsequentBlock) {
|
|
66
|
-
print.space();
|
|
67
|
-
print(consequent);
|
|
68
|
-
|
|
69
|
-
if (isInsideIf(path.parentPath) || isInsideNestedBody(path))
|
|
70
|
-
maybe.print.newline(isEmptyBody(consequent));
|
|
71
|
-
} else {
|
|
72
|
-
const isRet = isReturnStatement(consequent);
|
|
73
|
-
print.newline();
|
|
74
|
-
indent.inc();
|
|
75
|
-
maybe.indent(!isRet);
|
|
76
|
-
print(consequent);
|
|
77
|
-
indent.dec();
|
|
78
|
-
maybe.print.newline(!isRet);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
indent();
|
|
82
|
-
print(')');
|
|
83
|
-
print.newline();
|
|
84
|
-
|
|
85
|
-
if (alternate.isBlockStatement()) {
|
|
86
|
-
write.space();
|
|
87
|
-
write('(else');
|
|
88
|
-
write.space();
|
|
89
|
-
traverse(alternate);
|
|
90
|
-
} else if (alternate.isIfStatement()) {
|
|
91
|
-
if (alternate.get('consequent').isBlockStatement())
|
|
92
|
-
write.space();
|
|
93
|
-
else
|
|
94
|
-
indent();
|
|
95
|
-
|
|
96
|
-
write('(else ');
|
|
97
|
-
traverse(alternate);
|
|
98
|
-
} else if (exists(alternate)) {
|
|
99
|
-
maybe.write.newline(isVar);
|
|
100
|
-
maybe.indent(!isConsequentBlock);
|
|
101
|
-
maybe.write.space(isConsequentBlock);
|
|
102
|
-
write('(else');
|
|
103
|
-
write.splitter();
|
|
104
|
-
indent.inc();
|
|
105
|
-
|
|
106
|
-
const isRet = isReturnStatement(alternate);
|
|
107
|
-
maybe.indent(!isRet);
|
|
108
|
-
traverse(alternate);
|
|
109
|
-
maybe.write.newline(!isRet);
|
|
110
|
-
indent.dec();
|
|
111
|
-
indent();
|
|
112
|
-
write(')');
|
|
113
|
-
write.newline();
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const nextPath = path.parentPath.getNextSibling();
|
|
117
|
-
|
|
118
|
-
if (path === partOfAlternate && !isTopLevel(path) && !isStatement(nextPath))
|
|
119
|
-
print.newline();
|
|
120
|
-
|
|
121
|
-
if (isLastEmptyInsideBody(path))
|
|
122
|
-
print.newline();
|
|
123
|
-
|
|
124
|
-
indent.dec();
|
|
125
|
-
indent();
|
|
126
|
-
print(')');
|
|
127
|
-
print.newline();
|
|
128
|
-
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import {hasCoupleTrailingComments} from '@putout/printer/is';
|
|
2
|
-
|
|
3
|
-
export const Program = (path, printer) => {
|
|
4
|
-
const {body} = path.node;
|
|
5
|
-
const {
|
|
6
|
-
traverse,
|
|
7
|
-
write,
|
|
8
|
-
indent,
|
|
9
|
-
} = printer;
|
|
10
|
-
|
|
11
|
-
traverse(path.get('interpreter'));
|
|
12
|
-
write('(');
|
|
13
|
-
write('module');
|
|
14
|
-
indent.inc();
|
|
15
|
-
write.breakline();
|
|
16
|
-
|
|
17
|
-
path.get('body').forEach(traverse);
|
|
18
|
-
indent.dec();
|
|
19
|
-
write.newline();
|
|
20
|
-
write(')');
|
|
21
|
-
|
|
22
|
-
if (body.length && hasCoupleTrailingComments(body.at(-1)))
|
|
23
|
-
return;
|
|
24
|
-
|
|
25
|
-
write.endOfFile();
|
|
26
|
-
};
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export const ReturnStatement = (path, printer) => {
|
|
2
|
-
const {
|
|
3
|
-
indent,
|
|
4
|
-
print,
|
|
5
|
-
maybe,
|
|
6
|
-
} = printer;
|
|
7
|
-
|
|
8
|
-
indent();
|
|
9
|
-
print('(');
|
|
10
|
-
print('return');
|
|
11
|
-
maybe.print.space(path.node.argument);
|
|
12
|
-
|
|
13
|
-
print('__argument');
|
|
14
|
-
print(')');
|
|
15
|
-
print.newline();
|
|
16
|
-
};
|