|
| 1 | +package checkstyle.checks; |
| 2 | + |
| 3 | +import checkstyle.LintMessage.SeverityLevel; |
| 4 | +import haxeparser.Data; |
| 5 | +import haxe.macro.Expr; |
| 6 | + |
| 7 | +@name("MemberName") |
| 8 | +@desc("Checks on naming conventions of non-static fields") |
| 9 | +class MemberNameCheck extends NameCheckBase { |
| 10 | + |
| 11 | + public static inline var PUBLIC:String = "PUBLIC"; |
| 12 | + public static inline var PRIVATE:String = "PRIVATE"; |
| 13 | + public static inline var ENUM:String = "ENUM"; |
| 14 | + public static inline var TYPEDEF:String = "TYPEDEF"; |
| 15 | + |
| 16 | + public function new() { |
| 17 | + super(); |
| 18 | + severity = "ERROR"; |
| 19 | + format = "^[a-z]+[a-zA-Z0-9_]*$"; |
| 20 | + } |
| 21 | + |
| 22 | + override function checkClassType(d:Definition<ClassFlag, Array<Field>>, pos:Position) { |
| 23 | + if (ignoreExtern && (d.flags.indexOf (HExtern) > -1)) return; |
| 24 | + checkFields (d.data); |
| 25 | + } |
| 26 | + |
| 27 | + override function checkEnumType(d:Definition<EnumFlag, Array<EnumConstructor>>, pos:Position) { |
| 28 | + if (!hasToken (ENUM)) return; |
| 29 | + if (ignoreExtern && (d.flags.indexOf (EExtern) > -1)) return; |
| 30 | + checkEnumFields (d.data); |
| 31 | + } |
| 32 | + |
| 33 | + override function checkAbstractType(d:Definition<AbstractFlag, Array<Field>>, pos:Position) { |
| 34 | + checkFields (d.data); |
| 35 | + } |
| 36 | + |
| 37 | + override function checkTypedefType(d:Definition<EnumFlag, ComplexType>, pos:Position) { |
| 38 | + if (!hasToken (TYPEDEF)) return; |
| 39 | + if (ignoreExtern && (d.flags.indexOf (EExtern) > -1)) return; |
| 40 | + |
| 41 | + switch (d.data) { |
| 42 | + case TAnonymous (f): |
| 43 | + checkTypedefFields (f); |
| 44 | + default: |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + function checkFields(d:Array<Field>) { |
| 49 | + for (field in d) { |
| 50 | + switch (field.kind) { |
| 51 | + case FVar (t, e): |
| 52 | + checkField (field, t, e); |
| 53 | + default: |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + function checkTypedefFields(d:Array<Field>) { |
| 59 | + for (field in d) { |
| 60 | + switch (field.kind) { |
| 61 | + case FVar (t, e): |
| 62 | + checkTypedefField (field, t, e); |
| 63 | + default: |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + function checkEnumFields(d:Array<EnumConstructor>) { |
| 69 | + for (field in d) { |
| 70 | + matchTypeName ("enum member", field.name, field.pos); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + function checkField(f:Field, t:ComplexType, e:Expr) { |
| 75 | + var access = getFieldAccess (f); |
| 76 | + |
| 77 | + if (access.isStatic) return; |
| 78 | + if (!hasToken (PUBLIC) && access.isPublic) return; |
| 79 | + if (!hasToken (PRIVATE) && access.isPrivate) return; |
| 80 | + |
| 81 | + matchTypeName ("member", f.name, f.pos); |
| 82 | + } |
| 83 | + |
| 84 | + function checkTypedefField(f:Field, t:ComplexType, e:Expr) { |
| 85 | + matchTypeName ("typedef member", f.name, f.pos); |
| 86 | + } |
| 87 | +} |
0 commit comments