📄 Node.ts
¶
📊 Analysis Summary¶
Metric | Count |
---|---|
🔧 Functions | 7 |
📦 Imports | 2 |
📊 Variables & Constants | 5 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/ast-spec/tests/util/serializers/Node.ts
📦 Imports¶
Name | Source |
---|---|
NewPlugin |
@vitest/pretty-format |
AST_NODE_TYPES |
../../../src/index.js |
Variables & Constants¶
Name | Type | Kind | Value | Exported |
---|---|---|---|---|
keySet |
Set<string> |
const | new Set(Object.keys(node)) |
✗ |
outputLines |
any[] |
const | [] |
✗ |
childIndentation |
any |
const | indentation + config.indent |
✗ |
value |
unknown |
const | node[key] |
✗ |
serializer |
NewPlugin |
const | `{ | |
serialize( | ||||
node: Record |
||||
config, | ||||
indentation, | ||||
depth, | ||||
refs, | ||||
printer, | ||||
) { | ||||
const keys = sortKeys(node); | ||||
const { loc, range, type } = node; |
const outputLines = [];
const childIndentation = indentation + config.indent;
const printValue = (value: unknown): string =>
printer(value, config, childIndentation, depth, refs);
outputLines.push(`${type} {`);
outputLines.push(`${childIndentation}type: ${printValue(type)},`);
for (const key of keys) {
const value = node[key];
// eslint-disable-next-line @typescript-eslint/internal/eqeq-nullish -- intentional strict equality
if (value === undefined) {
continue;
}
outputLines.push(`${childIndentation}${key}: ${printValue(value)},`);
}
outputLines.push('');
outputLines.push(`${childIndentation}range: [${range.join(', ')}],`);
outputLines.push(
`${childIndentation}loc: {`,
`${childIndentation}${config.indent}start: ${stringifyLineAndColumn(
loc.start,
)},`,
`${childIndentation}${config.indent}end: ${stringifyLineAndColumn(
loc.end,
)},`,
`${childIndentation}},`,
);
outputLines.push(`${indentation}}`);
return outputLines.join('\n');
}, test(val: unknown) { return isObject(val) && hasValidType(val.type); }, }` | ✓ |
Functions¶
sortKeys(node: Node): (keyof typeof node)[]
¶
Code
function sortKeys<Node extends TSESTree.Node>(
node: Node,
): (keyof typeof node)[] {
const keySet = new Set(Object.keys(node));
// type place as first key
keySet.delete('type');
// range and loc we place after all properties
keySet.delete('range');
keySet.delete('loc');
// babel keys
keySet.delete('start');
keySet.delete('end');
if (node.type === AST_NODE_TYPES.Program) {
keySet.delete('interpreter');
}
return [...keySet].sort((a, b) =>
a.localeCompare(b),
) as (keyof typeof node)[];
}
- Parameters:
node: Node
- Return Type:
(keyof typeof node)[]
- Calls:
Object.keys
keySet.delete
[...keySet].sort
a.localeCompare
- Internal Comments:
stringifyLineAndColumn(loc: TSESTree.Position): string
¶
Code
- Parameters:
loc: TSESTree.Position
- Return Type:
string
- Calls:
loc.column.toString
loc.line.toString
isObject(val: unknown): val is Record<string, unknown>
¶
Code
- Parameters:
val: unknown
- Return Type:
val is Record<string, unknown>
hasValidType(type: unknown): type is string
¶
- Parameters:
type: unknown
- Return Type:
type is string
printValue(value: unknown): string
¶
- Parameters:
value: unknown
- Return Type:
string
- Calls:
printer
serialize(node: Record<string, unknown> & TSESTree.Node, config: any, indentation: any, depth: any, refs: any, printer: any): string
¶
Code
serialize(
node: Record<string, unknown> & TSESTree.Node,
config,
indentation,
depth,
refs,
printer,
) {
const keys = sortKeys(node);
const { loc, range, type } = node;
const outputLines = [];
const childIndentation = indentation + config.indent;
const printValue = (value: unknown): string =>
printer(value, config, childIndentation, depth, refs);
outputLines.push(`${type} {`);
outputLines.push(`${childIndentation}type: ${printValue(type)},`);
for (const key of keys) {
const value = node[key];
// eslint-disable-next-line @typescript-eslint/internal/eqeq-nullish -- intentional strict equality
if (value === undefined) {
continue;
}
outputLines.push(`${childIndentation}${key}: ${printValue(value)},`);
}
outputLines.push('');
outputLines.push(`${childIndentation}range: [${range.join(', ')}],`);
outputLines.push(
`${childIndentation}loc: {`,
`${childIndentation}${config.indent}start: ${stringifyLineAndColumn(
loc.start,
)},`,
`${childIndentation}${config.indent}end: ${stringifyLineAndColumn(
loc.end,
)},`,
`${childIndentation}},`,
);
outputLines.push(`${indentation}}`);
return outputLines.join('\n');
}
- Parameters:
node: Record<string, unknown> & TSESTree.Node
config: any
indentation: any
depth: any
refs: any
printer: any
- Return Type:
string
- Calls:
sortKeys
printer
outputLines.push
printValue
range.join
stringifyLineAndColumn
outputLines.join
- Internal Comments:
test(val: unknown): boolean
¶
- Parameters:
val: unknown
- Return Type:
boolean
- Calls:
isObject
hasValidType