-
Notifications
You must be signed in to change notification settings - Fork 1
/
enum.js
34 lines (30 loc) · 1.13 KB
/
enum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';
const path = require('path');
const util = require('./util');
exports.rule = {
meta: {
docs: {
description: 'require that enums appear in dedicated modules (unless private)'
}
},
create: function(context) {
const sourceRoot = path.join(process.cwd(), 'src');
return {
AssignmentExpression: function(expression) {
const comments = expression.parent.leadingComments;
if (comments && expression.left.type === 'MemberExpression') {
const comment = comments[comments.length - 1];
if (comment.value.indexOf('* @enum {') >= 0 && comment.value.indexOf('* @private') === -1) {
const filePath = context.getFilename();
const requirePath = path.relative(sourceRoot, filePath);
const name = util.getName(expression.left);
const expectedPath = name.split('.').join(path.sep).toLowerCase() + '.js';
if (requirePath.toLowerCase() !== expectedPath) {
context.report(expression, `Expected enum to be in a module named like the enum ('${expectedPath}')`);
}
}
}
}
};
}
};