This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
257 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
lib/rules/disallow-spaces-inside-imported-object-braces.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
test/specs/rules/disallow-spaces-inside-imported-object-braces.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
40
test/specs/rules/require-spaces-inside-imported-object-braces.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); |