-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNoModuleOnExposedNames.elm
More file actions
181 lines (127 loc) · 4.8 KB
/
NoModuleOnExposedNames.elm
File metadata and controls
181 lines (127 loc) · 4.8 KB
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
178
179
180
181
module NoModuleOnExposedNames exposing (rule)
{-|
@docs rule
-}
import Elm.Syntax.Import exposing (Import)
import Elm.Syntax.ModuleName exposing (ModuleName)
import Elm.Syntax.Node as Node exposing (Node)
import Elm.Syntax.Range exposing (Range)
import NoModuleOnExposedNames.Context as Context
import Review.Fix as Fix
import Review.Rule as Rule exposing (Error, Rule)
import Vendor.NameVisitor as NameVisitor
{-| Forbid modules on names that have been exposed.
config : List Rule
config =
[ NoModuleOnExposedNames.rule
]
If a function or type has been exposed on import do not allow it to be called via the module name/alias. You should either remove the call from the import exposes or remove the module name from the call.
## Failure
Here `class` is exposed but is still called by `Attr.class`.
import Html.Attributes as Attr exposing (class)
view children =
div [ Attr.class "container" ] children
## Success
Remove the module name from the call:
import Html.Attributes as Attr exposing (class)
view children =
div [ class "container" ] children
## Success
Or remove `class` from `exposing`:
import Html.Attributes as Attr
view children =
div [ Attr.class "container" ] children
## Failure
Here `Attribute` has been exposed but is still called by `Html.Attribute`.
import Html exposing (Attribute, Html)
container : List (Html.Attribute msg) -> List (Html msg) -> Html msg
container attributes children =
div attributes children
## Success
Remove the module name from the call:
import Html exposing (Attribute, Html)
container : List (Attribute msg) -> List (Html msg) -> Html msg
container attributes children =
div attributes children
## Success
Or remove `Attribute` from `exposing`:
import Html exposing (Html)
container : List (Html.Attribute msg) -> List (Html msg) -> Html msg
container attributes children =
div attributes children
-}
rule : Rule
rule =
Rule.newModuleRuleSchema "NoModuleOnExposedNames" Context.initial
|> Rule.withImportVisitor importVisitor
|> NameVisitor.withValueAndTypeVisitors
{ valueVisitor = valueVisitor
, typeVisitor = typeVisitor
}
|> Rule.providesFixesForModuleRule
|> Rule.fromModuleRuleSchema
importVisitor : Node Import -> Context.Module -> ( List (Error {}), Context.Module )
importVisitor node context =
( []
, rememberExposedNames (Node.value node) context
)
rememberExposedNames : Import -> Context.Module -> Context.Module
rememberExposedNames { moduleName, moduleAlias, exposingList } context =
case exposingList of
Nothing ->
context
Just exposes ->
let
moduleNameOrAlias : ModuleName
moduleNameOrAlias =
moduleAlias
|> Maybe.map Node.value
|> Maybe.withDefault (Node.value moduleName)
in
Context.expose moduleNameOrAlias (Node.value exposes) context
valueVisitor : Node ( ModuleName, String ) -> Context.Module -> ( List (Error {}), Context.Module )
valueVisitor node context =
let
( moduleName, name ) =
Node.value node
in
if Context.isFunctionExposed context moduleName name then
( [ moduleOnExposedValueError name (Node.range node) ]
, context
)
else
( [], context )
typeVisitor : Node ( ModuleName, String ) -> Context.Module -> ( List (Error {}), Context.Module )
typeVisitor node context =
let
( moduleName, name ) =
Node.value node
in
if Context.isTypeExposed context moduleName name then
( [ moduleOnExposedTypeError name (Node.range node) ]
, context
)
else
( [], context )
moduleOnExposedValueError : String -> Range -> Error {}
moduleOnExposedValueError name range =
Rule.errorWithFix
{ message = "Module used on exposed value `" ++ name ++ "`."
, details =
[ "It is not necessary to use the module here as `" ++ name ++ "` was exposed on import."
, "You should remove the module from this call, or remove the name from the import .. exposing list."
]
}
range
[ Fix.replaceRangeBy range name ]
moduleOnExposedTypeError : String -> Range -> Error {}
moduleOnExposedTypeError name range =
Rule.errorWithFix
{ message = "Module used on exposed type `" ++ name ++ "`."
, details =
[ "It is not necessary to use the module here as `" ++ name ++ "` was exposed on import."
, "You should remove the module from this call, or remove the name from the import .. exposing list."
]
}
range
[ Fix.replaceRangeBy range name ]