(options: TestContextOptions = {})
| 13 | } |
| 14 | |
| 15 | export const createTestContext = (options: TestContextOptions = {}) => { |
| 16 | const { |
| 17 | sourceCode = "", |
| 18 | filename = "/test/file.ts", |
| 19 | cwd = "/test", |
| 20 | ruleOptions = [] |
| 21 | } = options |
| 22 | |
| 23 | const errors: Array<ReportedError> = [] |
| 24 | const context = { |
| 25 | id: "test/rule", |
| 26 | filename, |
| 27 | physicalFilename: filename, |
| 28 | cwd, |
| 29 | options: ruleOptions, |
| 30 | getFilename: () => filename, |
| 31 | getCwd: () => cwd, |
| 32 | report(reportOptions: ReportedError) { |
| 33 | errors.push(reportOptions) |
| 34 | }, |
| 35 | sourceCode: { |
| 36 | text: sourceCode, |
| 37 | getText(node?: { range?: [number, number] } | null) { |
| 38 | if (node?.range) { |
| 39 | return sourceCode.slice(node.range[0], node.range[1]) |
| 40 | } |
| 41 | return sourceCode |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | return { errors, context } |
| 46 | } |
| 47 | |
| 48 | export const runRule = ( |
| 49 | rule: CreateRule, |
no outgoing calls