Skip to content

Commit 26e7e70

Browse files
committed
Relax UndesirableFunctions
1 parent e78aebe commit 26e7e70

File tree

5 files changed

+41
-20
lines changed

5 files changed

+41
-20
lines changed

dist/npm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "whine",
3-
"version": "0.0.25",
3+
"version": "0.0.26",
44
"description": "PureScript linter, extensible, with configurable rules, and one-off escape hatches",
55
"keywords": ["purescript", "lint"],
66
"author": "Fyodor Soikin <name.fa@gmail.com>",

dist/vscode-extension/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"publisher": "collegevine",
44
"displayName": "Whine at PureScript",
55
"description": "PureScript linter, extensible, with configurable rules, and one-off escape hatches",
6-
"version": "0.0.25",
6+
"version": "0.0.26",
77
"repository": "https://github.com/collegevine/purescript-whine",
88
"engines": {
99
"vscode": "^1.95.0"

spago.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ package:
5151
dependencies:
5252
- spec-node
5353
publish:
54-
version: 0.0.25
54+
version: 0.0.26
5555
license: MIT
5656
location:
5757
githubOwner: collegevine

src/Whine/Core/UndesirableFunctions.purs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,26 @@ rule badFunctions = emptyRule { onExpr = onExpr }
5959
| Just mods <- Map.lookup { function } badFunctions -> -- This function is on the list of undesirables.
6060
currentModule \m -> do
6161
let report message = reportViolation { source: Just $ rangeOf e, message }
62-
case findImport m mod function of
62+
importedFrom = explicitOrQualifiedImport m mod function <|> loneOpenImport m
63+
case importedFrom of
6364
Just imprt -> do -- Found whence this function is imported.
6465
let message =
6566
Map.lookup (Just imprt) mods -- See if we have a message for this specific import.
6667
<|> Map.lookup Nothing mods -- If not, see if we have a module-agnostic message for this function.
6768
report `traverse_` message
69+
6870
Nothing -> -- Couldn't find this function in any imports. It could have been imported via an "open" import.
69-
case Map.lookup Nothing mods of
70-
Just message -> -- Found a module-agnostic message for this function => report it.
71-
report message
72-
Nothing -> -- No message found for this function => report all messages just in case.
73-
report `traverse_` mods
71+
for_ (Map.lookup Nothing mods) \message -> -- Report a module-agnostic message for this function, if defined.
72+
report message
7473

7574
_ -> do
7675
pure unit
7776

7877
-- Look through imports in the header of the module and find one that either
7978
-- (1) is qualified with the given qualifier `mod` or (2) lists the given
8079
-- identifier `ident` among imported values or operators.
81-
findImport :: e. Module e -> Maybe ModuleName -> String -> Maybe ModuleName
82-
findImport (Module { header: ModuleHeader { imports } }) mod ident =
80+
explicitOrQualifiedImport :: e. Module e -> Maybe ModuleName -> String -> Maybe ModuleName
81+
explicitOrQualifiedImport (Module { header: ModuleHeader { imports } }) mod ident =
8382
imports # findMap \(ImportDecl i@{ module: Name { name } }) -> case i of
8483
{ qualified: Just (_ /\ Name { name: qualifier }) }
8584
| Just qualifier == mod
@@ -96,6 +95,18 @@ rule badFunctions = emptyRule { onExpr = onExpr }
9695
sameIdentifier (ImportOp (Name { name: Operator op })) = op == ident
9796
sameIdentifier _ = false
9897

98+
loneOpenImport :: e. Module e -> Maybe ModuleName
99+
loneOpenImport (Module { header: ModuleHeader { imports } }) =
100+
let candidates =
101+
imports # mapMaybe \(ImportDecl i@{ module: Name { name } }) -> case i of
102+
{ qualified: Nothing, names: Nothing } -> Just name
103+
_ -> Nothing
104+
105+
in
106+
case candidates of
107+
[name] -> Just name
108+
_ -> Nothing
109+
99110

100111
codec :: CJ.Codec Args
101112
codec =

test/Core/UndesirableFunctions.purs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,31 @@ spec = describe "UndesirableFunctions" do
3434
x = y fn3
3535
"""
3636

37+
it "Assumes open-imported module if there is only one" do
38+
hasViolations ["2:6-2:9" /\ "Do not use fn3 from Bad.Module"] """
39+
import Bad.Module
40+
import Another.Module (fn2)
41+
x = y fn3
42+
"""
43+
44+
it "Does not assume open-imported module if there are several of them" do
45+
hasViolations [] """
46+
import Bad.Module
47+
import Another.Module
48+
x = y fn2
49+
"""
50+
3751
it "Falls back to unqualified message when the import is open" do
3852
hasViolations ["1:6-1:9" /\ "Do not use fn3"] """
3953
import Some
4054
x = y fn3
4155
"""
4256

43-
it "Reports all messages when no import and no unqualified message" do
44-
hasViolations
45-
[ "1:6-1:9" /\ "Do not use fn2 from Another.Module"
46-
, "1:6-1:9" /\ "Do not use fn2 from Bad.Module"
47-
]
48-
"""
49-
import Some
50-
x = y fn2
51-
"""
57+
it "Does not report when no import and no unqualified message" do
58+
hasViolations [] """
59+
import Some
60+
x = y fn2
61+
"""
5262

5363
it "Reports when imported with a qualifier" do
5464
hasViolations

0 commit comments

Comments
 (0)