-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
no-namespace.ts
73 lines (69 loc) · 1.69 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
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();
return {
"TSModuleDeclaration[global!=true][id.type='Identifier']"(
node: TSESTree.TSModuleDeclaration,
) {
if (
(node.parent &&
node.parent.type === AST_NODE_TYPES.TSModuleDeclaration) ||
(allowDefinitionFiles && util.isDefinitionFile(filename)) ||
(allowDeclarations && node.declare === true)
) {
return;
}
context.report({
node,
messageId: 'moduleSyntaxIsPreferred',
});
},
};
},
});