|
| 1 | +/** Provides classes for detecting generated code. */ |
| 2 | + |
| 3 | +private import ruby |
| 4 | +private import codeql.ruby.ast.internal.TreeSitter |
| 5 | + |
| 6 | +/** A source file that contains generated code. */ |
| 7 | +abstract class GeneratedCodeFile extends RubyFile { } |
| 8 | + |
| 9 | +/** A file contining comments suggesting it contains generated code. */ |
| 10 | +class GeneratedCommentFile extends GeneratedCodeFile { |
| 11 | + GeneratedCommentFile() { this = any(GeneratedCodeComment c).getLocation().getFile() } |
| 12 | +} |
| 13 | + |
| 14 | +/** A comment line that indicates generated code. */ |
| 15 | +abstract class GeneratedCodeComment extends Ruby::Comment { } |
| 16 | + |
| 17 | +/** |
| 18 | + * A generic comment line that suggests that the file is generated. |
| 19 | + */ |
| 20 | +class GenericGeneratedCodeComment extends GeneratedCodeComment { |
| 21 | + GenericGeneratedCodeComment() { |
| 22 | + exists(string line, string entity, string was, string automatically | line = getValue() | |
| 23 | + entity = "file|class|art[ei]fact|module|script" and |
| 24 | + was = "was|is|has been" and |
| 25 | + automatically = "automatically |mechanically |auto[- ]?" and |
| 26 | + line.regexpMatch("(?i).*\\bThis (" + entity + ") (" + was + ") (" + automatically + |
| 27 | + ")?generated\\b.*") |
| 28 | + ) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/** A comment warning against modifications. */ |
| 33 | +class DontModifyMarkerComment extends GeneratedCodeComment { |
| 34 | + DontModifyMarkerComment() { |
| 35 | + exists(string line | line = getValue() | |
| 36 | + line.regexpMatch("(?i).*\\bGenerated by\\b.*\\bDo not edit\\b.*") or |
| 37 | + line.regexpMatch("(?i).*\\bAny modifications to this file will be lost\\b.*") |
| 38 | + ) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** Holds if `file` looks like it contains generated code. */ |
| 43 | +predicate isGeneratedCode(GeneratedCodeFile file) { any() } |
0 commit comments