Skip to content

Commit

Permalink
Merge pull request #27 from macklinu/feat/fixer-no-shorthand-properties
Browse files Browse the repository at this point in the history
feat: make no-shorthand-properties fixable
  • Loading branch information
nkt committed Aug 12, 2018
2 parents 35d0b65 + 3b7221c commit 79ce51c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -77,7 +77,7 @@ List of supported rules
- `es5/no-modules`: Forbid ES2015 [modules](https://babeljs.io/learn-es2015/#ecmascript-2015-features-modules) usage.
- `es5/no-object-super`: Forbid `super`/`super.foo()` calls.
- `es5/no-rest-parameters`: Forbid [rest parameters](https://babeljs.io/learn-es2015/#ecmascript-2015-features-default-rest-spread).
- `es5/no-shorthand-properties`: Forbid [shorthand properties](https://babeljs.io/learn-es2015/#ecmascript-2015-features-enhanced-object-literals).
- `es5/no-shorthand-properties`:wrench:: Forbid [shorthand properties](https://babeljs.io/learn-es2015/#ecmascript-2015-features-enhanced-object-literals).
- `es5/no-spread`:wrench:: Forbid [...spread expressions](https://babeljs.io/learn-es2015/#ecmascript-2015-features-default-rest-spread).
- `es5/no-template-literals`:wrench:: Forbid [template strings](https://babeljs.io/learn-es2015/#ecmascript-2015-features-template-strings) usage.
- `es5/no-typeof-symbol`: Forbid `typeof foo === 'symbol'` [checks](https://babeljs.io/learn-es2015/#ecmascript-2015-features-symbols).
Expand Down
14 changes: 12 additions & 2 deletions src/rules/no-shorthand-properties.js
Expand Up @@ -5,15 +5,25 @@ module.exports = {
docs: {
description: 'Forbid shorthand properties'
},
schema: []
schema: [],
fixable: 'code'
},
create(context) {
return {
Property(node) {
if (node.shorthand || node.method) {
context.report({
node,
message: 'Unexpected object shorthand property.'
message: 'Unexpected object shorthand property.',
fix(fixer) {
if (node.shorthand) {
return fixer.insertTextAfter(node.key, `: ${node.key.name}`);
}
if (node.method) {
return fixer.replaceText(node.key, `${node.key.name}: function`);
}
return null
}
});
}
}
Expand Down
12 changes: 10 additions & 2 deletions tests/rules/no-shorthand-properties.js
Expand Up @@ -6,7 +6,15 @@ module.exports = {
'var foo = { bar: function () {} };'
],
invalid: [
{ code: 'var foo = { bar };', errors: [{ message: 'Unexpected object shorthand property.' }] },
{ code: 'var foo = { bar() {} };', errors: [{ message: 'Unexpected object shorthand property.' }] }
{
code: 'var foo = { bar };',
errors: [{ message: 'Unexpected object shorthand property.' }],
output: 'var foo = { bar: bar };'
},
{
code: 'var foo = { bar() {} };',
errors: [{ message: 'Unexpected object shorthand property.' }],
output: 'var foo = { bar: function() {} };'
}
]
};

0 comments on commit 79ce51c

Please sign in to comment.