Skip to content

Commit

Permalink
fix: @putout/printer: ObjectProperty inside ObjectPattern after path.…
Browse files Browse the repository at this point in the history
…scope.rename() (babel/babel#15648)
  • Loading branch information
coderaiser committed May 25, 2023
1 parent a67bc3a commit e494ffb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
@@ -0,0 +1,2 @@
const {a: zzz} = b;
console.log(zzz);
@@ -0,0 +1,2 @@
const {a} = b;
console.log(a);
6 changes: 5 additions & 1 deletion lib/tokenize/expressions/object-pattern.js
Expand Up @@ -8,6 +8,10 @@ const {
exists,
} = require('../is');

const wrongShorthand = ({computed, isAssign, keyPath, valuePath}) => {
return !computed && !isAssign && keyPath.node.name !== valuePath.node.name;
};

const isOneParentProperty = ({parentPath}) => parentPath.parentPath.node.properties.length === 1;

module.exports.ObjectPattern = {
Expand Down Expand Up @@ -45,7 +49,7 @@ module.exports.ObjectPattern = {
print(keyPath);
maybe.print(computed, ']');

if (!shorthand) {
if (!shorthand || wrongShorthand({computed, isAssign, keyPath, valuePath})) {
print(': ');
print(valuePath);
} else if (isAssign) {
Expand Down
16 changes: 16 additions & 0 deletions lib/tokenize/expressions/object-pattern.spec.js
@@ -1,6 +1,7 @@
'use strict';

const {extend} = require('supertape');
const traverse = require('@babel/traverse').default;
const {printExtension} = require('../../../test/printer');
const {readFixtures} = require('../../../test/fixture');

Expand Down Expand Up @@ -65,3 +66,18 @@ test('printer: tokenizer: object-pattern: computed', (t) => {
t.print(fixture.objectPatternComputed);
t.end();
});

test('printer: tokenizer: object-pattern: wrong-shorthand', (t) => {
const ast = parse(fixture.objectPatternWrongShorthand);

traverse(ast, {
Statement(path) {
path.scope.rename('a', 'zzz');
},
});

const result = print(ast);

t.equal(result, fixture.objectPatternWrongShorthandFix);
t.end();
});

0 comments on commit e494ffb

Please sign in to comment.