/
no-namespace.ts
81 lines (76 loc) · 1.95 KB
/
no-namespace.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
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
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as util from '../util';
type Options = [
{
allowDeclarations?: boolean;
allowDefinitionFiles?: boolean;
},
];
type MessageIds = 'moduleSyntaxIsPreferred';
export default util.createRule<Options, MessageIds>({
name: 'no-namespace',
meta: {
type: 'suggestion',
docs: {
description:
'Disallow the use of custom TypeScript modules and namespaces',
category: 'Best Practices',
recommended: 'error',
},
messages: {
moduleSyntaxIsPreferred:
'ES2015 module syntax is preferred over custom TypeScript modules and namespaces.',
},
schema: [
{
type: 'object',
properties: {
allowDeclarations: {
type: 'boolean',
},
allowDefinitionFiles: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
},
defaultOptions: [
{
allowDeclarations: false,
allowDefinitionFiles: true,
},
],
create(context, [{ allowDeclarations, allowDefinitionFiles }]) {
const filename = context.getFilename();
function isDeclaration(node: TSESTree.TSModuleDeclaration): boolean {
return (
node.declare === true ||
(node.parent!.parent?.type === AST_NODE_TYPES.TSModuleDeclaration &&
isDeclaration(node.parent!.parent))
);
}
return {
"TSModuleDeclaration[global!=true][id.type='Identifier']"(
node: TSESTree.TSModuleDeclaration,
): void {
if (
(node.parent &&
node.parent.type === AST_NODE_TYPES.TSModuleDeclaration) ||
(allowDefinitionFiles && util.isDefinitionFile(filename)) ||
(allowDeclarations && isDeclaration(node))
) {
return;
}
context.report({
node,
messageId: 'moduleSyntaxIsPreferred',
});
},
};
},
});