get recasting working for pipes
This commit is contained in:
@ -74,6 +74,31 @@ show(mySketch)
|
|||||||
const recasted = recast(ast)
|
const recasted = recast(ast)
|
||||||
expect(recasted).toBe(code.trim())
|
expect(recasted).toBe(code.trim())
|
||||||
})
|
})
|
||||||
|
it('sketch piped into callExpression', () => {
|
||||||
|
const code = [
|
||||||
|
'sketch mySk1 {',
|
||||||
|
' lineTo(1, 1)',
|
||||||
|
' path myPath = lineTo(0, 1)',
|
||||||
|
' lineTo(1, 1)',
|
||||||
|
'}',
|
||||||
|
' |> rx(90, %)',
|
||||||
|
].join('\n')
|
||||||
|
const { ast } = code2ast(code)
|
||||||
|
const recasted = recast(ast)
|
||||||
|
expect(recasted).toBe(code.trim())
|
||||||
|
})
|
||||||
|
it('recast BinaryExpression piped into CallExpression', () => {
|
||||||
|
const code = [
|
||||||
|
'fn myFn = (a) => {',
|
||||||
|
' return a + 1',
|
||||||
|
'}',
|
||||||
|
'const myVar = 5 + 1',
|
||||||
|
' |> myFn(%)',
|
||||||
|
].join('\n')
|
||||||
|
const { ast } = code2ast(code)
|
||||||
|
const recasted = recast(ast)
|
||||||
|
expect(recasted).toBe(code.trim())
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// helpers
|
// helpers
|
||||||
|
@ -25,34 +25,19 @@ export function recast(
|
|||||||
} else if (statement.type === 'VariableDeclaration') {
|
} else if (statement.type === 'VariableDeclaration') {
|
||||||
return statement.declarations
|
return statement.declarations
|
||||||
.map((declaration) => {
|
.map((declaration) => {
|
||||||
if (declaration.init.type === 'BinaryExpression') {
|
const isSketchOrFirstPipeExpressionIsSketch =
|
||||||
return `${indentation}${statement.kind} ${
|
declaration.init.type === 'SketchExpression' ||
|
||||||
declaration.id.name
|
(declaration.init.type === 'PipeExpression' &&
|
||||||
} = ${recastBinaryExpression(declaration.init)}`
|
declaration.init.body[0].type === 'SketchExpression')
|
||||||
} else if (declaration.init.type === 'Literal') {
|
|
||||||
return `${indentation}${statement.kind} ${
|
const assignmentString = isSketchOrFirstPipeExpressionIsSketch ? ' ' : ' = '
|
||||||
declaration.id.name
|
return `${indentation}${statement.kind} ${declaration.id.name}${assignmentString}${recastValue(declaration.init)}`
|
||||||
} = ${recastLiteral(declaration.init)}`
|
|
||||||
} else if (declaration.init.type === 'FunctionExpression') {
|
|
||||||
return `${indentation}${statement.kind} ${
|
|
||||||
declaration.id.name
|
|
||||||
} = ${recastFunction(declaration.init)}`
|
|
||||||
} else if (declaration.init.type === 'CallExpression') {
|
|
||||||
return `${indentation}${statement.kind} ${
|
|
||||||
declaration.id.name
|
|
||||||
} = ${recastCallExpression(declaration.init)}`
|
|
||||||
} else if (declaration.init.type === 'SketchExpression') {
|
|
||||||
return `${indentation}${statement.kind} ${
|
|
||||||
declaration.id.name
|
|
||||||
} ${recastSketchExpression(declaration.init, indentation)}`
|
|
||||||
}
|
|
||||||
return ''
|
|
||||||
})
|
})
|
||||||
.join('')
|
.join('')
|
||||||
} else if (statement.type === 'ReturnStatement') {
|
} else if (statement.type === 'ReturnStatement') {
|
||||||
return `${indentation}return ${recastArgument(statement.argument)}`
|
return `${indentation}return ${recastArgument(statement.argument)}`
|
||||||
}
|
}
|
||||||
|
|
||||||
return statement.type
|
return statement.type
|
||||||
})
|
})
|
||||||
.join('\n')
|
.join('\n')
|
||||||
@ -70,7 +55,7 @@ function recastBinaryPart(part: BinaryPart): string {
|
|||||||
} else if (part.type === 'Identifier') {
|
} else if (part.type === 'Identifier') {
|
||||||
return part.name
|
return part.name
|
||||||
}
|
}
|
||||||
throw new Error(`Cannot recast ${part}`)
|
throw new Error(`Cannot recast BinaryPart ${part}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
function recastLiteral(literal: Literal): string {
|
function recastLiteral(literal: Literal): string {
|
||||||
@ -98,8 +83,10 @@ function recastArgument(argument: Value): string {
|
|||||||
return recastCallExpression(argument)
|
return recastCallExpression(argument)
|
||||||
} else if (argument.type === 'FunctionExpression') {
|
} else if (argument.type === 'FunctionExpression') {
|
||||||
return recastFunction(argument)
|
return recastFunction(argument)
|
||||||
|
} else if (argument.type === 'PipeSubstitution') {
|
||||||
|
return '%'
|
||||||
}
|
}
|
||||||
throw new Error(`Cannot recast ${argument}`)
|
throw new Error(`Cannot recast argument ${argument}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
function recastFunction(expression: FunctionExpression): string {
|
function recastFunction(expression: FunctionExpression): string {
|
||||||
@ -116,3 +103,23 @@ function recastSketchExpression(
|
|||||||
${recast(expression.body, '', indentation + ' ')}
|
${recast(expression.body, '', indentation + ' ')}
|
||||||
}`
|
}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function recastValue(node: Value, indentation = ''): string {
|
||||||
|
if (node.type === 'BinaryExpression') {
|
||||||
|
return recastBinaryExpression(node)
|
||||||
|
} else if (node.type === 'Literal') {
|
||||||
|
return recastLiteral(node)
|
||||||
|
} else if (node.type === 'FunctionExpression') {
|
||||||
|
return recastFunction(node)
|
||||||
|
} else if (node.type === 'CallExpression') {
|
||||||
|
return recastCallExpression(node)
|
||||||
|
} else if (node.type === 'SketchExpression') {
|
||||||
|
return recastSketchExpression(node, indentation)
|
||||||
|
} else if (node.type === 'PipeExpression') {
|
||||||
|
return node.body.map((statement): string =>
|
||||||
|
recastValue(statement, indentation)
|
||||||
|
).join('\n |> ')
|
||||||
|
}
|
||||||
|
return ''
|
||||||
|
}
|
Reference in New Issue
Block a user