-
Notifications
You must be signed in to change notification settings - Fork 167
/
no_namespace.rs
177 lines (159 loc) · 3.51 KB
/
no_namespace.rs
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::Program;
use deno_ast::view::NodeTrait;
use deno_ast::{view as ast_view, MediaType, SourceRanged};
#[derive(Debug)]
pub struct NoNamespace;
const CODE: &str = "no-namespace";
const MESSAGE: &str = "TypeScript's `module` and `namespace` are discouraged to
use";
const HINT: &str = "Use ES2015 module syntax (`import`/`export`) to organize
the code instead";
impl LintRule for NoNamespace {
fn tags(&self) -> &'static [&'static str] {
&["recommended"]
}
fn code(&self) -> &'static str {
CODE
}
fn lint_program_with_ast_view(
&self,
context: &mut Context,
program: Program<'_>,
) {
if matches!(context.media_type(), MediaType::Dts) {
return;
}
NoNamespaceHandler.traverse(program, context);
}
#[cfg(feature = "docs")]
fn docs(&self) -> &'static str {
include_str!("../../docs/rules/no_namespace.md")
}
}
struct NoNamespaceHandler;
impl Handler for NoNamespaceHandler {
fn ts_module_decl(
&mut self,
module_decl: &ast_view::TsModuleDecl,
ctx: &mut Context,
) {
fn inside_ambient_context(current_node: ast_view::Node) -> bool {
use deno_ast::view::Node::*;
match current_node {
TsModuleDecl(module_decl) if module_decl.declare() => true,
_ => match current_node.parent() {
Some(p) => inside_ambient_context(p),
None => false,
},
}
}
if !inside_ambient_context(module_decl.as_node()) {
ctx.add_diagnostic_with_hint(module_decl.range(), CODE, MESSAGE, HINT);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_namespace_valid() {
assert_lint_ok! {
NoNamespace,
filename: "file:///foo.ts",
r#"declare global {}"#,
r#"declare module 'foo' {}"#,
r#"declare module foo {}"#,
r#"declare namespace foo {}"#,
r#"
declare global {
namespace foo {}
}
"#,
r#"
declare module foo {
namespace bar {}
}
"#,
r#"
declare global {
namespace foo {
namespace bar {}
}
}
"#,
r#"
declare namespace foo {
namespace bar {
namespace baz {}
}
}
"#,
};
assert_lint_ok! {
NoNamespace,
filename: "file:///test.d.ts",
r#"namespace foo {}"#,
r#"module foo {}"#,
// https://github.com/denoland/deno_lint/issues/633
r#"
export declare namespace Utility {
export namespace Matcher {
export type CharSchema<
T extends string,
Schema extends string,
_Rest extends string = T
> = _Rest extends `${infer $First}${infer $Rest}`
? $First extends Schema
? CharSchema<T, Schema, $Rest>
: never
: "" extends _Rest
? T
: never;
}
}
"#,
};
}
#[test]
fn no_namespace_invalid() {
assert_lint_err! {
NoNamespace,
"module foo {}": [
{
col: 0,
message: MESSAGE,
hint: HINT,
},
],
"namespace foo {}": [
{
col: 0,
message: MESSAGE,
hint: HINT,
}
],
"namespace Foo.Bar {}": [
{
col: 0,
message: MESSAGE,
hint: HINT,
}
],
"namespace Foo.Bar { namespace Baz.Bas {} }": [
{
col: 0,
message: MESSAGE,
hint: HINT,
},
{
col: 20,
message: MESSAGE,
hint: HINT,
},
],
};
}
}