1010
1111module . exports = function ( context ) {
1212 var spaced = context . options [ 0 ] === 'always' ;
13+ var multiline = context . options [ 1 ] ? context . options [ 1 ] . allowMultiline : true ;
1314
1415 // --------------------------------------------------------------------------
1516 // Helpers
1617 // --------------------------------------------------------------------------
1718
1819 /**
19- * Determines whether two adjacent tokens are have whitespace between them.
20+ * Determines whether two adjacent tokens have a newline between them.
21+ * @param {Object } left - The left token object.
22+ * @param {Object } right - The right token object.
23+ * @returns {boolean } Whether or not there is a newline between the tokens.
24+ */
25+ function isMultiline ( left , right ) {
26+ return left . loc . start . line !== right . loc . start . line ;
27+ }
28+
29+ /**
30+ * Determines whether two adjacent tokens have whitespace between them.
2031 * @param {Object } left - The left token object.
2132 * @param {Object } right - The right token object.
2233 * @returns {boolean } Whether or not there is space between the tokens.
@@ -25,6 +36,28 @@ module.exports = function(context) {
2536 return left . range [ 1 ] < right . range [ 0 ] ;
2637 }
2738
39+ /**
40+ * Reports that there shouldn't be a newline after the first token
41+ * @param {ASTNode } node - The node to report in the event of an error.
42+ * @param {Token } token - The token to use for the report.
43+ * @returns {void }
44+ */
45+ function reportNoBeginningNewline ( node , token ) {
46+ context . report ( node , token . loc . start ,
47+ 'There should be no newline after \'' + token . value + '\'' ) ;
48+ }
49+
50+ /**
51+ * Reports that there shouldn't be a newline before the last token
52+ * @param {ASTNode } node - The node to report in the event of an error.
53+ * @param {Token } token - The token to use for the report.
54+ * @returns {void }
55+ */
56+ function reportNoEndingNewline ( node , token ) {
57+ context . report ( node , token . loc . start ,
58+ 'There should be no newline before \'' + token . value + '\'' ) ;
59+ }
60+
2861 /**
2962 * Reports that there shouldn't be a space after the first token
3063 * @param {ASTNode } node - The node to report in the event of an error.
@@ -79,16 +112,28 @@ module.exports = function(context) {
79112 * @returns {void }
80113 */
81114 function validateBraceSpacing ( node , first , second , penultimate , last ) {
82- if ( spaced && ! isSpaced ( first , second ) ) {
83- reportRequiredBeginningSpace ( node , first ) ;
115+ if ( spaced ) {
116+ if ( ! isSpaced ( first , second ) ) {
117+ reportRequiredBeginningSpace ( node , first ) ;
118+ } else if ( ! multiline && isMultiline ( first , second ) ) {
119+ reportNoBeginningNewline ( node , first ) ;
120+ }
121+
122+ if ( ! isSpaced ( penultimate , last ) ) {
123+ reportRequiredEndingSpace ( node , last ) ;
124+ } else if ( ! multiline && isMultiline ( penultimate , last ) ) {
125+ reportNoEndingNewline ( node , last ) ;
126+ }
127+
128+ return ;
84129 }
85- if ( ! spaced && isSpaced ( first , second ) ) {
130+
131+ // "never" setting if we get here.
132+ if ( isSpaced ( first , second ) && ! ( multiline && isMultiline ( first , second ) ) ) {
86133 reportNoBeginningSpace ( node , first ) ;
87134 }
88- if ( spaced && ! isSpaced ( penultimate , last ) ) {
89- reportRequiredEndingSpace ( node , last ) ;
90- }
91- if ( ! spaced && isSpaced ( penultimate , last ) ) {
135+
136+ if ( isSpaced ( penultimate , last ) && ! ( multiline && isMultiline ( penultimate , last ) ) ) {
92137 reportNoEndingSpace ( node , last ) ;
93138 }
94139 }
@@ -111,4 +156,12 @@ module.exports = function(context) {
111156
112157module . exports . schema = [ {
113158 enum : [ 'always' , 'never' ]
159+ } , {
160+ type : 'object' ,
161+ properties : {
162+ allowMultiline : {
163+ type : 'boolean'
164+ }
165+ } ,
166+ additionalProperties : false
114167} ] ;
0 commit comments