|
| 1 | +const path = require('path'); |
| 2 | +const minimatch = require('minimatch'); |
| 3 | + |
| 4 | +// Files matching these patterns will be ignored unless a rule has `static global = true` |
| 5 | +const ignore = ['contracts/mocks/**/*', 'test/**/*']; |
| 6 | + |
| 7 | +class Base { |
| 8 | + constructor(reporter, config, source, fileName) { |
| 9 | + this.reporter = reporter; |
| 10 | + this.ignored = this.constructor.global || ignore.some(p => minimatch(path.normalize(fileName), p)); |
| 11 | + this.ruleId = this.constructor.ruleId; |
| 12 | + if (this.ruleId === undefined) { |
| 13 | + throw Error('missing ruleId static property'); |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + error(node, message) { |
| 18 | + if (!this.ignored) { |
| 19 | + this.reporter.error(node, this.ruleId, message); |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +module.exports = [ |
| 25 | + class extends Base { |
| 26 | + static ruleId = 'interface-names'; |
| 27 | + |
| 28 | + ContractDefinition(node) { |
| 29 | + if (node.kind === 'interface' && !/^I[A-Z]/.test(node.name)) { |
| 30 | + this.error(node, 'Interface names should have a capital I prefix'); |
| 31 | + } |
| 32 | + } |
| 33 | + }, |
| 34 | + |
| 35 | + class extends Base { |
| 36 | + static ruleId = 'private-variables'; |
| 37 | + |
| 38 | + VariableDeclaration(node) { |
| 39 | + const constantOrImmutable = node.isDeclaredConst || node.isImmutable; |
| 40 | + if (node.isStateVar && !constantOrImmutable && node.visibility !== 'private') { |
| 41 | + this.error(node, 'State variables must be private'); |
| 42 | + } |
| 43 | + } |
| 44 | + }, |
| 45 | + |
| 46 | + class extends Base { |
| 47 | + static ruleId = 'leading-underscore'; |
| 48 | + |
| 49 | + VariableDeclaration(node) { |
| 50 | + if (node.isDeclaredConst) { |
| 51 | + if (/^_/.test(node.name)) { |
| 52 | + // TODO: re-enable and fix |
| 53 | + // this.error(node, 'Constant variables should not have leading underscore'); |
| 54 | + } |
| 55 | + } else if (node.visibility === 'private' && !/^_/.test(node.name)) { |
| 56 | + this.error(node, 'Non-constant private variables must have leading underscore'); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + FunctionDefinition(node) { |
| 61 | + if (node.visibility === 'private' || (node.visibility === 'internal' && node.parent.kind !== 'library')) { |
| 62 | + if (!/^_/.test(node.name)) { |
| 63 | + this.error(node, 'Private and internal functions must have leading underscore'); |
| 64 | + } |
| 65 | + } |
| 66 | + if (node.visibility === 'internal' && node.parent.kind === 'library') { |
| 67 | + if (/^_/.test(node.name)) { |
| 68 | + this.error(node, 'Library internal functions should not have leading underscore'); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + }, |
| 73 | + |
| 74 | + // TODO: re-enable and fix |
| 75 | + // class extends Base { |
| 76 | + // static ruleId = 'no-external-virtual'; |
| 77 | + // |
| 78 | + // FunctionDefinition(node) { |
| 79 | + // if (node.visibility == 'external' && node.isVirtual) { |
| 80 | + // this.error(node, 'Functions should not be external and virtual'); |
| 81 | + // } |
| 82 | + // } |
| 83 | + // }, |
| 84 | +]; |
0 commit comments