-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
no-namespace.ts
84 lines (77 loc) · 2.09 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
82
83
84
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/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 TypeScript namespaces',
recommended: 'error',
},
messages: {
moduleSyntaxIsPreferred:
'ES2015 module syntax is preferred over namespaces.',
},
schema: [
{
type: 'object',
properties: {
allowDeclarations: {
description:
'Whether to allow `declare` with custom TypeScript namespaces.',
type: 'boolean',
},
allowDefinitionFiles: {
description:
'Whether to allow `declare` with custom TypeScript namespaces inside definition files.',
type: 'boolean',
},
},
additionalProperties: false,
},
],
},
defaultOptions: [
{
allowDeclarations: false,
allowDefinitionFiles: true,
},
],
create(context, [{ allowDeclarations, allowDefinitionFiles }]) {
const filename = context.getFilename();
function isDeclaration(node: TSESTree.Node): boolean {
if (
node.type === AST_NODE_TYPES.TSModuleDeclaration &&
node.declare === true
) {
return true;
}
return node.parent != null && isDeclaration(node.parent);
}
return {
"TSModuleDeclaration[global!=true][id.type='Identifier']"(
node: TSESTree.TSModuleDeclaration,
): void {
if (
node.parent.type === AST_NODE_TYPES.TSModuleDeclaration ||
(allowDefinitionFiles && util.isDefinitionFile(filename)) ||
(allowDeclarations && isDeclaration(node))
) {
return;
}
context.report({
node,
messageId: 'moduleSyntaxIsPreferred',
});
},
};
},
});