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

Commit 488ec0d

Browse files
schempymarkelog
authored andcommitted
requirePaddingNewLinesBeforeExport: exclude if only statement in block
Fixes #1883 Closes gh-2106
1 parent c9fab40 commit 488ec0d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/rules/require-padding-newlines-before-export.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,22 @@
1717
* var a = 2;
1818
*
1919
* module.exports = a;
20+
*
21+
* if (cond) {
22+
* module.exports = a;
23+
* }
2024
* ```
2125
*
2226
* ##### Invalid
2327
*
2428
* ```js
2529
* var a = 2;
2630
* module.exports = a;
31+
*
32+
* if (cond) {
33+
* foo();
34+
* module.exports = a;
35+
* }
2736
* ```
2837
*/
2938

@@ -47,6 +56,7 @@ module.exports.prototype = {
4756
check: function(file, errors) {
4857
file.iterateNodesByType('AssignmentExpression', function(node) {
4958
var left = node.left;
59+
var greatGrandpa = node.parentNode.parentNode;
5060
if (!(
5161
left.object &&
5262
left.object.name === 'module' &&
@@ -55,6 +65,11 @@ module.exports.prototype = {
5565
return;
5666
}
5767

68+
// module.exports is in a block and is the only statement.
69+
if (greatGrandpa.type === 'BlockStatement' && greatGrandpa.body.length === 1) {
70+
return;
71+
}
72+
5873
var firstToken = file.getFirstNodeToken(node);
5974
var prevToken = file.getPrevToken(firstToken);
6075

@@ -65,6 +80,7 @@ module.exports.prototype = {
6580
message: 'Missing newline before export'
6681
});
6782
});
83+
6884
}
6985

7086
};

test/specs/rules/require-padding-newlines-before-export.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,9 @@ describe('rules/require-padding-newlines-before-export', function() {
3131
expect(checker.checkString('var a = 2;\nfoo.exports = a;')).to.have.no.errors();
3232
expect(checker.checkString('var a = 2;\nmodule.foo = a;')).to.have.no.errors();
3333
});
34+
35+
it('should not report missing padding if export is only statement in block', function() {
36+
expect(checker.checkString('if (true) {\nmodule.exports = a;\n}')).to.have.no.errors();
37+
});
3438
});
3539
});

0 commit comments

Comments
 (0)