Fix indentation for wrapped array and object expressions, within pipe bodies (#77)
This commit is contained in:
@ -303,6 +303,34 @@ describe('testing call Expressions in BinaryExpressions and UnaryExpressions', (
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('it recasts wrapped object expressions in pipe bodies with correct indentation', () => {
|
||||||
|
it('with a single line', () => {
|
||||||
|
const code = `const part001 = startSketchAt([-0.01, -0.08])
|
||||||
|
|> line({ to: [0.62, 4.15], tag: 'seg01' }, %)
|
||||||
|
|> line([2.77, -1.24], %)
|
||||||
|
|> angledLineThatIntersects({
|
||||||
|
angle: 201,
|
||||||
|
offset: -1.35,
|
||||||
|
intersectTag: 'seg01'
|
||||||
|
}, %)
|
||||||
|
|> line([-0.42, -1.72], %)
|
||||||
|
show(part001)`
|
||||||
|
const { ast } = code2ast(code)
|
||||||
|
const recasted = recast(ast)
|
||||||
|
expect(recasted).toBe(code)
|
||||||
|
})
|
||||||
|
it('recasts wrapped object expressions NOT in pipe body correctly', () => {
|
||||||
|
const code = `angledLineThatIntersects({
|
||||||
|
angle: 201,
|
||||||
|
offset: -1.35,
|
||||||
|
intersectTag: 'seg01'
|
||||||
|
}, %)`
|
||||||
|
const { ast } = code2ast(code)
|
||||||
|
const recasted = recast(ast)
|
||||||
|
expect(recasted).toBe(code)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
// helpers
|
// helpers
|
||||||
|
|
||||||
function code2ast(code: string): { ast: Program; tokens: Token[] } {
|
function code2ast(code: string): { ast: Program; tokens: Token[] } {
|
||||||
|
@ -118,7 +118,8 @@ ${indentation}]`
|
|||||||
|
|
||||||
function recastObjectExpression(
|
function recastObjectExpression(
|
||||||
expression: ObjectExpression,
|
expression: ObjectExpression,
|
||||||
indentation = ''
|
indentation = '',
|
||||||
|
isInPipeExpression = false
|
||||||
): string {
|
): string {
|
||||||
const flatRecast = `{ ${expression.properties
|
const flatRecast = `{ ${expression.properties
|
||||||
.map((prop) => `${prop.key.name}: ${recastValue(prop.value)}`)
|
.map((prop) => `${prop.key.name}: ${recastValue(prop.value)}`)
|
||||||
@ -130,7 +131,7 @@ function recastObjectExpression(
|
|||||||
${_indentation}${expression.properties
|
${_indentation}${expression.properties
|
||||||
.map((prop) => `${prop.key.name}: ${recastValue(prop.value)}`)
|
.map((prop) => `${prop.key.name}: ${recastValue(prop.value)}`)
|
||||||
.join(`,\n${_indentation}`)}
|
.join(`,\n${_indentation}`)}
|
||||||
}`
|
${isInPipeExpression ? ' ' : ''}}`
|
||||||
}
|
}
|
||||||
return flatRecast
|
return flatRecast
|
||||||
}
|
}
|
||||||
@ -159,14 +160,19 @@ function recastLiteral(literal: Literal): string {
|
|||||||
|
|
||||||
function recastCallExpression(
|
function recastCallExpression(
|
||||||
expression: CallExpression,
|
expression: CallExpression,
|
||||||
indentation = ''
|
indentation = '',
|
||||||
|
isInPipeExpression = false
|
||||||
): string {
|
): string {
|
||||||
return `${expression.callee.name}(${expression.arguments
|
return `${expression.callee.name}(${expression.arguments
|
||||||
.map((arg) => recastArgument(arg, indentation))
|
.map((arg) => recastArgument(arg, indentation, isInPipeExpression))
|
||||||
.join(', ')})`
|
.join(', ')})`
|
||||||
}
|
}
|
||||||
|
|
||||||
function recastArgument(argument: Value, indentation = ''): string {
|
function recastArgument(
|
||||||
|
argument: Value,
|
||||||
|
indentation = '',
|
||||||
|
isInPipeExpression = false
|
||||||
|
): string {
|
||||||
if (argument.type === 'Literal') {
|
if (argument.type === 'Literal') {
|
||||||
return recastLiteral(argument)
|
return recastLiteral(argument)
|
||||||
} else if (argument.type === 'Identifier') {
|
} else if (argument.type === 'Identifier') {
|
||||||
@ -176,7 +182,7 @@ function recastArgument(argument: Value, indentation = ''): string {
|
|||||||
} else if (argument.type === 'ArrayExpression') {
|
} else if (argument.type === 'ArrayExpression') {
|
||||||
return recastArrayExpression(argument, indentation)
|
return recastArrayExpression(argument, indentation)
|
||||||
} else if (argument.type === 'ObjectExpression') {
|
} else if (argument.type === 'ObjectExpression') {
|
||||||
return recastObjectExpression(argument)
|
return recastObjectExpression(argument, indentation, isInPipeExpression)
|
||||||
} else if (argument.type === 'CallExpression') {
|
} else if (argument.type === 'CallExpression') {
|
||||||
return recastCallExpression(argument)
|
return recastCallExpression(argument)
|
||||||
} else if (argument.type === 'FunctionExpression') {
|
} else if (argument.type === 'FunctionExpression') {
|
||||||
@ -212,13 +218,18 @@ function recastMemberExpression(
|
|||||||
return expression.object.name + keyString
|
return expression.object.name + keyString
|
||||||
}
|
}
|
||||||
|
|
||||||
function recastValue(node: Value, indentation = ''): string {
|
function recastValue(
|
||||||
|
node: Value,
|
||||||
|
_indentation = '',
|
||||||
|
isInPipeExpression = false
|
||||||
|
): string {
|
||||||
|
const indentation = _indentation + (isInPipeExpression ? ' ' : '')
|
||||||
if (node.type === 'BinaryExpression') {
|
if (node.type === 'BinaryExpression') {
|
||||||
return recastBinaryExpression(node)
|
return recastBinaryExpression(node)
|
||||||
} else if (node.type === 'ArrayExpression') {
|
} else if (node.type === 'ArrayExpression') {
|
||||||
return recastArrayExpression(node, indentation)
|
return recastArrayExpression(node, indentation)
|
||||||
} else if (node.type === 'ObjectExpression') {
|
} else if (node.type === 'ObjectExpression') {
|
||||||
return recastObjectExpression(node, indentation)
|
return recastObjectExpression(node, indentation, isInPipeExpression)
|
||||||
} else if (node.type === 'MemberExpression') {
|
} else if (node.type === 'MemberExpression') {
|
||||||
return recastMemberExpression(node, indentation)
|
return recastMemberExpression(node, indentation)
|
||||||
} else if (node.type === 'Literal') {
|
} else if (node.type === 'Literal') {
|
||||||
@ -226,7 +237,7 @@ function recastValue(node: Value, indentation = ''): string {
|
|||||||
} else if (node.type === 'FunctionExpression') {
|
} else if (node.type === 'FunctionExpression') {
|
||||||
return recastFunction(node)
|
return recastFunction(node)
|
||||||
} else if (node.type === 'CallExpression') {
|
} else if (node.type === 'CallExpression') {
|
||||||
return recastCallExpression(node, indentation)
|
return recastCallExpression(node, indentation, isInPipeExpression)
|
||||||
} else if (node.type === 'Identifier') {
|
} else if (node.type === 'Identifier') {
|
||||||
return node.name
|
return node.name
|
||||||
} else if (node.type === 'PipeExpression') {
|
} else if (node.type === 'PipeExpression') {
|
||||||
@ -243,7 +254,7 @@ function recastPipeExpression(expression: PipeExpression): string {
|
|||||||
let str = ''
|
let str = ''
|
||||||
let indentation = ' '
|
let indentation = ' '
|
||||||
let maybeLineBreak = '\n'
|
let maybeLineBreak = '\n'
|
||||||
str = recastValue(statement, indentation)
|
str = recastValue(statement, indentation, true)
|
||||||
if (
|
if (
|
||||||
expression.nonCodeMeta?.[index]?.value &&
|
expression.nonCodeMeta?.[index]?.value &&
|
||||||
expression.nonCodeMeta?.[index].value !== ' '
|
expression.nonCodeMeta?.[index].value !== ' '
|
||||||
|
Reference in New Issue
Block a user