Repository: Rich-Harris/buble
Pull request #91 (2018-01-06 09:42:41)

Handle string properties after computed properties correctly
it currently compiles to 
```js
var obj;
foo(( obj = {}, obj['computed'] = 1, obj.'some-var' = 2, obj ));
```

StephanHoyer: Sorry for the syntax error. I try to fix this and also try to add a fix for this issue this weekend.

And BTW: thanks for this great tool!
StephanHoyer: Not the most elegant way to fix this.

Any tipps where appreciated.

Patch:
diff --git a/src/program/types/ObjectExpression.js b/src/program/types/ObjectExpression.js
index 76a9aa9..7a476f1 100644
--- a/src/program/types/ObjectExpression.js
+++ b/src/program/types/ObjectExpression.js
@@ -161,7 +161,7 @@ export default class ObjectExpression extends Node {
 					} else {
 						const propId =
 							(isSimpleAssignment ? `;\n${i0}${name}` : `, ${name}`) +
-							(prop.computed ? '' : '.');
+							(prop.key.type === 'Literal' || prop.computed ? '' : '.');
 
 						if (moveStart < prop.start) {
 							code.overwrite(moveStart, prop.start, propId);
@@ -175,7 +175,15 @@ export default class ObjectExpression extends Node {
 						while (code.original[c] !== ']') c += 1;
 						c += 1;
 					}
-					if (prop.shorthand) {
+					if (prop.key.type === 'Literal' && !prop.computed) {
+						console.log('' + code)
+						code.overwrite(
+							prop.start,
+							prop.key.end + 1,
+							'[' + code.slice(prop.start, prop.key.end) + '] = '
+						);
+						console.log('' + code)
+					} else if (prop.shorthand) {
 						code.overwrite(
 							prop.start,
 							prop.key.end,
diff --git a/src/program/types/VariableDeclaration.js b/src/program/types/VariableDeclaration.js
index e95283b..694e1e7 100644
--- a/src/program/types/VariableDeclaration.js
+++ b/src/program/types/VariableDeclaration.js
@@ -43,7 +43,7 @@ export default class VariableDeclaration extends Node {
 						declarator.init.type === 'Identifier' && !declarator.init.rewritten;
 
 					const name = simple
-						? declarator.init.name
+						? (declarator.init.alias || declarator.init.name)
 						: declarator.findScope(true).createIdentifier('ref');
 
 					c = declarator.start;


Test patch:
diff --git a/test/samples/destructuring.js b/test/samples/destructuring.js
index 2ad05f6..5571b87 100644
--- a/test/samples/destructuring.js
+++ b/test/samples/destructuring.js
@@ -858,5 +858,23 @@ module.exports = [
 				var message = ref.message;
 
 			}`
+	},
+
+	{
+		description: 'destructures parameters with same name as function',
+
+		input: `
+			const a = {
+				options (options) {
+					const { input } = options;
+				}
+			}`,
+
+		output: `
+			var a = {
+				options: function options (options$1) {
+					var input = options$1.input;
+				}
+			}`
 	}
 ];
diff --git a/test/samples/object-properties.js b/test/samples/object-properties.js
index c43427f..a72a497 100644
--- a/test/samples/object-properties.js
+++ b/test/samples/object-properties.js
@@ -114,8 +114,20 @@ module.exports = [
 	},
 
 	{
-		description: 'avoids shadowing free variables with method names (#166)',
+		description: 'transpiles string-keyed properties after computed properties',
+
+		input: `
+			fn({['computed']:1, 'some-var':2, a: 3});
+		`,
+		output: `
+			var obj;
 
+			fn(( obj = {}, obj['computed'] = 1, obj['some-var'] = 2, obj.a = 3, obj ));
+		`
+	},
+
+	{
+		description: 'avoids shadowing free variables with method names (#166)',
 		input: `
 			let x = {
 				foo() { return foo },