(id: string, code: string)
| 291 | // between them, which holds no quote or slash to misread. A comment is none, |
| 292 | // as a transform printing the module drops it |
| 293 | const tokensOf = (id: string, code: string): ModuleTokens | undefined => { |
| 294 | const { program, comments, errors } = parseModule(id, code) |
| 295 | if (errors.length) return |
| 296 | const named: (Token | Span)[] = [...comments] |
| 297 | collectNamed(program as never, code, named) |
| 298 | named.sort((l, r) => l.start - r.start) |
| 299 | const tokens: Token[] = [] |
| 300 | let at = 0 |
| 301 | for (const span of named) { |
| 302 | // e.g. a shorthand property's key and value |
| 303 | if (span.start < at) continue |
| 304 | readBetween(code, at, span.start, tokens) |
| 305 | if ("text" in span) tokens.push(span) |
| 306 | at = span.end |
| 307 | } |
| 308 | readBetween(code, at, code.length, tokens) |
| 309 | const statements: StatementTokens[] = [] |
| 310 | let next = 0 |
| 311 | for (const statement of program.body) { |
| 312 | const start = next |
| 313 | while (next < tokens.length && tokens[next].start < statement.end) next++ |
| 314 | if (next > start) statements.push([start, next]) |
| 315 | } |
| 316 | // a token past the last statement, which nothing a parser reads leaves |
| 317 | if (next < tokens.length) statements.push([next, tokens.length]) |
| 318 | return { |
| 319 | tokens, |
| 320 | texts: tokens.map(token => token.text), |
| 321 | statements, |
| 322 | program |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | interface NamedNode extends Span { |
| 327 | type: string |
no test coverage detected