Commit 0da5176b by Adrian Heine

Honor named-function-expr in Object literals

parent a158f944
Pipeline #5424931 passed with stage
in 1 minute 59 seconds
......@@ -7,21 +7,24 @@ export default class Property extends Node {
if ( this.shorthand ) {
code.insertRight( this.start, `${this.key.name}: ` );
} else if ( this.method ) {
let name;
if ( this.key.type === 'Literal' && typeof this.key.value === 'number' ) {
name = "";
} else if ( this.key.type === 'Identifier' ) {
if ( reserved[ this.key.name ] || ! /^[a-z_$][a-z0-9_$]*$/i.test( this.key.name ) ) {
name = this.findScope( true ).createIdentifier( this.key.name );
let name = '';
if ( this.program.options.namedFunctionExpressions !== false ) {
if ( this.key.type === 'Literal' && typeof this.key.value === 'number' ) {
name = "";
} else if ( this.key.type === 'Identifier' ) {
if ( reserved[ this.key.name ] || ! /^[a-z_$][a-z0-9_$]*$/i.test( this.key.name ) ) {
name = this.findScope( true ).createIdentifier( this.key.name );
} else {
name = this.key.name;
}
} else {
name = this.key.name;
name = this.findScope( true ).createIdentifier( this.key.value );
}
} else {
name = this.findScope( true ).createIdentifier( this.key.value );
name = ' ' + name;
}
if ( this.value.generator ) code.remove( this.start, this.key.start );
code.insertLeft( this.key.end, `: function${this.value.generator ? '*' : ''} ${name}` );
code.insertLeft( this.key.end, `: function${this.value.generator ? '*' : ''}${name}` );
}
}
......
module.exports = [
{
description: 'transpiles shorthand properties',
options: { namedFunctionExpressions: false },
input: `obj = { x, y }`,
output: `obj = { x: x, y: y }`
},
{
description: 'transpiles shorthand methods',
options: { namedFunctionExpressions: false },
input: `
obj = {
foo () { return 42; }
}`,
output: `
obj = {
foo: function () { return 42; }
}`
},
{
description: 'transpiles shorthand methods with quoted names (#82)',
options: { namedFunctionExpressions: false },
input: `
obj = {
'foo-bar' () { return 42; }
}`,
output: `
obj = {
'foo-bar': function () { return 42; }
}`
},
{
description: 'transpiles shorthand methods with reserved names (!68)',
options: { namedFunctionExpressions: false },
input: `
obj = {
catch () { return 42; }
}`,
output: `
obj = {
catch: function () { return 42; }
}`
},
{
description: 'transpiles shorthand methods with numeric or string names (#139)',
options: { namedFunctionExpressions: false },
input: `
obj = {
0() {},
0b101() {},
80() {},
.12e3() {},
0o753() {},
12e34() {},
0xFFFF() {},
"a string"() {},
"var"() {},
}`,
output: `
obj = {
0: function() {},
5: function() {},
80: function() {},
.12e3: function() {},
491: function() {},
12e34: function() {},
0xFFFF: function() {},
"a string": function() {},
"var": function() {},
}`
},
{
description: 'shorthand properties can be disabled with `transforms.conciseMethodProperty === false`',
options: { namedFunctionExpressions: false, transforms: { conciseMethodProperty: false } },
input: `var obj = { x, y, z () {} }`,
output: `var obj = { x, y, z () {} }`
},
{
description: 'computed properties can be disabled with `transforms.computedProperty === false`',
options: { namedFunctionExpressions: false, transforms: { computedProperty: false } },
input: `var obj = { [x]: 'x' }`,
output: `var obj = { [x]: 'x' }`
},
{
description: 'transpiles computed properties without spacing (#117)',
options: { namedFunctionExpressions: false },
input: `
if (1)
console.log(JSON.stringify({['com'+'puted']:1,['foo']:2}));
else
console.log(JSON.stringify({['bar']:3}));
`,
output: `
if (1)
{ console.log(JSON.stringify(( obj = {}, obj['com'+'puted'] = 1, obj['foo'] = 2, obj )));
var obj; }
else
{ console.log(JSON.stringify(( obj$1 = {}, obj$1['bar'] = 3, obj$1 )));
var obj$1; }
`
},
];
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment