-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapitalize-modules.js
110 lines (103 loc) · 2.84 KB
/
capitalize-modules.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// @ts-check
/**
* Heavily inspired by @hapi/eslint-plugin https://github.com/hapijs/eslint-plugin/blob/d8af8ff3450809873814b60be9a03558abd02cbe/lib/rules/capitalize-modules.js
* Copyright (c) 2019-2020, Sideway Inc, and project contributors
* Copyright (c) 2015-2019 Continuation Labs and Colin J. Ihrig All rights reserved.
* Copyright (c) 2021 Type of Web - Michał Miszczyszyn
*/
/**
* @typedef {import('estree').AssignmentExpression | import('estree').VariableDeclarator | import('estree').ImportDeclaration} CheckNodeArgument
*/
/**
* @param {string} name
*/
const isCapitalized = (name) => {
const firstChar = name.charAt(0);
return firstChar === firstChar.toUpperCase();
};
/**
* @param {import('estree').Expression} node
*/
const isRequire = (node) => {
return (
node !== null &&
node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
node.callee.name === 'require'
);
};
/**
* @param {import('estree').ImportDeclaration} node
*/
const getNotCapitalizedImports = (node) => {
return (
node !== null &&
node.specifiers
.filter(
(specifier) =>
(specifier.type === 'ImportDefaultSpecifier' || specifier.type === 'ImportNamespaceSpecifier') &&
!isCapitalized(specifier.local.name),
)
.map((s) => s.local)
);
};
/**
* @param {import('eslint').Rule.RuleContext} context
*/
const check =
(context) =>
/**
* @param {CheckNodeArgument} node
*/
(node) => {
if (
node.type === 'VariableDeclarator' &&
node.id.type === 'Identifier' &&
isRequire(node.init) &&
!isCapitalized(node.id.name)
) {
context.report({ node, messageId: 'notCapitalized' });
} else if (
node.type === 'AssignmentExpression' &&
isRequire(node.right) &&
node.left.type === 'Identifier' &&
!isCapitalized(node.left.name)
) {
context.report({ node, messageId: 'notCapitalized' });
} else if (node.type === 'ImportDeclaration') {
const notCapitalized = getNotCapitalizedImports(node);
notCapitalized.forEach((node) =>
context.report({
node,
messageId: 'notCapitalized',
fix: (fixer) => fixer.replaceTextRange([node.range[0], node.range[0] + 1], node.name.charAt(0).toUpperCase()),
}),
);
}
};
/**
* @type {import('eslint').Rule.RuleModule}
*/
const rule = {
meta: {
type: 'suggestion',
docs: {
description: 'enforce the capitalization of imported module variables',
category: 'Stylistic Issues',
recommended: true,
},
fixable: 'code',
messages: {
notCapitalized: 'Imported module name is not capitalized.',
},
},
create(context) {
const fn = check(context);
return {
AssignmentExpression: fn,
VariableDeclarator: fn,
ImportDeclaration: fn,
};
},
};
module.exports = rule;