-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tests): refactor token snaphot serializer
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
- Loading branch information
1 parent
a4837a7
commit 874267c
Showing
7 changed files
with
262 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @file Test Utilities - inspect | ||
* @module tests/utils/inspect | ||
*/ | ||
|
||
import type { Token } from '#src/interfaces' | ||
import { omit } from '@flex-development/tutils' | ||
import { u } from '@flex-development/unist-util-builder' | ||
import * as i from '@flex-development/unist-util-inspect' | ||
import type { Literal, Node } from 'unist' | ||
|
||
export default inspect | ||
|
||
/** | ||
* Inspect a token list. | ||
* | ||
* @see {@linkcode i.Options} | ||
* @see {@linkcode Token} | ||
* | ||
* @param {Token} token - Head token | ||
* @param {(i.Options | null)?} [options] - Configuration options | ||
* @return {string} Pretty printed token list | ||
*/ | ||
function inspect(token: Token, options?: i.Options | null): string { | ||
return i.inspectNoColor(u('tokens', nodes(token)), options) | ||
} | ||
|
||
/** | ||
* Convert a token to a list of nodes. | ||
* | ||
* @internal | ||
* | ||
* @param {Token} token - Head token | ||
* @return {(Literal | Node)[]} Node list | ||
*/ | ||
function nodes(token: Token): (Literal | Node)[] { | ||
/** | ||
* Node list. | ||
* | ||
* @const {(Literal | Node)[]} list | ||
*/ | ||
const list: (Literal | Node)[] = [] | ||
|
||
/** | ||
* Current token. | ||
* | ||
* @var {Token | undefined} tok | ||
*/ | ||
let tok: Token | undefined = token | ||
|
||
// build list | ||
while (tok) { | ||
/** | ||
* New node. | ||
* | ||
* @const {Literal | Node} node | ||
*/ | ||
const node: Literal | Node = u(tok.type, { | ||
...omit(tok, ['end', 'next', 'previous', 'start']), | ||
position: { end: tok.end, start: tok.start } | ||
}) | ||
|
||
list.push(node) | ||
tok = tok.next | ||
} | ||
|
||
return list | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @file Test Utilities - isPoint | ||
* @module tests/utils/isPoint | ||
*/ | ||
|
||
import type { Point } from '@flex-development/vfile-reader' | ||
|
||
/** | ||
* Check if the specified `value` is a point. | ||
* | ||
* @see {@linkcode Point} | ||
* | ||
* @param {unknown} value - Value to check | ||
* @return {value is Point} `true` if `value` is point | ||
*/ | ||
function isPoint(value: unknown): value is Point { | ||
return ( | ||
typeof value === 'object' && | ||
value !== null && | ||
'column' in value && | ||
'line' in value && | ||
'offset' in value && | ||
typeof value.column === 'number' && | ||
typeof value.line === 'number' && | ||
typeof value.offset === 'number' | ||
) | ||
} | ||
|
||
export default isPoint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* @file Test Utilities - isToken | ||
* @module tests/utils/isToken | ||
*/ | ||
|
||
import type { Token } from '#src/interfaces' | ||
import isPoint from './is-point' | ||
|
||
/** | ||
* Check if the specified `value` is a token. | ||
* | ||
* @see {@linkcode Token} | ||
* | ||
* @param {unknown} value - Value to check | ||
* @return {value is Token} `true` if `value` is token | ||
*/ | ||
function isToken(value: unknown): value is Token { | ||
return ( | ||
check(value) && | ||
(value.next === undefined || check(value.next)) && | ||
(value.previous === undefined || check(value.previous)) | ||
) | ||
|
||
/** | ||
* Check if `value` is token like. | ||
* | ||
* @param {unknown} value - Value to check | ||
* @return {value is Token} `true` if `value` is token like | ||
*/ | ||
function check(value: unknown): value is Token { | ||
return ( | ||
typeof value === 'object' && | ||
value !== null && | ||
'end' in value && | ||
'type' in value && | ||
'start' in value && | ||
isPoint(value.end) && | ||
isPoint(value.start) && | ||
typeof value.type === 'string' | ||
) | ||
} | ||
} | ||
|
||
export default isToken |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.