-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
prefer-namespace-keyword.ts
53 lines (50 loc) · 1.38 KB
/
prefer-namespace-keyword.ts
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
import {
AST_NODE_TYPES,
AST_TOKEN_TYPES,
} from '@typescript-eslint/experimental-utils';
import * as util from '../util';
export default util.createRule({
name: 'prefer-namespace-keyword',
meta: {
type: 'suggestion',
docs: {
description:
'Require the use of the `namespace` keyword instead of the `module` keyword to declare custom TypeScript modules',
category: 'Best Practices',
recommended: 'error',
},
fixable: 'code',
messages: {
useNamespace:
"Use 'namespace' instead of 'module' to declare custom TypeScript modules.",
},
schema: [],
},
defaultOptions: [],
create(context) {
const sourceCode = context.getSourceCode();
return {
TSModuleDeclaration(node): void {
// Do nothing if the name is a string.
if (!node.id || node.id.type === AST_NODE_TYPES.Literal) {
return;
}
// Get tokens of the declaration header.
const moduleType = sourceCode.getTokenBefore(node.id);
if (
moduleType &&
moduleType.type === AST_TOKEN_TYPES.Identifier &&
moduleType.value === 'module'
) {
context.report({
node,
messageId: 'useNamespace',
fix(fixer) {
return fixer.replaceText(moduleType, 'namespace');
},
});
}
},
};
},
});