|
| 1 | +module dscanner.analysis.nolint; |
| 2 | + |
| 3 | +@safe: |
| 4 | + |
| 5 | +import dparse.ast; |
| 6 | +import dparse.lexer; |
| 7 | + |
| 8 | +import std.algorithm : canFind; |
| 9 | +import std.regex : matchAll, regex; |
| 10 | +import std.string : lastIndexOf, strip; |
| 11 | +import std.typecons; |
| 12 | + |
| 13 | +struct NoLint |
| 14 | +{ |
| 15 | + bool containsCheck(scope const(char)[] check) const |
| 16 | + { |
| 17 | + while (true) |
| 18 | + { |
| 19 | + if (disabledChecks.get((() @trusted => cast(string) check)(), 0) > 0) |
| 20 | + return true; |
| 21 | + |
| 22 | + auto dot = check.lastIndexOf('.'); |
| 23 | + if (dot == -1) |
| 24 | + break; |
| 25 | + check = check[0 .. dot]; |
| 26 | + } |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + // automatic pop when returned value goes out of scope |
| 31 | + Poppable push(in Nullable!NoLint other) scope |
| 32 | + { |
| 33 | + if (other.isNull) |
| 34 | + return Poppable(null); |
| 35 | + |
| 36 | + foreach (key, value; other.get.getDisabledChecks) |
| 37 | + this.disabledChecks[key] += value; |
| 38 | + |
| 39 | + return Poppable(() => this.pop(other)); |
| 40 | + } |
| 41 | + |
| 42 | +package: |
| 43 | + const(int[string]) getDisabledChecks() const |
| 44 | + { |
| 45 | + return this.disabledChecks; |
| 46 | + } |
| 47 | + |
| 48 | + void pushCheck(in string check) |
| 49 | + { |
| 50 | + disabledChecks[check]++; |
| 51 | + } |
| 52 | + |
| 53 | + void merge(in Nullable!NoLint other) |
| 54 | + { |
| 55 | + if (other.isNull) |
| 56 | + return; |
| 57 | + |
| 58 | + foreach (key, value; other.get.getDisabledChecks) |
| 59 | + this.disabledChecks[key] += value; |
| 60 | + } |
| 61 | + |
| 62 | +private: |
| 63 | + void pop(in Nullable!NoLint other) |
| 64 | + { |
| 65 | + if (other.isNull) |
| 66 | + return; |
| 67 | + |
| 68 | + foreach (key, value; other.get.getDisabledChecks) |
| 69 | + { |
| 70 | + assert(this.disabledChecks.get(key, 0) >= value); |
| 71 | + |
| 72 | + this.disabledChecks[key] -= value; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + static struct Poppable |
| 77 | + { |
| 78 | + ~this() |
| 79 | + { |
| 80 | + if (onPop) |
| 81 | + onPop(); |
| 82 | + onPop = null; |
| 83 | + } |
| 84 | + |
| 85 | + private: |
| 86 | + void delegate() onPop; |
| 87 | + } |
| 88 | + |
| 89 | + int[string] disabledChecks; |
| 90 | +} |
| 91 | + |
| 92 | +struct NoLintFactory |
| 93 | +{ |
| 94 | + static Nullable!NoLint fromModuleDeclaration(in ModuleDeclaration moduleDeclaration) |
| 95 | + { |
| 96 | + NoLint noLint; |
| 97 | + |
| 98 | + foreach (atAttribute; moduleDeclaration.atAttributes) |
| 99 | + noLint.merge(NoLintFactory.fromAtAttribute(atAttribute)); |
| 100 | + |
| 101 | + if (!noLint.getDisabledChecks.length) |
| 102 | + return nullNoLint; |
| 103 | + |
| 104 | + return noLint.nullable; |
| 105 | + } |
| 106 | + |
| 107 | + static Nullable!NoLint fromDeclaration(in Declaration declaration) |
| 108 | + { |
| 109 | + NoLint noLint; |
| 110 | + foreach (attribute; declaration.attributes) |
| 111 | + noLint.merge(NoLintFactory.fromAttribute(attribute)); |
| 112 | + |
| 113 | + if (!noLint.getDisabledChecks.length) |
| 114 | + return nullNoLint; |
| 115 | + |
| 116 | + return noLint.nullable; |
| 117 | + } |
| 118 | + |
| 119 | +private: |
| 120 | + static Nullable!NoLint fromAttribute(const(Attribute) attribute) |
| 121 | + { |
| 122 | + if (attribute is null) |
| 123 | + return nullNoLint; |
| 124 | + |
| 125 | + return NoLintFactory.fromAtAttribute(attribute.atAttribute); |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + static Nullable!NoLint fromAtAttribute(const(AtAttribute) atAttribute) |
| 130 | + { |
| 131 | + if (atAttribute is null) |
| 132 | + return nullNoLint; |
| 133 | + |
| 134 | + auto ident = atAttribute.identifier; |
| 135 | + auto argumentList = atAttribute.argumentList; |
| 136 | + |
| 137 | + if (argumentList !is null) |
| 138 | + { |
| 139 | + if (ident.text.length) |
| 140 | + return NoLintFactory.fromStructUda(ident, argumentList); |
| 141 | + else |
| 142 | + return NoLintFactory.fromStringUda(argumentList); |
| 143 | + |
| 144 | + } |
| 145 | + else |
| 146 | + return nullNoLint; |
| 147 | + } |
| 148 | + |
| 149 | + // @nolint("..") |
| 150 | + static Nullable!NoLint fromStructUda(in Token ident, in ArgumentList argumentList) |
| 151 | + in (ident.text.length && argumentList !is null) |
| 152 | + { |
| 153 | + if (ident.text != "nolint") |
| 154 | + return nullNoLint; |
| 155 | + |
| 156 | + NoLint noLint; |
| 157 | + |
| 158 | + foreach (nodeExpr; argumentList.items) |
| 159 | + { |
| 160 | + if (auto unaryExpr = cast(const UnaryExpression) nodeExpr) |
| 161 | + { |
| 162 | + auto primaryExpression = unaryExpr.primaryExpression; |
| 163 | + if (primaryExpression is null) |
| 164 | + continue; |
| 165 | + |
| 166 | + if (primaryExpression.primary != tok!"stringLiteral") |
| 167 | + continue; |
| 168 | + |
| 169 | + noLint.pushCheck(primaryExpression.primary.text.strip("\"")); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + if (!noLint.getDisabledChecks().length) |
| 174 | + return nullNoLint; |
| 175 | + |
| 176 | + return noLint.nullable; |
| 177 | + } |
| 178 | + |
| 179 | + // @("nolint(..)") |
| 180 | + static Nullable!NoLint fromStringUda(in ArgumentList argumentList) |
| 181 | + in (argumentList !is null) |
| 182 | + { |
| 183 | + NoLint noLint; |
| 184 | + |
| 185 | + foreach (nodeExpr; argumentList.items) |
| 186 | + { |
| 187 | + if (auto unaryExpr = cast(const UnaryExpression) nodeExpr) |
| 188 | + { |
| 189 | + auto primaryExpression = unaryExpr.primaryExpression; |
| 190 | + if (primaryExpression is null) |
| 191 | + continue; |
| 192 | + |
| 193 | + if (primaryExpression.primary != tok!"stringLiteral") |
| 194 | + continue; |
| 195 | + |
| 196 | + auto str = primaryExpression.primary.text.strip("\""); |
| 197 | + Nullable!NoLint currNoLint = NoLintFactory.fromString(str); |
| 198 | + noLint.merge(currNoLint); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + if (!noLint.getDisabledChecks().length) |
| 203 | + return nullNoLint; |
| 204 | + |
| 205 | + return noLint.nullable; |
| 206 | + |
| 207 | + } |
| 208 | + |
| 209 | + // Transform a string with form "nolint(abc, efg)" |
| 210 | + // into a NoLint struct |
| 211 | + static Nullable!NoLint fromString(in string str) |
| 212 | + { |
| 213 | + static immutable re = regex(`[\w-_.]+`, "g"); |
| 214 | + auto matches = matchAll(str, re); |
| 215 | + |
| 216 | + if (!matches) |
| 217 | + return nullNoLint; |
| 218 | + |
| 219 | + const udaName = matches.hit; |
| 220 | + if (udaName != "nolint") |
| 221 | + return nullNoLint; |
| 222 | + |
| 223 | + matches.popFront; |
| 224 | + |
| 225 | + NoLint noLint; |
| 226 | + |
| 227 | + while (matches) |
| 228 | + { |
| 229 | + noLint.pushCheck(matches.hit); |
| 230 | + matches.popFront; |
| 231 | + } |
| 232 | + |
| 233 | + if (!noLint.getDisabledChecks.length) |
| 234 | + return nullNoLint; |
| 235 | + |
| 236 | + return noLint.nullable; |
| 237 | + } |
| 238 | + |
| 239 | + static nullNoLint = Nullable!NoLint.init; |
| 240 | +} |
| 241 | + |
| 242 | +unittest |
| 243 | +{ |
| 244 | + const s1 = "nolint(abc)"; |
| 245 | + const s2 = "nolint(abc, efg, hij)"; |
| 246 | + const s3 = " nolint ( abc , efg ) "; |
| 247 | + const s4 = "nolint(dscanner.style.abc_efg-ijh)"; |
| 248 | + const s5 = "OtherUda(abc)"; |
| 249 | + const s6 = "nolint(dscanner)"; |
| 250 | + |
| 251 | + assert(NoLintFactory.fromString(s1).get.containsCheck("abc")); |
| 252 | + |
| 253 | + assert(NoLintFactory.fromString(s2).get.containsCheck("abc")); |
| 254 | + assert(NoLintFactory.fromString(s2).get.containsCheck("efg")); |
| 255 | + assert(NoLintFactory.fromString(s2).get.containsCheck("hij")); |
| 256 | + |
| 257 | + assert(NoLintFactory.fromString(s3).get.containsCheck("abc")); |
| 258 | + assert(NoLintFactory.fromString(s3).get.containsCheck("efg")); |
| 259 | + |
| 260 | + assert(NoLintFactory.fromString(s4).get.containsCheck("dscanner.style.abc_efg-ijh")); |
| 261 | + |
| 262 | + assert(NoLintFactory.fromString(s5).isNull); |
| 263 | + |
| 264 | + assert(NoLintFactory.fromString(s6).get.containsCheck("dscanner")); |
| 265 | + assert(!NoLintFactory.fromString(s6).get.containsCheck("dscanner2")); |
| 266 | + assert(NoLintFactory.fromString(s6).get.containsCheck("dscanner.foo")); |
| 267 | + |
| 268 | + import std.stdio : stderr, writeln; |
| 269 | + |
| 270 | + (() @trusted => stderr.writeln("Unittest for NoLint passed."))(); |
| 271 | +} |
0 commit comments