KCL: No 'let' or 'const' required when declaring vars (#4063)
Previously variable declaration required a keyword, e.g. ```kcl let x = 4 const x = 4 var x = 4 ``` These were all valid, and did the exact same thing. As of this PR, they're all still valid, but the KCL formatter will change them all to just: ```kcl x = 4 ``` which is the new preferred way to declare a constant. But the formatter will remove the var/let/const keywords. Closes https://github.com/KittyCAD/modeling-app/issues/3985
This commit is contained in:
@ -129,7 +129,7 @@ describe('Testing addSketchTo', () => {
|
||||
'yz'
|
||||
)
|
||||
const str = recast(result.modifiedAst)
|
||||
expect(str).toBe(`const sketch001 = startSketchOn('YZ')
|
||||
expect(str).toBe(`sketch001 = startSketchOn('YZ')
|
||||
|> startProfileAt('default', %)
|
||||
|> line('default', %)
|
||||
`)
|
||||
@ -156,7 +156,7 @@ function giveSketchFnCallTagTestHelper(
|
||||
}
|
||||
|
||||
describe('Testing giveSketchFnCallTag', () => {
|
||||
const code = `const part001 = startSketchOn('XY')
|
||||
const code = `part001 = startSketchOn('XY')
|
||||
|> startProfileAt([0, 0], %)
|
||||
|> line([-2.57, -0.13], %)
|
||||
|> line([0, 0.83], %)
|
||||
@ -207,8 +207,8 @@ fn ghi = (x) => {
|
||||
}
|
||||
const abc = 3
|
||||
const identifierGuy = 5
|
||||
const yo = 5 + 6
|
||||
const part001 = startSketchOn('XY')
|
||||
yo = 5 + 6
|
||||
part001 = startSketchOn('XY')
|
||||
|> startProfileAt([-1.2, 4.83], %)
|
||||
|> line([2.8, 0], %)
|
||||
|> angledLine([100 + 100, 3.09], %)
|
||||
@ -216,7 +216,7 @@ const part001 = startSketchOn('XY')
|
||||
|> angledLine([def(yo), 3.09], %)
|
||||
|> angledLine([ghi(%), 3.09], %)
|
||||
|> angledLine([jkl(yo) + 2, 3.09], %)
|
||||
const yo2 = hmm([identifierGuy + 5])`
|
||||
yo2 = hmm([identifierGuy + 5])`
|
||||
it('should move a binary expression into a new variable', async () => {
|
||||
const ast = parse(code)
|
||||
if (err(ast)) throw ast
|
||||
@ -229,7 +229,7 @@ const yo2 = hmm([identifierGuy + 5])`
|
||||
'newVar'
|
||||
)
|
||||
const newCode = recast(modifiedAst)
|
||||
expect(newCode).toContain(`const newVar = 100 + 100`)
|
||||
expect(newCode).toContain(`newVar = 100 + 100`)
|
||||
expect(newCode).toContain(`angledLine([newVar, 3.09], %)`)
|
||||
})
|
||||
it('should move a value into a new variable', async () => {
|
||||
@ -244,7 +244,7 @@ const yo2 = hmm([identifierGuy + 5])`
|
||||
'newVar'
|
||||
)
|
||||
const newCode = recast(modifiedAst)
|
||||
expect(newCode).toContain(`const newVar = 2.8`)
|
||||
expect(newCode).toContain(`newVar = 2.8`)
|
||||
expect(newCode).toContain(`line([newVar, 0], %)`)
|
||||
})
|
||||
it('should move a callExpression into a new variable', async () => {
|
||||
@ -259,7 +259,7 @@ const yo2 = hmm([identifierGuy + 5])`
|
||||
'newVar'
|
||||
)
|
||||
const newCode = recast(modifiedAst)
|
||||
expect(newCode).toContain(`const newVar = def(yo)`)
|
||||
expect(newCode).toContain(`newVar = def(yo)`)
|
||||
expect(newCode).toContain(`angledLine([newVar, 3.09], %)`)
|
||||
})
|
||||
it('should move a binary expression with call expression into a new variable', async () => {
|
||||
@ -274,7 +274,7 @@ const yo2 = hmm([identifierGuy + 5])`
|
||||
'newVar'
|
||||
)
|
||||
const newCode = recast(modifiedAst)
|
||||
expect(newCode).toContain(`const newVar = jkl(yo) + 2`)
|
||||
expect(newCode).toContain(`newVar = jkl(yo) + 2`)
|
||||
expect(newCode).toContain(`angledLine([newVar, 3.09], %)`)
|
||||
})
|
||||
it('should move a identifier into a new variable', async () => {
|
||||
@ -289,14 +289,14 @@ const yo2 = hmm([identifierGuy + 5])`
|
||||
'newVar'
|
||||
)
|
||||
const newCode = recast(modifiedAst)
|
||||
expect(newCode).toContain(`const newVar = identifierGuy + 5`)
|
||||
expect(newCode).toContain(`const yo2 = hmm([newVar])`)
|
||||
expect(newCode).toContain(`newVar = identifierGuy + 5`)
|
||||
expect(newCode).toContain(`yo2 = hmm([newVar])`)
|
||||
})
|
||||
})
|
||||
|
||||
describe('testing sketchOnExtrudedFace', () => {
|
||||
test('it should be able to extrude on regular segments', async () => {
|
||||
const code = `const part001 = startSketchOn('-XZ')
|
||||
const code = `part001 = startSketchOn('-XZ')
|
||||
|> startProfileAt([3.58, 2.06], %)
|
||||
|> line([9.7, 9.19], %)
|
||||
|> line([8.62, -9.57], %)
|
||||
@ -327,16 +327,16 @@ describe('testing sketchOnExtrudedFace', () => {
|
||||
const { modifiedAst } = extruded
|
||||
|
||||
const newCode = recast(modifiedAst)
|
||||
expect(newCode).toContain(`const part001 = startSketchOn('-XZ')
|
||||
expect(newCode).toContain(`part001 = startSketchOn('-XZ')
|
||||
|> startProfileAt([3.58, 2.06], %)
|
||||
|> line([9.7, 9.19], %, $seg01)
|
||||
|> line([8.62, -9.57], %)
|
||||
|> close(%)
|
||||
|> extrude(5 + 7, %)
|
||||
const sketch001 = startSketchOn(part001, seg01)`)
|
||||
sketch001 = startSketchOn(part001, seg01)`)
|
||||
})
|
||||
test('it should be able to extrude on close segments', async () => {
|
||||
const code = `const part001 = startSketchOn('-XZ')
|
||||
const code = `part001 = startSketchOn('-XZ')
|
||||
|> startProfileAt([3.58, 2.06], %)
|
||||
|> line([9.7, 9.19], %)
|
||||
|> line([8.62, -9.57], %)
|
||||
@ -366,16 +366,16 @@ const sketch001 = startSketchOn(part001, seg01)`)
|
||||
const { modifiedAst } = extruded
|
||||
|
||||
const newCode = recast(modifiedAst)
|
||||
expect(newCode).toContain(`const part001 = startSketchOn('-XZ')
|
||||
expect(newCode).toContain(`part001 = startSketchOn('-XZ')
|
||||
|> startProfileAt([3.58, 2.06], %)
|
||||
|> line([9.7, 9.19], %)
|
||||
|> line([8.62, -9.57], %)
|
||||
|> close(%, $seg01)
|
||||
|> extrude(5 + 7, %)
|
||||
const sketch001 = startSketchOn(part001, seg01)`)
|
||||
sketch001 = startSketchOn(part001, seg01)`)
|
||||
})
|
||||
test('it should be able to extrude on start-end caps', async () => {
|
||||
const code = `const part001 = startSketchOn('-XZ')
|
||||
const code = `part001 = startSketchOn('-XZ')
|
||||
|> startProfileAt([3.58, 2.06], %)
|
||||
|> line([9.7, 9.19], %)
|
||||
|> line([8.62, -9.57], %)
|
||||
@ -406,16 +406,16 @@ const sketch001 = startSketchOn(part001, seg01)`)
|
||||
const { modifiedAst } = extruded
|
||||
|
||||
const newCode = recast(modifiedAst)
|
||||
expect(newCode).toContain(`const part001 = startSketchOn('-XZ')
|
||||
expect(newCode).toContain(`part001 = startSketchOn('-XZ')
|
||||
|> startProfileAt([3.58, 2.06], %)
|
||||
|> line([9.7, 9.19], %)
|
||||
|> line([8.62, -9.57], %)
|
||||
|> close(%)
|
||||
|> extrude(5 + 7, %)
|
||||
const sketch001 = startSketchOn(part001, 'END')`)
|
||||
sketch001 = startSketchOn(part001, 'END')`)
|
||||
})
|
||||
test('it should ensure that the new sketch is inserted after the extrude', async () => {
|
||||
const code = `const sketch001 = startSketchOn('-XZ')
|
||||
const code = `sketch001 = startSketchOn('-XZ')
|
||||
|> startProfileAt([3.29, 7.86], %)
|
||||
|> line([2.48, 2.44], %)
|
||||
|> line([2.66, 1.17], %)
|
||||
@ -428,7 +428,7 @@ const sketch001 = startSketchOn(part001, 'END')`)
|
||||
|> line([-3.86, -2.73], %)
|
||||
|> line([-17.67, 0.85], %)
|
||||
|> close(%)
|
||||
const part001 = extrude(5 + 7, sketch001)`
|
||||
part001 = extrude(5 + 7, sketch001)`
|
||||
const ast = parse(code)
|
||||
if (err(ast)) throw ast
|
||||
const segmentSnippet = `line([4.99, -0.46], %)`
|
||||
@ -451,14 +451,14 @@ const sketch001 = startSketchOn(part001, 'END')`)
|
||||
)
|
||||
if (err(updatedAst)) throw updatedAst
|
||||
const newCode = recast(updatedAst.modifiedAst)
|
||||
expect(newCode).toContain(`const part001 = extrude(5 + 7, sketch001)
|
||||
const sketch002 = startSketchOn(part001, seg01)`)
|
||||
expect(newCode).toContain(`part001 = extrude(5 + 7, sketch001)
|
||||
sketch002 = startSketchOn(part001, seg01)`)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Testing deleteSegmentFromPipeExpression', () => {
|
||||
it('Should delete a segment withOUT any dependent segments', async () => {
|
||||
const code = `const part001 = startSketchOn('-XZ')
|
||||
const code = `part001 = startSketchOn('-XZ')
|
||||
|> startProfileAt([54.78, -95.91], %)
|
||||
|> line([306.21, 198.82], %)
|
||||
|> line([306.21, 198.85], %, $a)
|
||||
@ -481,7 +481,7 @@ describe('Testing deleteSegmentFromPipeExpression', () => {
|
||||
)
|
||||
if (err(modifiedAst)) throw modifiedAst
|
||||
const newCode = recast(modifiedAst)
|
||||
expect(newCode).toBe(`const part001 = startSketchOn('-XZ')
|
||||
expect(newCode).toBe(`part001 = startSketchOn('-XZ')
|
||||
|> startProfileAt([54.78, -95.91], %)
|
||||
|> line([306.21, 198.82], %)
|
||||
|> line([306.21, 198.87], %)
|
||||
@ -492,7 +492,7 @@ describe('Testing deleteSegmentFromPipeExpression', () => {
|
||||
line: string,
|
||||
replace1 = '',
|
||||
replace2 = ''
|
||||
) => `const part001 = startSketchOn('-XZ')
|
||||
) => `part001 = startSketchOn('-XZ')
|
||||
|> startProfileAt([54.78, -95.91], %)
|
||||
|> line([306.21, 198.82], %, $b)
|
||||
${!replace1 ? ` |> ${line}\n` : ''} |> angledLine([-65, ${
|
||||
@ -567,7 +567,7 @@ ${!replace1 ? ` |> ${line}\n` : ''} |> angledLine([-65, ${
|
||||
|
||||
describe('Testing removeSingleConstraintInfo', () => {
|
||||
describe('with mostly object notation', () => {
|
||||
const code = `const part001 = startSketchOn('-XZ')
|
||||
const code = `part001 = startSketchOn('-XZ')
|
||||
|> startProfileAt([0, 0], %)
|
||||
|> line([3 + 0, 4 + 0], %)
|
||||
|> angledLine({ angle: 3 + 0, length: 3.14 + 0 }, %)
|
||||
@ -669,7 +669,7 @@ describe('Testing removeSingleConstraintInfo', () => {
|
||||
})
|
||||
})
|
||||
describe('with array notation', () => {
|
||||
const code = `const part001 = startSketchOn('-XZ')
|
||||
const code = `part001 = startSketchOn('-XZ')
|
||||
|> startProfileAt([0, 0], %)
|
||||
|> angledLine([3.14 + 0, 3.14 + 0], %)
|
||||
|> angledLineOfXLength([3 + 0, 3.14 + 0], %)
|
||||
@ -725,14 +725,14 @@ describe('Testing deleteFromSelection', () => {
|
||||
[
|
||||
'basicCase',
|
||||
{
|
||||
codeBefore: `const myVar = 5
|
||||
const sketch003 = startSketchOn('XZ')
|
||||
codeBefore: `myVar = 5
|
||||
sketch003 = startSketchOn('XZ')
|
||||
|> startProfileAt([3.82, 13.6], %)
|
||||
|> line([-2.94, 2.7], %)
|
||||
|> line([7.7, 0.16], %)
|
||||
|> lineTo([profileStartX(%), profileStartY(%)], %)
|
||||
|> close(%)`,
|
||||
codeAfter: `const myVar = 5\n`,
|
||||
codeAfter: `myVar = 5\n`,
|
||||
lineOfInterest: 'line([-2.94, 2.7], %)',
|
||||
type: 'default',
|
||||
},
|
||||
@ -740,7 +740,7 @@ const sketch003 = startSketchOn('XZ')
|
||||
[
|
||||
'delete extrude',
|
||||
{
|
||||
codeBefore: `const sketch001 = startSketchOn('XZ')
|
||||
codeBefore: `sketch001 = startSketchOn('XZ')
|
||||
|> startProfileAt([3.29, 7.86], %)
|
||||
|> line([2.48, 2.44], %)
|
||||
|> line([2.66, 1.17], %)
|
||||
@ -750,7 +750,7 @@ const sketch003 = startSketchOn('XZ')
|
||||
|> line([-17.67, 0.85], %)
|
||||
|> close(%)
|
||||
const extrude001 = extrude(10, sketch001)`,
|
||||
codeAfter: `const sketch001 = startSketchOn('XZ')
|
||||
codeAfter: `sketch001 = startSketchOn('XZ')
|
||||
|> startProfileAt([3.29, 7.86], %)
|
||||
|> line([2.48, 2.44], %)
|
||||
|> line([2.66, 1.17], %)
|
||||
@ -766,8 +766,8 @@ const extrude001 = extrude(10, sketch001)`,
|
||||
[
|
||||
'delete extrude with sketch on it',
|
||||
{
|
||||
codeBefore: `const myVar = 5
|
||||
const sketch001 = startSketchOn('XZ')
|
||||
codeBefore: `myVar = 5
|
||||
sketch001 = startSketchOn('XZ')
|
||||
|> startProfileAt([4.46, 5.12], %, $tag)
|
||||
|> line([0.08, myVar], %)
|
||||
|> line([13.03, 2.02], %, $seg01)
|
||||
@ -778,7 +778,7 @@ const sketch001 = startSketchOn('XZ')
|
||||
|> lineTo([profileStartX(%), profileStartY(%)], %)
|
||||
|> close(%)
|
||||
const extrude001 = extrude(5, sketch001)
|
||||
const sketch002 = startSketchOn(extrude001, seg01)
|
||||
sketch002 = startSketchOn(extrude001, seg01)
|
||||
|> startProfileAt([-12.55, 2.89], %)
|
||||
|> line([3.02, 1.9], %)
|
||||
|> line([1.82, -1.49], %, $seg02)
|
||||
@ -787,8 +787,8 @@ const sketch002 = startSketchOn(extrude001, seg01)
|
||||
|> line([0.3, 0.84], %)
|
||||
|> lineTo([profileStartX(%), profileStartY(%)], %)
|
||||
|> close(%)`,
|
||||
codeAfter: `const myVar = 5
|
||||
const sketch001 = startSketchOn('XZ')
|
||||
codeAfter: `myVar = 5
|
||||
sketch001 = startSketchOn('XZ')
|
||||
|> startProfileAt([4.46, 5.12], %, $tag)
|
||||
|> line([0.08, myVar], %)
|
||||
|> line([13.03, 2.02], %, $seg01)
|
||||
@ -798,7 +798,7 @@ const sketch001 = startSketchOn('XZ')
|
||||
|> line([-8.54, -2.51], %)
|
||||
|> lineTo([profileStartX(%), profileStartY(%)], %)
|
||||
|> close(%)
|
||||
const sketch002 = startSketchOn({
|
||||
sketch002 = startSketchOn({
|
||||
plane: {
|
||||
origin: { x: 1, y: 2, z: 3 },
|
||||
x_axis: { x: 4, y: 5, z: 6 },
|
||||
@ -822,8 +822,8 @@ const sketch002 = startSketchOn({
|
||||
[
|
||||
'delete extrude with sketch on it',
|
||||
{
|
||||
codeBefore: `const myVar = 5
|
||||
const sketch001 = startSketchOn('XZ')
|
||||
codeBefore: `myVar = 5
|
||||
sketch001 = startSketchOn('XZ')
|
||||
|> startProfileAt([4.46, 5.12], %, $tag)
|
||||
|> line([0.08, myVar], %)
|
||||
|> line([13.03, 2.02], %, $seg01)
|
||||
@ -834,7 +834,7 @@ const sketch001 = startSketchOn('XZ')
|
||||
|> lineTo([profileStartX(%), profileStartY(%)], %)
|
||||
|> close(%)
|
||||
const extrude001 = extrude(5, sketch001)
|
||||
const sketch002 = startSketchOn(extrude001, seg01)
|
||||
sketch002 = startSketchOn(extrude001, seg01)
|
||||
|> startProfileAt([-12.55, 2.89], %)
|
||||
|> line([3.02, 1.9], %)
|
||||
|> line([1.82, -1.49], %, $seg02)
|
||||
@ -843,8 +843,8 @@ const sketch002 = startSketchOn(extrude001, seg01)
|
||||
|> line([0.3, 0.84], %)
|
||||
|> lineTo([profileStartX(%), profileStartY(%)], %)
|
||||
|> close(%)`,
|
||||
codeAfter: `const myVar = 5
|
||||
const sketch001 = startSketchOn('XZ')
|
||||
codeAfter: `myVar = 5
|
||||
sketch001 = startSketchOn('XZ')
|
||||
|> startProfileAt([4.46, 5.12], %, $tag)
|
||||
|> line([0.08, myVar], %)
|
||||
|> line([13.03, 2.02], %, $seg01)
|
||||
@ -854,7 +854,7 @@ const sketch001 = startSketchOn('XZ')
|
||||
|> line([-8.54, -2.51], %)
|
||||
|> lineTo([profileStartX(%), profileStartY(%)], %)
|
||||
|> close(%)
|
||||
const sketch002 = startSketchOn({
|
||||
sketch002 = startSketchOn({
|
||||
plane: {
|
||||
origin: { x: 1, y: 2, z: 3 },
|
||||
x_axis: { x: 4, y: 5, z: 6 },
|
||||
|
Reference in New Issue
Block a user