Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
Merge d965d35 into fbe9bf4
Browse files Browse the repository at this point in the history
  • Loading branch information
fxmaxvl committed Mar 12, 2016
2 parents fbe9bf4 + d965d35 commit e8b9a64
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 2 deletions.
4 changes: 3 additions & 1 deletion grouping.json
Expand Up @@ -164,7 +164,9 @@
"requireArrayDestructuring",
"disallowVar",
"requireSpaceBeforeDestructuredValues",
"disallowArrayDestructuringReturn"
"disallowArrayDestructuringReturn",
"requireSpacesInsideImportedObjectBraces",
"disallowSpacesInsideImportedObjectBraces"
],
"Everything else": [
"requireParenthesesAroundIIFE",
Expand Down
2 changes: 2 additions & 0 deletions lib/config/configuration.js
Expand Up @@ -806,6 +806,8 @@ Configuration.prototype.registerDefaultRules = function() {
this.registerRule(require('../rules/require-imports-alphabetized'));
this.registerRule(require('../rules/require-space-before-destructured-values'));
this.registerRule(require('../rules/disallow-array-destructuring-return'));
this.registerRule(require('../rules/require-spaces-inside-imported-object-braces'));
this.registerRule(require('../rules/disallow-spaces-inside-imported-object-braces'));
/* ES6 only (end) */

this.registerRule(require('../rules/require-curly-braces'));
Expand Down
82 changes: 82 additions & 0 deletions lib/rules/disallow-spaces-inside-imported-object-braces.js
@@ -0,0 +1,82 @@
/**
* Disallow space after opening object curly brace and before closing in import statements.
*
* Type: `Boolean`
*
* Value: `true`
*
* #### Example
*
* ```js
* "disallowSpacesInsideImportedObjectBraces": true
* ```
*
* ##### Valid
*
* ```js
* import {foo, bar} from 'foo-bar';
*
* import {foo as f, bar} from 'foo-bar';
* ```
*
* ##### Invalid
*
* ```js
* import { foo, bar } from 'foo-bar';
*
* import { foo as f, bar } from 'foo-bar';
* ```
*/

var assert = require('assert');

module.exports = function() {};

module.exports.prototype = {

configure: function(options) {
assert(
options === true,
this.getOptionName() + ' option requires a true value or should be removed'
);
},

getOptionName: function() {
return 'disallowSpacesInsideImportedObjectBraces';
},

check: function(file, errors) {
file.iterateNodesByType(['ImportDeclaration'], function(node) {

if (!node.specifiers) {
return;
}

node.specifiers.forEach(function(specifier) {

if (specifier.type !== 'ImportSpecifier') {
return;
}

var maybeOpeningBrace = file.getPrevToken(specifier.firstToken);
var maybeClosingBrace = file.getNextToken(specifier.lastToken);

if (maybeOpeningBrace.value === '{') {
errors.assert.noWhitespaceBetween({
token: maybeOpeningBrace,
nextToken: specifier.firstToken,
message: 'Illegal space after opening curly brace'
});
}

if (maybeClosingBrace.value === '}') {
errors.assert.noWhitespaceBetween({
token: specifier.lastToken,
nextToken: maybeClosingBrace,
message: 'Illegal space before closing curly brace'
});
}
});
});
}
};
84 changes: 84 additions & 0 deletions lib/rules/require-spaces-inside-imported-object-braces.js
@@ -0,0 +1,84 @@
/**
* Requires space after opening object curly brace and before closing in import statements.
*
* Type: `Boolean`
*
* Value: `true`
*
* #### Example
*
* ```js
* "requireSpacesInsideImportedObjectBraces": true
* ```
*
* ##### Valid
*
* ```js
* import { foo, bar } from 'foo-bar';
*
* import { foo as f, bar } from 'foo-bar';
* ```
*
* ##### Invalid
*
* ```js
* import {foo, bar} from 'foo-bar';
*
* import {foo as f, bar} from 'foo-bar';
* ```
*/

var assert = require('assert');

module.exports = function() {};

module.exports.prototype = {

configure: function(options) {
assert(
options === true,
this.getOptionName() + ' option requires a true value or should be removed'
);
},

getOptionName: function() {
return 'requireSpacesInsideImportedObjectBraces';
},

check: function(file, errors) {
file.iterateNodesByType(['ImportDeclaration'], function(node) {

if (!node.specifiers) {
return;
}

node.specifiers.forEach(function(specifier) {

if (specifier.type !== 'ImportSpecifier') {
return;
}

var maybeOpeningBrace = file.getPrevToken(specifier.firstToken);
var maybeClosingBrace = file.getNextToken(specifier.lastToken);

if (maybeOpeningBrace.value === '{') {
errors.assert.spacesBetween({
token: maybeOpeningBrace,
nextToken: specifier.firstToken,
exactly: 1,
message: 'One space required after opening curly brace'
});
}

if (maybeClosingBrace.value === '}') {
errors.assert.spacesBetween({
token: specifier.lastToken,
nextToken: maybeClosingBrace,
exactly: 1,
message: 'One space required before closing curly brace'
});
}
});
});
}
};
3 changes: 2 additions & 1 deletion presets/airbnb.json
Expand Up @@ -76,5 +76,6 @@
"validateIndentation": 2,
"maximumLineLength": 100,
"disallowArrayDestructuringReturn": true,
"requireShorthandArrowFunctions": true
"requireShorthandArrowFunctions": true,
"requireSpacesInsideImportedObjectBraces": true
}
44 changes: 44 additions & 0 deletions test/specs/rules/disallow-spaces-inside-imported-object-braces.js
@@ -0,0 +1,44 @@
var Checker = require('../../../lib/checker');
var expect = require('chai').expect;

describe('rules/disallow-spaces-inside-imported-object-braces', function() {
var checker;
beforeEach(function() {
checker = new Checker();
checker.registerDefaultRules();
});

describe('when { disallowSpacesInsideImportedObjectBraces: true }', function() {
beforeEach(function() {
checker.configure({ disallowSpacesInsideImportedObjectBraces: true });
});

it('should not report for import without braces', function() {
expect(checker.checkString('import fooBar from "foo-bar";')).to.have.no.errors();
expect(checker.checkString('import * as fooBar from "foo-bar";')).to.have.no.errors();
expect(checker.checkString('import {} from "foo-bar";')).to.have.no.errors();
});

it('should report for import with spaces', function() {
expect(
checker.checkString('import { foo} from "foo-bar";')
).to.have.error.count.equal(1);

expect(
checker.checkString('import {foo } from "foo-bar";')
).to.have.error.count.equal(1);

expect(
checker.checkString('import { foo, bar } from "foo-bar";')
).to.have.error.count.equal(2);

expect(
checker.checkString('import fooBar, { foo, bar } from "foo-bar";')
).to.have.error.count.equal(2);

expect(
checker.checkString('import {foo as bar, bar as foo } from "foo-bar";')
).to.have.error.count.equal(1);
});
});
});
40 changes: 40 additions & 0 deletions test/specs/rules/require-spaces-inside-imported-object-braces.js
@@ -0,0 +1,40 @@
var Checker = require('../../../lib/checker');
var expect = require('chai').expect;

describe('rules/require-spaces-inside-imported-object-braces', function() {
var checker;
beforeEach(function() {
checker = new Checker();
checker.registerDefaultRules();
});

describe('when { requireSpacesInsideImportedObjectBraces: true }', function() {
beforeEach(function() {
checker.configure({ requireSpacesInsideImportedObjectBraces: true });
});

it('should not report for import without braces', function() {
expect(checker.checkString('import fooBar from "foo-bar";')).to.have.no.errors();
expect(checker.checkString('import * as fooBar from "foo-bar";')).to.have.no.errors();
expect(checker.checkString('import {} from "foo-bar";')).to.have.no.errors();
});

it('should report for import without spaces', function() {
expect(
checker.checkString('import {foo} from "foo-bar";')
).to.have.error.count.equal(2);

expect(
checker.checkString('import {foo, bar} from "foo-bar";')
).to.have.error.count.equal(2);

expect(
checker.checkString('import fooBar, {foo, bar} from "foo-bar";')
).to.have.error.count.equal(2);

expect(
checker.checkString('import {foo as bar, bar as foo} from "foo-bar";')
).to.have.error.count.equal(2);
});
});
});

0 comments on commit e8b9a64

Please sign in to comment.