Skip to content

Commit d458e69

Browse files
author
[Interfaced] Кирилл Дронкин
committed
1.1.2
1 parent c0d2c26 commit d458e69

5 files changed

Lines changed: 109 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change log
22

3+
## 1.1.2 (release date: 2.08.2018)
4+
5+
* `lines-around-class`: handle nested classes
6+
37
## 1.1.1 (release date: 30.07.2018)
48

59
* Use public fork of Doctrine instead of local one

lib/rules/lines-around-class.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,28 @@ module.exports = {
3535
const sourceCode = context.getSourceCode();
3636

3737
/**
38+
* @param {Scope} scope
39+
* @return {Array<ASTNode>}
40+
*/
41+
function getScopeBody(scope) {
42+
return scope.type === 'function' ? scope.block.body.body : scope.block.body;
43+
}
44+
45+
/**
46+
* @param {Scope} scope
47+
* @return {number}
48+
*/
49+
function getScopeStart(scope) {
50+
return scope.type === 'function' ? scope.block.body.start : scope.block.start;
51+
}
52+
53+
/**
54+
* @param {Scope} upperScope
3855
* @param {ASTNode} classNode
3956
* @return {boolean}
4057
*/
41-
function isCollisionWithAnotherClassBefore(classNode) {
42-
const upperScopeBody = context.getScope().upper.block.body;
58+
function isCollisionBefore(upperScope, classNode) {
59+
const upperScopeBody = getScopeBody(upperScope);
4360
const classIndexInScope = upperScopeBody.indexOf(classNode);
4461
const classJSDocComment = getJSDocComment(classNode, sourceCode);
4562

@@ -56,11 +73,12 @@ module.exports = {
5673
}
5774

5875
/**
76+
* @param {Scope} upperScope
5977
* @param {ASTNode} classNode
6078
* @return {boolean}
6179
*/
62-
function isCollisionWithAnotherClassAfter(classNode) {
63-
const upperScopeBody = context.getScope().upper.block.body;
80+
function isCollisionAfter(upperScope, classNode) {
81+
const upperScopeBody = getScopeBody(upperScope);
6482
const classIndexInScope = upperScopeBody.indexOf(classNode);
6583

6684
const nodeAfterClass = upperScopeBody[classIndexInScope + 1] || null;
@@ -81,24 +99,31 @@ module.exports = {
8199
* @param {ASTNode} classNode
82100
*/
83101
function check(classNode) {
102+
const upperScope = context.getScope().upper;
103+
const upperScopeStart = getScopeStart(upperScope);
84104
const classJSDocComment = getJSDocComment(classNode, sourceCode);
85105

86106
const tokenBeforeClass = sourceCode.getTokenBefore((classJSDocComment || classNode), {
87-
includeComments: true
107+
includeComments: true,
108+
filter(token) {
109+
return token.start > upperScopeStart;
110+
}
88111
});
89112

90113
const tokenAfterClass = sourceCode.getTokenAfter((classNode), {
91-
includeComments: true
114+
includeComments: true,
115+
filter(token) {
116+
return token.start > upperScopeStart;
117+
}
92118
});
93119

94120
if (tokenBeforeClass) {
95-
if (isCollisionWithAnotherClassBefore(classNode) && options.collisionPriority !== 'before') {
121+
if (isCollisionBefore(upperScope, classNode) && options.collisionPriority !== 'before') {
96122
return;
97123
}
98124

99125
const classStartLine = (classJSDocComment || classNode).loc.start.line;
100126
const tokenBeforeClassEndLine = tokenBeforeClass.loc.end.line;
101-
102127
const newlinesAmountBeforeClass = Math.max(classStartLine - tokenBeforeClassEndLine - 1, 0);
103128

104129
if (newlinesAmountBeforeClass !== options.before) {
@@ -122,13 +147,12 @@ module.exports = {
122147
}
123148

124149
if (tokenAfterClass) {
125-
if (isCollisionWithAnotherClassAfter(classNode) && options.collisionPriority !== 'after') {
150+
if (isCollisionAfter(upperScope, classNode) && options.collisionPriority !== 'after') {
126151
return;
127152
}
128153

129154
const classEndLine = classNode.loc.end.line;
130155
const tokenAfterClassStartLine = tokenAfterClass.loc.start.line;
131-
132156
const newlinesAmountAfterClass = Math.max(tokenAfterClassStartLine - classEndLine - 1, 0);
133157

134158
if (newlinesAmountAfterClass !== options.after) {

lib/types.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ let ASTNode;
88
*/
99
let ASTComment;
1010

11+
/**
12+
* @typedef {Object}
13+
*/
14+
let Scope;
15+
1116
/**
1217
* @typedef {Object}
1318
*/

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-interfaced",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "ESLint plugin with rules for the approaches adopted by Interfaced company",
55
"license": "MIT",
66
"author": "Interfaced (http://interfaced.tv)",

test/eslint/rules/lines-around-class.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,38 @@ module.exports = extendToClassExpression({
3535
` */`,
3636
`Klass.CONST = 1;`
3737
)
38+
}, {
39+
options: [{
40+
before: 2,
41+
after: 2
42+
}],
43+
code: concat(
44+
`goog.provide('Klass');`,
45+
``,
46+
``,
47+
`function func() {`,
48+
` class Klass {}`,
49+
``,
50+
``,
51+
` Klass.CONST = 1;`,
52+
`}`
53+
)
54+
}, {
55+
options: [{
56+
before: 2,
57+
after: 2
58+
}],
59+
code: concat(
60+
`goog.provide('Klass');`,
61+
``,
62+
``,
63+
`block: {`,
64+
` class Klass {}`,
65+
``,
66+
``,
67+
` Klass.CONST = 1;`,
68+
`}`
69+
)
3870
}, {
3971
options: [{
4072
before: 2,
@@ -221,5 +253,38 @@ module.exports = extendToClassExpression({
221253
`Amount of newlines before class should be 2, but 1 given.`,
222254
`Amount of newlines after class should be 2, but 1 given.`
223255
)
256+
}, {
257+
options: [{
258+
before: 2,
259+
after: 2
260+
}],
261+
code: concat(
262+
`goog.provide('Klass');`,
263+
``,
264+
`function func() {`,
265+
` // Comment`,
266+
``,
267+
` class Klass {}`,
268+
``,
269+
` Klass.CONST = 1;`,
270+
`}`
271+
),
272+
output: concat(
273+
`goog.provide('Klass');`,
274+
``,
275+
`function func() {`,
276+
` // Comment`,
277+
``,
278+
``,
279+
` class Klass {}`,
280+
``,
281+
``,
282+
` Klass.CONST = 1;`,
283+
`}`
284+
),
285+
errors: errors(
286+
`Amount of newlines before class should be 2, but 1 given.`,
287+
`Amount of newlines after class should be 2, but 1 given.`
288+
)
224289
}]
225290
});

0 commit comments

Comments
 (0)