|
1 | 1 | import { parseFile } from './parser.js' |
2 | 2 | import { resolveSourceFile } from './source-path.js' |
3 | | -import { locateSuiteByTitle, collectSuiteStatements } from './locate/suite.js' |
| 3 | +import { locateSuiteByTitle, collectSuiteStatements, HOOK_KINDS } from './locate/suite.js' |
4 | 4 | import { extractScenarioDepsJS, extractScenarioDepsTS } from './locate/deps.js' |
5 | 5 | import { Edit } from './edit.js' |
6 | | -import { ReflectionError, NotFoundError } from './errors.js' |
| 6 | +import { ReflectionError, NotFoundError, AmbiguousLocateError } from './errors.js' |
| 7 | + |
| 8 | +export { HOOK_KINDS } |
7 | 9 |
|
8 | 10 | export class SuiteReflection { |
9 | 11 | constructor(suite) { |
@@ -61,6 +63,19 @@ export class SuiteReflection { |
61 | 63 | })) |
62 | 64 | } |
63 | 65 |
|
| 66 | + get hooks() { |
| 67 | + const { hooks } = this._statements() |
| 68 | + return hooks.map(h => ({ |
| 69 | + kind: h.kind, |
| 70 | + line: h.line, |
| 71 | + range: h.range, |
| 72 | + })) |
| 73 | + } |
| 74 | + |
| 75 | + findHook(kind) { |
| 76 | + return this.hooks.filter(h => h.kind === kind) |
| 77 | + } |
| 78 | + |
64 | 79 | get dependencies() { |
65 | 80 | const parsed = this._parsed() |
66 | 81 | const { scenarios } = this._statements() |
@@ -132,19 +147,108 @@ export class SuiteReflection { |
132 | 147 | { filePath: parsed.filePath }, |
133 | 148 | ) |
134 | 149 | } |
| 150 | + return this._buildRemoveEdit(parsed, match) |
| 151 | + } |
| 152 | + |
| 153 | + addHook(kind, code, { position = 'afterHooks' } = {}) { |
| 154 | + if (!HOOK_KINDS.includes(kind)) { |
| 155 | + throw new ReflectionError( |
| 156 | + `addHook: unknown hook kind "${kind}". Expected one of: ${HOOK_KINDS.join(', ')}`, |
| 157 | + ) |
| 158 | + } |
| 159 | + const parsed = this._parsed() |
| 160 | + const { featureStmt, hooks, suiteEnd } = this._statements() |
| 161 | + const eol = parsed.eol |
| 162 | + |
| 163 | + let insertPos |
| 164 | + if (position === 'afterFeature' || hooks.length === 0) { |
| 165 | + insertPos = featureStmt ? featureStmt.end : this._locate().range.end |
| 166 | + } else { |
| 167 | + insertPos = hooks[hooks.length - 1].range.end |
| 168 | + } |
| 169 | + if (insertPos > suiteEnd) insertPos = suiteEnd |
| 170 | + |
| 171 | + const trimmed = code.replace(/[\r\n]+$/, '') |
| 172 | + const replacement = eol + eol + trimmed + eol |
| 173 | + |
| 174 | + return new Edit({ |
| 175 | + filePath: parsed.filePath, |
| 176 | + source: parsed.source, |
| 177 | + parsedAtHash: parsed.hash, |
| 178 | + start: insertPos, |
| 179 | + end: insertPos, |
| 180 | + replacement, |
| 181 | + eol, |
| 182 | + }) |
| 183 | + } |
| 184 | + |
| 185 | + removeHook(kind, { index } = {}) { |
| 186 | + const parsed = this._parsed() |
| 187 | + const match = this._pickHook(kind, index, parsed.filePath) |
| 188 | + return this._buildRemoveEdit(parsed, match) |
| 189 | + } |
| 190 | + |
| 191 | + replaceHook(kind, code, { index } = {}) { |
| 192 | + const parsed = this._parsed() |
| 193 | + const match = this._pickHook(kind, index, parsed.filePath) |
| 194 | + return new Edit({ |
| 195 | + filePath: parsed.filePath, |
| 196 | + source: parsed.source, |
| 197 | + parsedAtHash: parsed.hash, |
| 198 | + start: match.range.start, |
| 199 | + end: match.range.end, |
| 200 | + replacement: code.replace(/[\r\n]+$/, ''), |
| 201 | + eol: parsed.eol, |
| 202 | + }) |
| 203 | + } |
| 204 | + |
| 205 | + _pickHook(kind, index, filePath) { |
| 206 | + if (!HOOK_KINDS.includes(kind)) { |
| 207 | + throw new ReflectionError( |
| 208 | + `Unknown hook kind "${kind}". Expected one of: ${HOOK_KINDS.join(', ')}`, |
| 209 | + { filePath }, |
| 210 | + ) |
| 211 | + } |
| 212 | + const { hooks } = this._statements() |
| 213 | + const matches = hooks.filter(h => h.kind === kind) |
| 214 | + if (matches.length === 0) { |
| 215 | + throw new NotFoundError( |
| 216 | + `No ${kind} hook found in suite "${this.title}" in ${filePath}`, |
| 217 | + { filePath }, |
| 218 | + ) |
| 219 | + } |
| 220 | + if (index != null) { |
| 221 | + if (index < 0 || index >= matches.length) { |
| 222 | + throw new NotFoundError( |
| 223 | + `${kind} hook index ${index} out of range (0..${matches.length - 1})`, |
| 224 | + { filePath }, |
| 225 | + ) |
| 226 | + } |
| 227 | + return matches[index] |
| 228 | + } |
| 229 | + if (matches.length > 1) { |
| 230 | + throw new AmbiguousLocateError( |
| 231 | + `Multiple ${kind} hooks in suite "${this.title}". Pass { index } to disambiguate.`, |
| 232 | + { |
| 233 | + filePath, |
| 234 | + candidates: matches.map(m => ({ start: m.range.start, end: m.range.end, line: m.line })), |
| 235 | + }, |
| 236 | + ) |
| 237 | + } |
| 238 | + return matches[0] |
| 239 | + } |
| 240 | + |
| 241 | + _buildRemoveEdit(parsed, match) { |
135 | 242 | const eol = parsed.eol |
136 | 243 | let start = match.range.start |
137 | 244 | let end = match.range.end |
138 | | - // Eat one trailing newline so the surrounding blank line becomes the new separator |
139 | 245 | if (parsed.source.slice(end, end + eol.length) === eol) end += eol.length |
140 | | - // If there was a preceding blank line, eat that too |
141 | 246 | if ( |
142 | 247 | parsed.source.slice(start - eol.length, start) === eol && |
143 | 248 | parsed.source.slice(start - 2 * eol.length, start - eol.length) === eol |
144 | 249 | ) { |
145 | 250 | start -= eol.length |
146 | 251 | } |
147 | | - |
148 | 252 | return new Edit({ |
149 | 253 | filePath: parsed.filePath, |
150 | 254 | source: parsed.source, |
|
0 commit comments