Skip to content

Commit 524967f

Browse files
committed
feature: @putout/printer: ObjectExpression inside TSAsExpression
1 parent a0f12b5 commit 524967f

6 files changed

Lines changed: 53 additions & 8 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a = {
2+
...c,
3+
[d.name]: 'hello',
4+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a = ({
2+
...c,
3+
[d.name]: 'hello',
4+
});

lib/tokenize/expressions/object-expression/object-expression.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
isIf,
99
hasLeadingComment,
1010
isInsideCall,
11-
isInsideBody,
1211
isInsideExpression,
1312
hasTrailingComment,
13+
callWithParent,
1414
} from '#is';
1515
import {isInsideTuple} from './is-inside-tuple.js';
1616
import {isLooksLikeChain} from '../member-expression/is-looks-like-chain/index.js';
@@ -34,9 +34,17 @@ const notLastArgInsideCall = (path) => {
3434
const hasNoProperties = (path) => !path.node.properties.length;
3535
const hasValue = (path) => path.node.properties[0].value;
3636

37-
const {isMemberExpression} = types;
37+
const {
38+
isMemberExpression,
39+
isAssignmentExpression,
40+
isArrowFunctionExpression,
41+
} = types;
3842

39-
const isParens = createTypeChecker([isInsideBody, isInsideExpression]);
43+
const isParens = createTypeChecker([
44+
['-', callWithParent(isAssignmentExpression)],
45+
['+', callWithParent(isArrowFunctionExpression)],
46+
['+', isInsideExpression],
47+
]);
4048

4149
const callWithCallee = (fn) => (a) => fn(a.get('callee'));
4250

@@ -75,6 +83,7 @@ const isIndentBeforeProperty = createTypeChecker([
7583
]);
7684

7785
export const ObjectExpression = maybeParens({
86+
checkParens: false,
7887
condition: isParens,
7988
print: (path, printer, semantics) => {
8089
const {trailingComma} = semantics;

lib/tokenize/expressions/object-expression/object-expression.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,14 @@ test('printer: tokenizer: object-expression: method: no-trailing-comma', (t) =>
151151
});
152152
t.end();
153153
});
154+
155+
test('printer: tokenizer: object-expression: inside-assign', (t) => {
156+
const source = fixture.objectInsideAssign;
157+
const ast = parse(source);
158+
159+
const result = print(ast);
160+
const expected = fixture.objectInsideAssignFix;
161+
162+
t.equal(result, expected);
163+
t.end();
164+
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var t = (testRelationship) => ({
22
type: 'TestBasisRelationshipsByTestId',
33
id: testRelationship.TestId,
4-
}) as const;
4+
} as const);
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1+
import {types} from '@putout/babel';
12
import {maybeParens} from '#maybe-parens';
3+
import {createTypeChecker} from '#type-checker';
4+
import {callWithParent} from '#is';
25

3-
export const TSAsExpression = maybeParens((path, {print}) => {
4-
print('__expression');
5-
print(' as ');
6-
print('__typeAnnotation');
6+
const {
7+
isCallExpression,
8+
isVariableDeclarator,
9+
} = types;
10+
11+
const condition = createTypeChecker([
12+
['-', callWithParent(isCallExpression)],
13+
['-', callWithParent(isVariableDeclarator)],
14+
['+: node.expression -> ObjectExpression'],
15+
]);
16+
17+
export const TSAsExpression = maybeParens({
18+
condition,
19+
print: (path, {print}) => {
20+
print('__expression');
21+
print(' as ');
22+
print('__typeAnnotation');
23+
},
724
});

0 commit comments

Comments
 (0)