|
| 1 | +declare namespace usercssMeta { |
| 2 | + export const ParseError: ParseError; |
| 3 | + |
| 4 | + export interface ParseError extends Error { |
| 5 | + code: |
| 6 | + | "invalidCheckboxDefault" |
| 7 | + | "invalidRange" |
| 8 | + | "invalidRangeMultipleUnits" |
| 9 | + | "invalidRangeTooManyValues" |
| 10 | + | "invalidRangeValue" |
| 11 | + | "invalidRangeDefault" |
| 12 | + | "invalidRangeMin" |
| 13 | + | "invalidRangeMax" |
| 14 | + | "invalidRangeStep" |
| 15 | + | "invalidRangeUnits" |
| 16 | + | "invalidNumber" |
| 17 | + | "invalidSelect" |
| 18 | + | "invalidSelectValue" |
| 19 | + | "invalidSelectEmptyOptions" |
| 20 | + | "invalidSelectLabel" |
| 21 | + | "invalidSelectMultipleDefaults" |
| 22 | + | "invalidSelectNameDuplicated" |
| 23 | + | "invalidString" |
| 24 | + | "invalidURLProtocol" |
| 25 | + | "invalidVersion" |
| 26 | + | "invalidWord" |
| 27 | + | "missingChar" |
| 28 | + | "missingEOT" |
| 29 | + | "missingMandatory" |
| 30 | + | "missingValue" |
| 31 | + | "unknownJSONLiteral" |
| 32 | + | "unknownMeta" |
| 33 | + | "unknownVarType"; |
| 34 | + |
| 35 | + message: string; |
| 36 | + |
| 37 | + /** |
| 38 | + * The string index where the error occurs |
| 39 | + */ |
| 40 | + index: number; |
| 41 | + |
| 42 | + /** |
| 43 | + * An array of values that is used to compose the error message. |
| 44 | + * This allows other clients to generate i18n error message. |
| 45 | + */ |
| 46 | + args: unknown[]; |
| 47 | + } |
| 48 | + |
| 49 | + // TODO: export util types |
| 50 | + // export const util: {}; |
| 51 | + |
| 52 | + /** |
| 53 | + * This is a shortcut of `createParser(options).parse(text);` |
| 54 | + */ |
| 55 | + export function parse( |
| 56 | + content: string, |
| 57 | + options?: ParserOptions, |
| 58 | + ): ParseResult; |
| 59 | + |
| 60 | + /** |
| 61 | + * Create a metadata parser. |
| 62 | + */ |
| 63 | + export function createParser(options?: ParserOptions): Parser; |
| 64 | + |
| 65 | + // TODO: export stringify types |
| 66 | + // export function stringify( |
| 67 | + // metadata: Metadata, |
| 68 | + // options: StringifierOptions, |
| 69 | + // ): string; |
| 70 | + // export function createStringifier(options: StringifierOptions): Stringifier; |
| 71 | + |
| 72 | + type Parser = { |
| 73 | + /** |
| 74 | + * Parse the text (metadata header) and return the result. |
| 75 | + */ |
| 76 | + parse: typeof parse; |
| 77 | + |
| 78 | + /** |
| 79 | + * Validate the value of the variable object. |
| 80 | + * This function uses the validators defined in `createParser`. |
| 81 | + */ |
| 82 | + validateVar: (varObj: VarObj) => void; |
| 83 | + }; |
| 84 | + |
| 85 | + type ParserOptions = { |
| 86 | + /** |
| 87 | + * `unknownKey` decides how to parse unknown keys. Possible values are: |
| 88 | + * - `ignore`: The directive is ignored. Default. |
| 89 | + * - `assign`: Assign the text value (characters before `\s*\n`) to result object. |
| 90 | + * - `throw`: Throw a `ParseError`. |
| 91 | + * @default "ignore" |
| 92 | + */ |
| 93 | + unknownKey?: "ignore" | "assign" | "throw"; |
| 94 | + |
| 95 | + /** |
| 96 | + * mandatoryKeys marks multiple keys as mandatory. If some keys are missing then throw a ParseError |
| 97 | + * @default ["name", "namespace", "version"] |
| 98 | + */ |
| 99 | + mandatoryKeys?: string[]; |
| 100 | + |
| 101 | + /** |
| 102 | + * A `key`/`parseFunction` map. |
| 103 | + * It allows users to extend the parser. |
| 104 | + * |
| 105 | + * @example |
| 106 | + * const parser = createParser({ |
| 107 | + * mandatoryKeys: [], |
| 108 | + * parseKey: { |
| 109 | + * myKey: util.parseNumber |
| 110 | + * } |
| 111 | + * }); |
| 112 | + * const {metadata} = parser.parse(` |
| 113 | + * /* ==UserStyle== |
| 114 | + * \@myKey 123456 |
| 115 | + * ==/UserStyle== |
| 116 | + * `); |
| 117 | + * assert.equal(metadata.myKey, 123456); |
| 118 | + */ |
| 119 | + parseKey?: Record<string, unknown>; |
| 120 | + |
| 121 | + /** |
| 122 | + * A `variableType`/`parseFunction` map. |
| 123 | + * It extends the parser to parse additional variable types. |
| 124 | + * |
| 125 | + * @example |
| 126 | + * const parser = createParser({ |
| 127 | + * mandatoryKeys: [], |
| 128 | + * parseVar: { |
| 129 | + * myvar: util.parseNumber |
| 130 | + * } |
| 131 | + * }); |
| 132 | + * const {metadata} = parser.parse(`/* ==UserStyle== |
| 133 | + * \@var myvar var-name 'Customized variable' 123456 |
| 134 | + * ==/UserStyle== *\/`); |
| 135 | + * const va = metadata.vars['var-name']; |
| 136 | + * assert.equal(va.type, 'myvar'); |
| 137 | + * assert.equal(va.label, 'Customized variable'); |
| 138 | + * assert.equal(va.default, 123456); |
| 139 | + */ |
| 140 | + parseVar?: Record<string, unknown>; |
| 141 | + /** |
| 142 | + * A `key`/`validateFunction` map, which is used to validate the metadata value. |
| 143 | + * The function accepts a state object. |
| 144 | + * |
| 145 | + * @example |
| 146 | + * const parser = createParser({ |
| 147 | + * validateKey: { |
| 148 | + * updateURL: state => { |
| 149 | + * if (/example\.com/.test(state.value)) { |
| 150 | + * throw new ParseError({ |
| 151 | + * message: 'Example.com is not a good URL', |
| 152 | + * index: state.valueIndex |
| 153 | + * }); |
| 154 | + * } |
| 155 | + * } |
| 156 | + * } |
| 157 | + * }); |
| 158 | + */ |
| 159 | + validateKey?: Record<string, validateFn>; |
| 160 | + |
| 161 | + /** |
| 162 | + * A `variableType`/`validateFunction` map, which is used to validate variables. |
| 163 | + * The function accepts a state object. |
| 164 | + * |
| 165 | + * @example |
| 166 | + * const parser = createParser({ |
| 167 | + * validateVar: { |
| 168 | + * color: state => { |
| 169 | + * if (state.value === 'red') { |
| 170 | + * throw new ParseError({ |
| 171 | + * message: '`red` is not allowed', |
| 172 | + * index: state.valueIndex |
| 173 | + * }); |
| 174 | + * } |
| 175 | + * } |
| 176 | + * } |
| 177 | + * }); |
| 178 | + */ |
| 179 | + validateVar?: Record<string, (state: StateObject) => void>; |
| 180 | + |
| 181 | + /** |
| 182 | + * If allowErrors is true, the parser will collect parsing errors while |
| 183 | + * `parser.parse()` and return them as {@link ParseResult.errors} |
| 184 | + * Otherwise, the first parsing error will be thrown. |
| 185 | + * @default false |
| 186 | + */ |
| 187 | + allowErrors?: boolean; |
| 188 | + }; |
| 189 | + |
| 190 | + export type StateObject = { |
| 191 | + key: string; |
| 192 | + type: string; |
| 193 | + value: string; |
| 194 | + varResult: unknown; |
| 195 | + text: string; |
| 196 | + lastIndex: number; |
| 197 | + valueIndex: number; |
| 198 | + shouldIgnore: boolean; |
| 199 | + }; |
| 200 | + |
| 201 | + type validateFn = (state: StateObject) => void; |
| 202 | + |
| 203 | + type VarObj = { |
| 204 | + label: string; |
| 205 | + name: string; |
| 206 | + value?: string; |
| 207 | + default?: string; |
| 208 | + options?: unknown; |
| 209 | + }; |
| 210 | + type Metadata = { |
| 211 | + vars: VarObj[]; |
| 212 | + [key: string]: unknown; |
| 213 | + }; |
| 214 | + |
| 215 | + type ParseResult = { |
| 216 | + metadata: Metadata; |
| 217 | + errors: ParseError[]; |
| 218 | + }; |
| 219 | +} |
| 220 | + |
| 221 | +declare module "usercss-meta" { |
| 222 | + export = usercssMeta; |
| 223 | +} |
0 commit comments