Permalink
Jump to Line
Branch:
master
Switch branches/tags
dollar-ignores
feature/new-rule-require-new-lines-in-array
gh-pages
issues/1242
issues/1271-related
master
mdevils/cst
Nothing to show
Nothing to show
Fetching contributors…
![]()
Cannot retrieve contributors at this time
| /** | |
| * Requires space before `()` or `{}` in function declarations. | |
| * | |
| * Type: `Object` | |
| * | |
| * Values: `"beforeOpeningRoundBrace"` and `"beforeOpeningCurlyBrace"` as child properties. | |
| * Child properties must be set to `true`. | |
| * | |
| * #### Example | |
| * | |
| * ```js | |
| * "requireSpacesInFunctionDeclaration": { | |
| * "beforeOpeningRoundBrace": true, | |
| * "beforeOpeningCurlyBrace": true | |
| * } | |
| * ``` | |
| * | |
| * ##### Valid | |
| * | |
| * ```js | |
| * function a () {} | |
| * ``` | |
| * | |
| * ##### Invalid | |
| * | |
| * ```js | |
| * function a() {} | |
| * function a (){} | |
| * function a(){} | |
| * ``` | |
| */ | |
| var assert = require('assert'); | |
| module.exports = function() {}; | |
| module.exports.prototype = { | |
| configure: function(options) { | |
| assert( | |
| typeof options === 'object', | |
| this.getOptionName() + ' option must be the object' | |
| ); | |
| if ('beforeOpeningRoundBrace' in options) { | |
| assert( | |
| options.beforeOpeningRoundBrace === true, | |
| this.getOptionName() + '.beforeOpeningRoundBrace ' + | |
| 'property requires true value or should be removed' | |
| ); | |
| } | |
| if ('beforeOpeningCurlyBrace' in options) { | |
| assert( | |
| options.beforeOpeningCurlyBrace === true, | |
| this.getOptionName() + '.beforeOpeningCurlyBrace ' + | |
| 'property requires true value or should be removed' | |
| ); | |
| } | |
| assert( | |
| options.beforeOpeningCurlyBrace || options.beforeOpeningRoundBrace, | |
| this.getOptionName() + ' must have beforeOpeningCurlyBrace or beforeOpeningRoundBrace property' | |
| ); | |
| this._beforeOpeningRoundBrace = Boolean(options.beforeOpeningRoundBrace); | |
| this._beforeOpeningCurlyBrace = Boolean(options.beforeOpeningCurlyBrace); | |
| }, | |
| getOptionName: function() { | |
| return 'requireSpacesInFunctionDeclaration'; | |
| }, | |
| check: function(file, errors) { | |
| var beforeOpeningRoundBrace = this._beforeOpeningRoundBrace; | |
| var beforeOpeningCurlyBrace = this._beforeOpeningCurlyBrace; | |
| file.iterateNodesByType(['FunctionDeclaration'], function(node) { | |
| // Exception for `export default function` #1376 | |
| if (!node.id) { | |
| return; | |
| } | |
| if (beforeOpeningRoundBrace) { | |
| // for a named function, use node.id | |
| var functionToken = file.getFirstNodeToken(node.id || node); | |
| errors.assert.whitespaceBetween({ | |
| token: functionToken, | |
| nextToken: file.getNextToken(functionToken), | |
| message: 'Missing space before opening round brace' | |
| }); | |
| } | |
| if (beforeOpeningCurlyBrace) { | |
| var bodyToken = file.getFirstNodeToken(node.body); | |
| errors.assert.whitespaceBetween({ | |
| token: file.getPrevToken(bodyToken), | |
| nextToken: bodyToken, | |
| message: 'Missing space before opening curly brace' | |
| }); | |
| } | |
| }); | |
| } | |
| }; |