Skip to content
This repository has been archived by the owner on Aug 4, 2020. It is now read-only.

Update: Implement auto fix for object-curly-spacing (fixes #37) #39

Merged
merged 1 commit into from
Dec 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions rules/object-curly-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,15 @@ module.exports = function(context) {
* @returns {void}
*/
function reportNoBeginningSpace(node, token) {
context.report(node, token.loc.end,
"There should be no space after '" + token.value + "'");
context.report({
node: node,
loc: token.loc.end,
message: "There should be no space after '" + token.value + "'",
fix: function(fixer) {
var nextToken = sourceCode.getTokenAfter(token);
return fixer.removeRange([token.range[1], nextToken.range[0]]);
}
});
}

/**
Expand All @@ -78,8 +85,15 @@ module.exports = function(context) {
* @returns {void}
*/
function reportNoEndingSpace(node, token) {
context.report(node, token.loc.start,
"There should be no space before '" + token.value + "'");
context.report({
node: node,
loc: token.loc.start,
message: "There should be no space before '" + token.value + "'",
fix: function(fixer) {
var previousToken = sourceCode.getTokenBefore(token);
return fixer.removeRange([previousToken.range[1], token.range[0]]);
}
});
}

/**
Expand All @@ -89,8 +103,14 @@ module.exports = function(context) {
* @returns {void}
*/
function reportRequiredBeginningSpace(node, token) {
context.report(node, token.loc.end,
"A space is required after '" + token.value + "'");
context.report({
node: node,
loc: token.loc.end,
message: "A space is required after '" + token.value + "'",
fix: function(fixer) {
return fixer.insertTextAfter(token, " ");
}
});
}

/**
Expand All @@ -100,8 +120,14 @@ module.exports = function(context) {
* @returns {void}
*/
function reportRequiredEndingSpace(node, token) {
context.report(node, token.loc.start,
"A space is required before '" + token.value + "'");
context.report({
node: node,
loc: token.loc.start,
message: "A space is required before '" + token.value + "'",
fix: function(fixer) {
return fixer.insertTextBefore(token, " ");
}
});
}

/**
Expand Down
47 changes: 38 additions & 9 deletions tests/object-curly-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,12 @@ ruleTester.run('babel/object-curly-spacing', rule, {
// Babel test cases.
{ code: "export * as x from \"mod\";", parser: "babel-eslint", ecmaFeatures: { modules: true } },
{ code: "export x from \"mod\";", parser: "babel-eslint", ecmaFeatures: { modules: true } },

// always - destructuring typed object param
{ code: "function fn({ a,b }:Object){}", options: ["always"], parser: "babel-eslint", ecmaFeatures: { destructuring: true } },

// never - destructuring typed object param
{ code: "function fn({a,b}: Object){}", options: ["never"], parser: "babel-eslint", ecmaFeatures: { destructuring: true } },
],

invalid: [
{
code: "import {bar} from 'foo.js';",
output: "import { bar } from 'foo.js';",
options: ["always"],
ecmaFeatures: {
modules: true
Expand All @@ -175,6 +170,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "import { bar as y} from 'foo.js';",
output: "import { bar as y } from 'foo.js';",
options: ["always"],
ecmaFeatures: {
modules: true
Expand All @@ -190,6 +186,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "import {bar as y} from 'foo.js';",
output: "import { bar as y } from 'foo.js';",
options: ["always"],
ecmaFeatures: {
modules: true
Expand All @@ -211,6 +208,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "import { bar} from 'foo.js';",
output: "import { bar } from 'foo.js';",
options: ["always"],
ecmaFeatures: {
modules: true
Expand All @@ -226,6 +224,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "import x, { bar} from 'foo';",
output: "import x, { bar } from 'foo';",
options: ["always"],
ecmaFeatures: {
modules: true
Expand All @@ -242,6 +241,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "import x, { bar, baz} from 'foo';",
output: "import x, { bar, baz } from 'foo';",
options: ["always"],
ecmaFeatures: {
modules: true
Expand All @@ -258,6 +258,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "import x, {bar} from 'foo';",
output: "import x, { bar } from 'foo';",
options: ["always"],
ecmaFeatures: {
modules: true
Expand All @@ -280,6 +281,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "import x, {bar, baz} from 'foo';",
output: "import x, { bar, baz } from 'foo';",
options: ["always"],
ecmaFeatures: {
modules: true
Expand All @@ -301,6 +303,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "import {bar,} from 'foo';",
output: "import { bar, } from 'foo';",
options: ["always"],
ecmaFeatures: {
modules: true
Expand All @@ -323,6 +326,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "import { bar, } from 'foo';",
output: "import {bar,} from 'foo';",
options: ["never"],
ecmaFeatures: {
modules: true
Expand All @@ -344,6 +348,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "export {bar};",
output: "export { bar };",
options: ["always"],
ecmaFeatures: {
modules: true
Expand All @@ -367,6 +372,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
// always - arraysInObjects
{
code: "var obj = { 'foo': [ 1, 2 ] };",
output: "var obj = { 'foo': [ 1, 2 ]};",
options: ["always", {"arraysInObjects": false}],
errors: [
{
Expand All @@ -377,6 +383,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var obj = { 'foo': [ 1, 2 ] , 'bar': [ 'baz', 'qux' ] };",
output: "var obj = { 'foo': [ 1, 2 ] , 'bar': [ 'baz', 'qux' ]};",
options: ["always", {"arraysInObjects": false}],
errors: [
{
Expand All @@ -389,6 +396,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
// always-objectsInObjects
{
code: "var obj = { 'foo': { 'bar': 1, 'baz': 2 } };",
output: "var obj = { 'foo': { 'bar': 1, 'baz': 2 }};",
options: ["always", {"objectsInObjects": false}],
errors: [
{
Expand All @@ -401,6 +409,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var obj = { 'foo': [ 1, 2 ] , 'bar': { 'baz': 1, 'qux': 2 } };",
output: "var obj = { 'foo': [ 1, 2 ] , 'bar': { 'baz': 1, 'qux': 2 }};",
options: ["always", {"objectsInObjects": false}],
errors: [
{
Expand All @@ -415,6 +424,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
// always-destructuring trailing comma
{
code: "var { a,} = x;",
output: "var { a, } = x;",
options: ["always"],
ecmaFeatures: { destructuring: true },
errors: [
Expand All @@ -428,6 +438,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var {a, } = x;",
output: "var {a,} = x;",
options: ["never"],
ecmaFeatures: { destructuring: true },
errors: [
Expand All @@ -441,6 +452,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var {a:b } = x;",
output: "var {a:b} = x;",
options: ["never"],
ecmaFeatures: { destructuring: true },
errors: [
Expand All @@ -454,6 +466,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var { a:b } = x;",
output: "var {a:b} = x;",
options: ["never"],
ecmaFeatures: { destructuring: true },
errors: [
Expand All @@ -475,6 +488,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
// never-objectsInObjects
{
code: "var obj = {'foo': {'bar': 1, 'baz': 2}};",
output: "var obj = {'foo': {'bar': 1, 'baz': 2} };",
options: ["never", {"objectsInObjects": true}],
errors: [
{
Expand All @@ -487,6 +501,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var obj = {'foo': [1, 2] , 'bar': {'baz': 1, 'qux': 2}};",
output: "var obj = {'foo': [1, 2] , 'bar': {'baz': 1, 'qux': 2} };",
options: ["never", {"objectsInObjects": true}],
errors: [
{
Expand All @@ -501,6 +516,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
// always & never
{
code: "var obj = {foo: bar, baz: qux};",
output: "var obj = { foo: bar, baz: qux };",
options: ["always"],
errors: [
{
Expand All @@ -519,6 +535,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var obj = {foo: bar, baz: qux };",
output: "var obj = { foo: bar, baz: qux };",
options: ["always"],
errors: [
{
Expand All @@ -531,6 +548,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var obj = { foo: bar, baz: qux};",
output: "var obj = { foo: bar, baz: qux };",
options: ["always"],
errors: [
{
Expand All @@ -543,6 +561,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var obj = { foo: bar, baz: qux };",
output: "var obj = {foo: bar, baz: qux};",
options: ["never"],
errors: [
{
Expand All @@ -561,6 +580,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var obj = {foo: bar, baz: qux };",
output: "var obj = {foo: bar, baz: qux};",
options: ["never"],
errors: [
{
Expand All @@ -573,6 +593,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var obj = { foo: bar, baz: qux};",
output: "var obj = {foo: bar, baz: qux};",
options: ["never"],
errors: [
{
Expand All @@ -585,6 +606,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var obj = { foo: { bar: quxx}, baz: qux};",
output: "var obj = {foo: {bar: quxx}, baz: qux};",
options: ["never"],
errors: [
{
Expand All @@ -603,6 +625,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var obj = {foo: {bar: quxx }, baz: qux };",
output: "var obj = {foo: {bar: quxx}, baz: qux};",
options: ["never"],
errors: [
{
Expand All @@ -621,6 +644,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "export const thing = {value: 1 };",
output: "export const thing = { value: 1 };",
ecmaFeatures: {
modules: true,
blockBindings: true
Expand All @@ -639,7 +663,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
// destructuring
{
code: "var {x, y} = y",
utput: "var { x, y = y",
output: "var { x, y } = y",
ecmaFeatures: {destructuring: true},
options: ["always"],
errors: [
Expand All @@ -659,7 +683,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var { x, y} = y",
ouput: "var { x, y } y",
output: "var { x, y } = y",
ecmaFeatures: {destructuring: true},
options: ["always"],
errors: [
Expand All @@ -673,7 +697,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var { x, y } = y",
ouput: "var {x, y} = ",
output: "var {x, y} = y",
ecmaFeatures: {destructuring: true},
options: ["never"],
errors: [
Expand All @@ -693,6 +717,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var {x, y } = y",
output: "var {x, y} = y",
ecmaFeatures: {destructuring: true},
options: ["never"],
errors: [
Expand All @@ -706,6 +731,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var { x=10} = y",
output: "var { x=10 } = y",
ecmaFeatures: {destructuring: true},
options: ["always"],
errors: [
Expand All @@ -719,6 +745,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var {x=10 } = y",
output: "var { x=10 } = y",
ecmaFeatures: {destructuring: true},
options: ["always"],
errors: [
Expand All @@ -734,6 +761,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
// never - arraysInObjects
{
code: "var obj = {'foo': [1, 2]};",
output: "var obj = {'foo': [1, 2] };",
options: ["never", {"arraysInObjects": true}],
errors: [
{
Expand All @@ -744,6 +772,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
},
{
code: "var obj = {'foo': [1, 2] , 'bar': ['baz', 'qux']};",
output: "var obj = {'foo': [1, 2] , 'bar': ['baz', 'qux'] };",
options: ["never", {"arraysInObjects": true}],
errors: [
{
Expand Down