diff --git a/README.md b/README.md index ad69694..7c14ec9 100644 --- a/README.md +++ b/README.md @@ -181,28 +181,16 @@ walk(ast, (node) => { }) ``` -### `tokenize_cb(source, on_token)` +### `tokenize(source, on_token)` -Calls `on_token(type, start, end, line, column)` for every token. Whitespace and comments are emitted as tokens (`TOKEN_WHITESPACE`, `TOKEN_COMMENT`). This is about 35% faster than iterating `tokenize()`, because no generator or token objects are allocated. +Calls `on_token(type, start, end, line, column)` for every token. Whitespace and comments are emitted as tokens (`TOKEN_WHITESPACE`, `TOKEN_COMMENT`). ```typescript -tokenize_cb('a { color: red }', (type, start, end, line, column) => { +tokenize('a { color: red }', (type, start, end, line, column) => { console.log(type, start, end) }) ``` -In 0.20.0 this becomes `tokenize()`, replacing the iterator form below. - -### `tokenize(source, on_comment?)` (deprecated) - -A generator of tokens; comments are skipped and reported through `on_comment`. Deprecated: use `tokenize_cb()` now, it becomes `tokenize()` in 0.20.0. - -```typescript -for (const token of tokenize('a { color: red }')) { - console.log(token.type, token.start, token.end) -} -``` - ### Node types, type guards and `clone()` Type guards such as `is_declaration()` narrow a node to its specific type. `node.clone({ deep?, locations? })` returns a mutable plain-object copy of a node. diff --git a/benchmark/parsing.ts b/benchmark/parsing.ts index cfaf745..8c4a2b0 100644 --- a/benchmark/parsing.ts +++ b/benchmark/parsing.ts @@ -11,7 +11,6 @@ import { parse_value, parse_dimension, tokenize, - tokenize_cb, walk, traverse, STYLE_RULE, @@ -35,13 +34,7 @@ for (const [name, file] of Object.entries(files)) { const css = fs.readFileSync(path.resolve('benchmark', file), 'utf-8') bench.add(`tokenize|${name}`, () => { - for (const _token of tokenize(css)) { - // iterate - } - }) - - bench.add(`tokenize_cb|${name}`, () => { - tokenize_cb(css, () => {}) + tokenize(css, () => {}) }) bench.add(`parse|${name}`, () => { diff --git a/src/index.ts b/src/index.ts index cb1f096..8ab848d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ export { parse_atrule_prelude } from './parse-atrule-prelude' export { parse_declaration } from './parse-declaration' export { parse_value } from './parse-value' export { parse_dimension } from './parse-dimension' -export { tokenize, tokenize_cb } from './tokenize' +export { tokenize } from './tokenize' export { walk, traverse, walk_declarations, walk_selectors, walk_values, SKIP, BREAK } from './walk' export { is_custom, diff --git a/src/tokenize.test.ts b/src/tokenize.test.ts index 9c51116..df07a7f 100644 --- a/src/tokenize.test.ts +++ b/src/tokenize.test.ts @@ -1,5 +1,5 @@ import { describe, test, expect } from 'vitest' -import { Lexer, tokenize, tokenize_cb } from './tokenize' +import { Lexer, tokenize } from './tokenize' import { is_ident_start } from './char-types' import { TOKEN_IDENT, @@ -833,20 +833,10 @@ describe('Lexer', () => { }) }) -describe('tokenize_cb', () => { - test('reports the same tokens as the iterator tokenize()', () => { - const css = 'a { color: red; margin: 0 }' - const from_cb: Token[] = [] - tokenize_cb(css, (type, start, end, line, column) => { - from_cb.push({ type, start, end, line, column }) - }) - expect(from_cb.length).toBeGreaterThan(0) - expect(from_cb).toEqual(Array.from(tokenize(css))) - }) - +describe('tokenize', () => { test('emits comments as TOKEN_COMMENT with position', () => { const tokens: Token[] = [] - tokenize_cb('a{/* hi */}', (type, start, end, line, column) => + tokenize('a{/* hi */}', (type, start, end, line, column) => tokens.push({ type, start, end, line, column }), ) const comments = tokens.filter((t) => t.type === TOKEN_COMMENT) @@ -861,7 +851,7 @@ describe('tokenize_cb', () => { test('tracks lines across multi-line comments', () => { const tokens: Token[] = [] - tokenize_cb('/* a\nb */ x', (type, start, end, line, column) => + tokenize('/* a\nb */ x', (type, start, end, line, column) => tokens.push({ type, start, end, line, column }), ) expect(tokens.at(-1)).toMatchObject({ type: TOKEN_IDENT, line: 2, column: 6 }) @@ -875,7 +865,7 @@ describe('tokenize_cb', () => { test('calls nothing for empty source', () => { let calls = 0 - tokenize_cb('', () => calls++) + tokenize('', () => calls++) expect(calls).toBe(0) }) }) diff --git a/src/tokenize.ts b/src/tokenize.ts index 9f84bfa..ae5a626 100644 --- a/src/tokenize.ts +++ b/src/tokenize.ts @@ -1118,43 +1118,13 @@ export type OnToken = ( /** * Tokenize CSS source code, calling `on_token` for every token. - * Faster than the iterator-based `tokenize()` because it allocates no generator or token objects. * @param source - The CSS source code to tokenize * @param on_token - Called with `(type, start, end, line, column)` for each token - * @deprecated Renamed to `tokenize()` in 0.20.0, where the callback form replaces the iterator form. */ -export function tokenize_cb(source: string, on_token: OnToken): void { +export function tokenize(source: string, on_token: OnToken): void { const lexer = new Lexer(source, undefined, true) let type: TokenType while ((type = lexer.next_token()) !== TOKEN_EOF) { on_token(type, lexer.token_start, lexer.token_end, lexer.token_line, lexer.token_column) } } - -/** - * Tokenize CSS source code - * @param source - The CSS source code to tokenize - * @param on_comment - Optional callback for comment tokens - * @yields CSS tokens - * @deprecated The iterator form is replaced in 0.20.0 by a callback-based `tokenize(source, on_token)`. Use `tokenize_cb()` today for the same behavior. - */ -export function* tokenize( - source: string, - on_comment?: (info: CommentInfo) => void, -): Generator { - const lexer = new Lexer(source, on_comment) - - while (true) { - const type = lexer.next_token() - if (type === TOKEN_EOF) { - break - } - yield { - type, - start: lexer.token_start, - end: lexer.token_end, - line: lexer.token_line, - column: lexer.token_column, - } - } -}