|
| 1 | +/** |
| 2 | + * AGLint configuration comments. Inspired by ESLint inline configuration comments: |
| 3 | + * |
| 4 | + * @see {@link https://eslint.org/docs/latest/user-guide/configuring/rules#using-configuration-comments} |
| 5 | + */ |
| 6 | + |
| 7 | +import { AdblockSyntax } from "../../utils/adblockers"; |
| 8 | +import { COMMA, EMPTY, SPACE } from "../../utils/constants"; |
| 9 | +import { RuleCategories } from "../common"; |
| 10 | +import { CommentMarker, CommentRuleType, IComment } from "./common"; |
| 11 | +import JSON5 from "json5"; |
| 12 | + |
| 13 | +const AGLINT_COMMAND_PREFIX = "aglint"; |
| 14 | +const PARAMS_SEPARATOR = COMMA; |
| 15 | +const CONFIG_COMMENT_MARKER = "--"; |
| 16 | + |
| 17 | +/** |
| 18 | + * Represents an inline configuration comment. |
| 19 | + * |
| 20 | + * For example, if the comment is |
| 21 | + * ```adblock |
| 22 | + * ! aglint-disable some-rule another-rule |
| 23 | + * ``` |
| 24 | + * then the command is `aglint-disable` and its params is `["some-rule", "another-rule"]`. |
| 25 | + */ |
| 26 | +export interface IConfigComment extends IComment { |
| 27 | + category: RuleCategories.Comment; |
| 28 | + type: CommentRuleType.ConfigComment; |
| 29 | + |
| 30 | + /** Comment marker: `!` or `#` */ |
| 31 | + marker: CommentMarker; |
| 32 | + |
| 33 | + /** Command, for example: `aglint` */ |
| 34 | + command: string; |
| 35 | + |
| 36 | + /** Params: can be a rule configuration object or a list of rule names */ |
| 37 | + params?: object | string[]; |
| 38 | + |
| 39 | + /** Config comment, for example: `! aglint-enable -- this is the comment` */ |
| 40 | + comment?: string; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * ConfigCommentParser is responsible for parsing inline AGLint configuration rules. |
| 45 | + * |
| 46 | + * Inspired by ESLint inline configuration comments. |
| 47 | + * |
| 48 | + * @see {@link https://eslint.org/docs/latest/user-guide/configuring/rules#using-configuration-comments} |
| 49 | + */ |
| 50 | +export class ConfigCommentParser { |
| 51 | + /** |
| 52 | + * Determines whether the rule is an inline configuration comment rule. |
| 53 | + * |
| 54 | + * @param {string} raw - Raw rule |
| 55 | + * @returns {boolean} true/false |
| 56 | + */ |
| 57 | + public static isConfigComment(raw: string): boolean { |
| 58 | + const trimmed = raw.trim(); |
| 59 | + |
| 60 | + if (trimmed[0] == CommentMarker.Regular || trimmed[0] == CommentMarker.Hashmark) { |
| 61 | + // Skip comment marker and trim comment text (it is necessary because of "! something") |
| 62 | + const text = raw.slice(1).trim(); |
| 63 | + |
| 64 | + // The code below is "not pretty", but it runs fast, which is necessary, since it will run on EVERY comment |
| 65 | + // The essence of the indicator is that the control comment always starts with the "aglint" prefix |
| 66 | + return ( |
| 67 | + (text[0] == "a" || text[0] == "A") && |
| 68 | + (text[1] == "g" || text[1] == "G") && |
| 69 | + (text[2] == "l" || text[2] == "L") && |
| 70 | + (text[3] == "i" || text[3] == "I") && |
| 71 | + (text[4] == "n" || text[4] == "N") && |
| 72 | + (text[5] == "t" || text[5] == "T") |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Parses a raw rule as an inline configuration comment. |
| 81 | + * |
| 82 | + * @param {string} raw - Raw rule |
| 83 | + * @returns {IConfigComment | null} |
| 84 | + * Inline configuration comment AST or null (if the raw rule cannot be parsed as configuration comment) |
| 85 | + */ |
| 86 | + public static parse(raw: string): IConfigComment | null { |
| 87 | + const trimmed = raw.trim(); |
| 88 | + |
| 89 | + if (!ConfigCommentParser.isConfigComment(trimmed)) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + let text = raw.slice(1).trim(); |
| 94 | + let comment: string | undefined; |
| 95 | + |
| 96 | + // Remove comment part, for example: "! aglint rule1: "off" -- this is a comment" |
| 97 | + // Correct rules doesn't includes "--" inside |
| 98 | + const commentPos = text.indexOf(CONFIG_COMMENT_MARKER); |
| 99 | + if (commentPos != -1) { |
| 100 | + comment = text.substring(commentPos + 2).trim(); |
| 101 | + text = text.substring(0, commentPos).trim(); |
| 102 | + } |
| 103 | + |
| 104 | + // Prepare result |
| 105 | + const result: IConfigComment = { |
| 106 | + category: RuleCategories.Comment, |
| 107 | + type: CommentRuleType.ConfigComment, |
| 108 | + syntax: AdblockSyntax.Unknown, |
| 109 | + marker: raw[0] as CommentMarker, |
| 110 | + command: text, |
| 111 | + }; |
| 112 | + |
| 113 | + if (comment) { |
| 114 | + result.comment = comment; |
| 115 | + } |
| 116 | + |
| 117 | + let rawParams: string | undefined; |
| 118 | + |
| 119 | + // Get the AGLint command and its parameters. For example, if the following config comment is given: |
| 120 | + // ! aglint-disable something |
| 121 | + // then the command is "aglint-disable" and the parameter is "something". |
| 122 | + const firstSpaceIndex = text.indexOf(SPACE); |
| 123 | + if (firstSpaceIndex != -1) { |
| 124 | + result.command = text.substring(0, firstSpaceIndex).trim().toLocaleLowerCase(); |
| 125 | + rawParams = text.substring(firstSpaceIndex + 1).trim(); |
| 126 | + } |
| 127 | + |
| 128 | + // If the command is simply "aglint", then it is a special case whose parameter is a rule configuration object |
| 129 | + if (result.command === AGLINT_COMMAND_PREFIX) { |
| 130 | + if (!rawParams || rawParams.length == 0) { |
| 131 | + throw new SyntaxError("Missing configuration object"); |
| 132 | + } |
| 133 | + |
| 134 | + // Delegate to JSON parser (JSON5 also supports unquoted properties) |
| 135 | + // TODO: Is some structure validation required at this point? This is currently just a general object |
| 136 | + result.params = JSON5.parse(`{ ${rawParams} }`); |
| 137 | + } else if (rawParams) { |
| 138 | + result.params = rawParams.split(PARAMS_SEPARATOR).map((param) => param.trim()); |
| 139 | + } |
| 140 | + |
| 141 | + return result; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Converts an inline configuration comment AST to a string. |
| 146 | + * |
| 147 | + * @param {IConfigComment} ast - Inline configuration comment AST |
| 148 | + * @returns {string} Raw string |
| 149 | + */ |
| 150 | + public static generate(ast: IConfigComment): string { |
| 151 | + let result = EMPTY; |
| 152 | + |
| 153 | + result += ast.marker; |
| 154 | + result += SPACE; |
| 155 | + result += ast.command; |
| 156 | + |
| 157 | + if (ast.params) { |
| 158 | + result += SPACE; |
| 159 | + if (Array.isArray(ast.params)) { |
| 160 | + result += ast.params.join(PARAMS_SEPARATOR + SPACE); |
| 161 | + } else { |
| 162 | + result += JSON.stringify(ast.params).slice(1, -1).trim(); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if (ast.comment) { |
| 167 | + result += SPACE; |
| 168 | + result += CONFIG_COMMENT_MARKER; |
| 169 | + result += SPACE; |
| 170 | + result += ast.comment; |
| 171 | + } |
| 172 | + |
| 173 | + return result; |
| 174 | + } |
| 175 | +} |
0 commit comments