2024-11-21 15:04:30 +11:00
|
|
|
import {
|
2024-12-06 13:57:31 +13:00
|
|
|
assertParse,
|
2024-11-21 15:04:30 +11:00
|
|
|
Sketch,
|
|
|
|
recast,
|
|
|
|
initPromise,
|
|
|
|
sketchFromKclValue,
|
|
|
|
SourceRange,
|
2025-01-17 14:34:36 -05:00
|
|
|
topLevelRange,
|
2024-11-21 15:04:30 +11:00
|
|
|
} from '../wasm'
|
2023-03-02 21:19:11 +11:00
|
|
|
import {
|
|
|
|
ConstraintType,
|
|
|
|
getTransformInfos,
|
2023-03-07 15:45:59 +11:00
|
|
|
transformAstSketchLines,
|
2023-03-02 21:19:11 +11:00
|
|
|
} from './sketchcombos'
|
2023-03-20 07:09:19 +11:00
|
|
|
import { getSketchSegmentFromSourceRange } from './sketchConstraints'
|
2023-07-10 15:15:07 +10:00
|
|
|
import { enginelessExecutor } from '../../lib/testHelpers'
|
2024-06-24 11:45:40 -04:00
|
|
|
import { err } from 'lib/trap'
|
2024-11-21 15:04:30 +11:00
|
|
|
import { codeRefFromRange } from './artifactGraph'
|
2023-02-21 10:50:45 +11:00
|
|
|
|
2024-04-19 14:24:40 -07:00
|
|
|
beforeAll(async () => {
|
|
|
|
await initPromise
|
|
|
|
})
|
2023-02-21 10:50:45 +11:00
|
|
|
|
|
|
|
// testing helper function
|
2023-06-22 16:43:33 +10:00
|
|
|
async function testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode,
|
|
|
|
callToSwap,
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType,
|
2023-02-21 10:50:45 +11:00
|
|
|
}: {
|
|
|
|
inputCode: string
|
|
|
|
callToSwap: string
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: ConstraintType
|
2023-06-22 16:43:33 +10:00
|
|
|
}): Promise<{
|
2023-02-21 10:50:45 +11:00
|
|
|
newCode: string
|
2025-01-17 14:34:36 -05:00
|
|
|
originalRange: SourceRange
|
2023-06-22 16:43:33 +10:00
|
|
|
}> {
|
2023-02-21 10:50:45 +11:00
|
|
|
const startIndex = inputCode.indexOf(callToSwap)
|
2025-01-17 14:34:36 -05:00
|
|
|
const range = topLevelRange(startIndex, startIndex + callToSwap.length)
|
2024-12-06 13:57:31 +13:00
|
|
|
const ast = assertParse(inputCode)
|
2024-06-24 11:45:40 -04:00
|
|
|
|
2024-10-09 19:38:40 -04:00
|
|
|
const execState = await enginelessExecutor(ast)
|
2023-04-03 16:05:25 +10:00
|
|
|
const selections = {
|
2024-11-21 15:04:30 +11:00
|
|
|
graphSelections: [
|
|
|
|
{
|
|
|
|
codeRef: codeRefFromRange(range, ast),
|
|
|
|
},
|
|
|
|
],
|
2023-04-03 16:05:25 +10:00
|
|
|
otherSelections: [],
|
|
|
|
}
|
|
|
|
const transformInfos = getTransformInfos(selections, ast, constraintType)
|
2023-03-02 21:19:11 +11:00
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
if (!transformInfos)
|
|
|
|
return Promise.reject(new Error('transformInfos undefined'))
|
|
|
|
const ast2 = transformAstSketchLines({
|
2023-02-21 10:50:45 +11:00
|
|
|
ast,
|
2025-02-12 10:22:56 +13:00
|
|
|
memVars: execState.variables,
|
2023-04-03 16:05:25 +10:00
|
|
|
selectionRanges: selections,
|
2023-03-02 21:19:11 +11:00
|
|
|
transformInfos,
|
2023-03-07 15:45:59 +11:00
|
|
|
referenceSegName: '',
|
2023-03-02 21:19:11 +11:00
|
|
|
})
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(ast2)) return Promise.reject(ast2)
|
|
|
|
|
|
|
|
const newCode = recast(ast2.modifiedAst)
|
|
|
|
if (err(newCode)) return Promise.reject(newCode)
|
|
|
|
|
2023-02-21 10:50:45 +11:00
|
|
|
return {
|
2024-06-24 11:45:40 -04:00
|
|
|
newCode,
|
2024-11-21 15:04:30 +11:00
|
|
|
originalRange: range,
|
2023-02-21 10:50:45 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-01 17:34:54 -05:00
|
|
|
describe('testing swapping out sketch calls with xLine/xLineTo', () => {
|
2023-02-21 10:50:45 +11:00
|
|
|
const bigExampleArr = [
|
2024-10-02 14:19:40 -05:00
|
|
|
`part001 = startSketchOn('XY')`,
|
2023-10-05 14:27:48 -07:00
|
|
|
` |> startProfileAt([0, 0], %)`,
|
KCL: Use keyword arguments for line, lineTo, extrude and close (#5249)
Part of #4600.
PR: https://github.com/KittyCAD/modeling-app/pull/4826
# Changes to KCL stdlib
- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@sketch, length)`
Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:
```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```
Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as
```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```
Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main
The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:
```
line\(([^=]*), %\)
line(end = $1)
line\((.*), %, (.*)\)
line(end = $1, tag = $2)
lineTo\((.*), %\)
line(endAbsolute = $1)
lineTo\((.*), %, (.*)\)
line(endAbsolute = $1, tag = $2)
extrude\((.*), %\)
extrude(length = $1)
extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)
close\(%, (.*)\)
close(tag = $1)
```
# Selected notes from commits before I squash them all
* Fix test 'yRelative to horizontal distance'
Fixes:
- Make a lineTo helper
- Fix pathToNode to go through the labeled arg .arg property
* Fix test by changing lookups into transformMap
Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.
So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.
* Stop asserting on exact source ranges
When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.
* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.
* Fixed another queryAst test
There were 2 problems:
- Test was looking for the old style of `line` call to choose an offset
for pathToNode
- Test assumed that the `tag` param was always the third one, but in
a kwarg call, you have to look it up by label
* Fix test: traverse was not handling CallExpressionKw
* Fix another test, addTagKw
addTag helper was not aware of kw args.
* Convert close from positional to kwargs
If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.
But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.
The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.
* Fix the last test
Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.
Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.
* Fix many bad regex find-replaces
I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.
Fixes this.
* Fixes test 'Basic sketch › code pane closed at start'
Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.
This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.
* Fix: Optional args given with wrong type were silently ignored
Optional args don't have to be given. But if the user gives them, they
should be the right type.
Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.
Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: Kevin Nadro <kevin@zoo.dev>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-04 08:31:43 -06:00
|
|
|
` |> line(endAbsolute = [1, 1], tag = $abc1)`,
|
|
|
|
` |> line(end = [-2.04, -0.7], tag = $abc2)`,
|
2024-11-25 09:21:55 +13:00
|
|
|
` |> angledLine({ angle = 157, length = 1.69 }, %, $abc3)`,
|
|
|
|
` |> angledLineOfXLength({ angle = 217, length = 0.86 }, %, $abc4)`,
|
|
|
|
` |> angledLineOfYLength({ angle = 104, length = 1.58 }, %, $abc5)`,
|
|
|
|
` |> angledLineToX({ angle = 55, to = -2.89 }, %, $abc6)`,
|
|
|
|
` |> angledLineToY({ angle = 330, to = 2.53 }, %, $abc7)`,
|
2024-06-24 22:39:04 -07:00
|
|
|
` |> xLine(1.47, %, $abc8)`,
|
|
|
|
` |> yLine(1.57, %, $abc9)`,
|
|
|
|
` |> xLineTo(1.49, %, $abc10)`,
|
|
|
|
` |> yLineTo(2.64, %, $abc11)`,
|
KCL: Use keyword arguments for line, lineTo, extrude and close (#5249)
Part of #4600.
PR: https://github.com/KittyCAD/modeling-app/pull/4826
# Changes to KCL stdlib
- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@sketch, length)`
Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:
```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```
Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as
```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```
Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main
The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:
```
line\(([^=]*), %\)
line(end = $1)
line\((.*), %, (.*)\)
line(end = $1, tag = $2)
lineTo\((.*), %\)
line(endAbsolute = $1)
lineTo\((.*), %, (.*)\)
line(endAbsolute = $1, tag = $2)
extrude\((.*), %\)
extrude(length = $1)
extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)
close\(%, (.*)\)
close(tag = $1)
```
# Selected notes from commits before I squash them all
* Fix test 'yRelative to horizontal distance'
Fixes:
- Make a lineTo helper
- Fix pathToNode to go through the labeled arg .arg property
* Fix test by changing lookups into transformMap
Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.
So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.
* Stop asserting on exact source ranges
When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.
* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.
* Fixed another queryAst test
There were 2 problems:
- Test was looking for the old style of `line` call to choose an offset
for pathToNode
- Test assumed that the `tag` param was always the third one, but in
a kwarg call, you have to look it up by label
* Fix test: traverse was not handling CallExpressionKw
* Fix another test, addTagKw
addTag helper was not aware of kw args.
* Convert close from positional to kwargs
If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.
But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.
The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.
* Fix the last test
Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.
Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.
* Fix many bad regex find-replaces
I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.
Fixes this.
* Fixes test 'Basic sketch › code pane closed at start'
Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.
This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.
* Fix: Optional args given with wrong type were silently ignored
Optional args don't have to be given. But if the user gives them, they
should be the right type.
Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.
Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: Kevin Nadro <kevin@zoo.dev>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-04 08:31:43 -06:00
|
|
|
` |> line(endAbsolute = [2.55, 3.58]) // lineTo`,
|
|
|
|
` |> line(end = [0.73, -0.75])`,
|
2023-02-21 10:50:45 +11:00
|
|
|
` |> angledLine([63, 1.38], %) // angledLine`,
|
|
|
|
` |> angledLineOfXLength([319, 1.15], %) // angledLineOfXLength`,
|
|
|
|
` |> angledLineOfYLength([50, 1.35], %) // angledLineOfYLength`,
|
|
|
|
` |> angledLineToX([291, 6.66], %) // angledLineToX`,
|
|
|
|
` |> angledLineToY([228, 2.14], %) // angledLineToY`,
|
|
|
|
` |> xLine(-1.33, %)`,
|
|
|
|
` |> yLine(-1.07, %)`,
|
|
|
|
` |> xLineTo(3.27, %)`,
|
|
|
|
` |> yLineTo(2.14, %)`,
|
|
|
|
]
|
|
|
|
const bigExample = bigExampleArr.join('\n')
|
2023-06-22 16:43:33 +10:00
|
|
|
it('line with tag converts to xLine', async () => {
|
KCL: Use keyword arguments for line, lineTo, extrude and close (#5249)
Part of #4600.
PR: https://github.com/KittyCAD/modeling-app/pull/4826
# Changes to KCL stdlib
- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@sketch, length)`
Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:
```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```
Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as
```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```
Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main
The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:
```
line\(([^=]*), %\)
line(end = $1)
line\((.*), %, (.*)\)
line(end = $1, tag = $2)
lineTo\((.*), %\)
line(endAbsolute = $1)
lineTo\((.*), %, (.*)\)
line(endAbsolute = $1, tag = $2)
extrude\((.*), %\)
extrude(length = $1)
extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)
close\(%, (.*)\)
close(tag = $1)
```
# Selected notes from commits before I squash them all
* Fix test 'yRelative to horizontal distance'
Fixes:
- Make a lineTo helper
- Fix pathToNode to go through the labeled arg .arg property
* Fix test by changing lookups into transformMap
Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.
So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.
* Stop asserting on exact source ranges
When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.
* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.
* Fixed another queryAst test
There were 2 problems:
- Test was looking for the old style of `line` call to choose an offset
for pathToNode
- Test assumed that the `tag` param was always the third one, but in
a kwarg call, you have to look it up by label
* Fix test: traverse was not handling CallExpressionKw
* Fix another test, addTagKw
addTag helper was not aware of kw args.
* Convert close from positional to kwargs
If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.
But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.
The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.
* Fix the last test
Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.
Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.
* Fix many bad regex find-replaces
I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.
Fixes this.
* Fixes test 'Basic sketch › code pane closed at start'
Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.
This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.
* Fix: Optional args given with wrong type were silently ignored
Optional args don't have to be given. But if the user gives them, they
should be the right type.
Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.
Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: Kevin Nadro <kevin@zoo.dev>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-04 08:31:43 -06:00
|
|
|
const callToSwap = 'line(end = [-2.04, -0.7], tag = $abc2)'
|
2024-06-24 22:39:04 -07:00
|
|
|
const expectedLine = 'xLine(-2.04, %, $abc2)'
|
2023-06-22 16:43:33 +10:00
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: bigExample,
|
|
|
|
callToSwap,
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'horizontal',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('line w/o tag converts to xLine', async () => {
|
KCL: Use keyword arguments for line, lineTo, extrude and close (#5249)
Part of #4600.
PR: https://github.com/KittyCAD/modeling-app/pull/4826
# Changes to KCL stdlib
- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@sketch, length)`
Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:
```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```
Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as
```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```
Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main
The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:
```
line\(([^=]*), %\)
line(end = $1)
line\((.*), %, (.*)\)
line(end = $1, tag = $2)
lineTo\((.*), %\)
line(endAbsolute = $1)
lineTo\((.*), %, (.*)\)
line(endAbsolute = $1, tag = $2)
extrude\((.*), %\)
extrude(length = $1)
extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)
close\(%, (.*)\)
close(tag = $1)
```
# Selected notes from commits before I squash them all
* Fix test 'yRelative to horizontal distance'
Fixes:
- Make a lineTo helper
- Fix pathToNode to go through the labeled arg .arg property
* Fix test by changing lookups into transformMap
Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.
So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.
* Stop asserting on exact source ranges
When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.
* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.
* Fixed another queryAst test
There were 2 problems:
- Test was looking for the old style of `line` call to choose an offset
for pathToNode
- Test assumed that the `tag` param was always the third one, but in
a kwarg call, you have to look it up by label
* Fix test: traverse was not handling CallExpressionKw
* Fix another test, addTagKw
addTag helper was not aware of kw args.
* Convert close from positional to kwargs
If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.
But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.
The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.
* Fix the last test
Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.
Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.
* Fix many bad regex find-replaces
I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.
Fixes this.
* Fixes test 'Basic sketch › code pane closed at start'
Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.
This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.
* Fix: Optional args given with wrong type were silently ignored
Optional args don't have to be given. But if the user gives them, they
should be the right type.
Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.
Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: Kevin Nadro <kevin@zoo.dev>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-04 08:31:43 -06:00
|
|
|
const callToSwap = 'line(end = [0.73, -0.75])'
|
2023-02-21 10:50:45 +11:00
|
|
|
const expectedLine = 'xLine(0.73, %)'
|
2023-06-22 16:43:33 +10:00
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: bigExample,
|
|
|
|
callToSwap,
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'horizontal',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('lineTo with tag converts to xLineTo', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: bigExample,
|
KCL: Use keyword arguments for line, lineTo, extrude and close (#5249)
Part of #4600.
PR: https://github.com/KittyCAD/modeling-app/pull/4826
# Changes to KCL stdlib
- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@sketch, length)`
Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:
```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```
Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as
```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```
Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main
The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:
```
line\(([^=]*), %\)
line(end = $1)
line\((.*), %, (.*)\)
line(end = $1, tag = $2)
lineTo\((.*), %\)
line(endAbsolute = $1)
lineTo\((.*), %, (.*)\)
line(endAbsolute = $1, tag = $2)
extrude\((.*), %\)
extrude(length = $1)
extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)
close\(%, (.*)\)
close(tag = $1)
```
# Selected notes from commits before I squash them all
* Fix test 'yRelative to horizontal distance'
Fixes:
- Make a lineTo helper
- Fix pathToNode to go through the labeled arg .arg property
* Fix test by changing lookups into transformMap
Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.
So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.
* Stop asserting on exact source ranges
When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.
* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.
* Fixed another queryAst test
There were 2 problems:
- Test was looking for the old style of `line` call to choose an offset
for pathToNode
- Test assumed that the `tag` param was always the third one, but in
a kwarg call, you have to look it up by label
* Fix test: traverse was not handling CallExpressionKw
* Fix another test, addTagKw
addTag helper was not aware of kw args.
* Convert close from positional to kwargs
If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.
But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.
The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.
* Fix the last test
Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.
Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.
* Fix many bad regex find-replaces
I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.
Fixes this.
* Fixes test 'Basic sketch › code pane closed at start'
Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.
This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.
* Fix: Optional args given with wrong type were silently ignored
Optional args don't have to be given. But if the user gives them, they
should be the right type.
Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.
Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: Kevin Nadro <kevin@zoo.dev>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-04 08:31:43 -06:00
|
|
|
callToSwap: 'line(endAbsolute = [1, 1], tag = $abc1)',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'horizontal',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
2024-06-24 22:39:04 -07:00
|
|
|
const expectedLine = 'xLineTo(1, %, $abc1)'
|
2023-02-21 10:50:45 +11:00
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('lineTo w/o tag converts to xLineTo', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: bigExample,
|
KCL: Use keyword arguments for line, lineTo, extrude and close (#5249)
Part of #4600.
PR: https://github.com/KittyCAD/modeling-app/pull/4826
# Changes to KCL stdlib
- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@sketch, length)`
Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:
```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```
Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as
```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```
Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main
The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:
```
line\(([^=]*), %\)
line(end = $1)
line\((.*), %, (.*)\)
line(end = $1, tag = $2)
lineTo\((.*), %\)
line(endAbsolute = $1)
lineTo\((.*), %, (.*)\)
line(endAbsolute = $1, tag = $2)
extrude\((.*), %\)
extrude(length = $1)
extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)
close\(%, (.*)\)
close(tag = $1)
```
# Selected notes from commits before I squash them all
* Fix test 'yRelative to horizontal distance'
Fixes:
- Make a lineTo helper
- Fix pathToNode to go through the labeled arg .arg property
* Fix test by changing lookups into transformMap
Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.
So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.
* Stop asserting on exact source ranges
When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.
* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.
* Fixed another queryAst test
There were 2 problems:
- Test was looking for the old style of `line` call to choose an offset
for pathToNode
- Test assumed that the `tag` param was always the third one, but in
a kwarg call, you have to look it up by label
* Fix test: traverse was not handling CallExpressionKw
* Fix another test, addTagKw
addTag helper was not aware of kw args.
* Convert close from positional to kwargs
If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.
But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.
The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.
* Fix the last test
Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.
Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.
* Fix many bad regex find-replaces
I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.
Fixes this.
* Fixes test 'Basic sketch › code pane closed at start'
Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.
This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.
* Fix: Optional args given with wrong type were silently ignored
Optional args don't have to be given. But if the user gives them, they
should be the right type.
Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.
Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: Kevin Nadro <kevin@zoo.dev>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-04 08:31:43 -06:00
|
|
|
callToSwap: 'line(endAbsolute = [2.55, 3.58])',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'horizontal',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
|
|
|
const expectedLine = 'xLineTo(2.55, %) // lineTo'
|
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('angledLine with tag converts to xLine', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: bigExample,
|
2024-11-25 09:21:55 +13:00
|
|
|
callToSwap: 'angledLine({ angle = 157, length = 1.69 }, %, $abc3)',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'horizontal',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
2024-06-24 22:39:04 -07:00
|
|
|
const expectedLine = 'xLine(-1.56, %, $abc3)'
|
2024-03-15 17:03:42 -04:00
|
|
|
console.log(newCode)
|
2023-02-21 10:50:45 +11:00
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('angledLine w/o tag converts to xLine', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: bigExample,
|
|
|
|
callToSwap: 'angledLine([63, 1.38], %)',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'horizontal',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
|
|
|
const expectedLine = 'xLine(0.63, %) // angledLine'
|
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('angledLineOfXLength with tag converts to xLine', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: bigExample,
|
2024-11-25 09:21:55 +13:00
|
|
|
callToSwap:
|
|
|
|
'angledLineOfXLength({ angle = 217, length = 0.86 }, %, $abc4)',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'horizontal',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
2024-06-24 22:39:04 -07:00
|
|
|
const expectedLine = 'xLine(-0.86, %, $abc4)'
|
2023-11-01 17:34:54 -05:00
|
|
|
// hmm "-0.86" is correct since the angle is 104, but need to make sure this is compatible `-myVar`
|
2023-02-21 10:50:45 +11:00
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('angledLineOfXLength w/o tag converts to xLine', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: bigExample,
|
|
|
|
callToSwap: 'angledLineOfXLength([319, 1.15], %)',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'horizontal',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
|
|
|
const expectedLine = 'xLine(1.15, %) // angledLineOfXLength'
|
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('angledLineOfYLength with tag converts to yLine', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: bigExample,
|
2024-11-25 09:21:55 +13:00
|
|
|
callToSwap:
|
|
|
|
'angledLineOfYLength({ angle = 104, length = 1.58 }, %, $abc5)',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'vertical',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
2024-06-24 22:39:04 -07:00
|
|
|
const expectedLine = 'yLine(1.58, %, $abc5)'
|
2023-02-21 10:50:45 +11:00
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('angledLineOfYLength w/o tag converts to yLine', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: bigExample,
|
|
|
|
callToSwap: 'angledLineOfYLength([50, 1.35], %)',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'vertical',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
|
|
|
const expectedLine = 'yLine(1.35, %) // angledLineOfYLength'
|
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('angledLineToX with tag converts to xLineTo', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: bigExample,
|
2024-11-25 09:21:55 +13:00
|
|
|
callToSwap: 'angledLineToX({ angle = 55, to = -2.89 }, %, $abc6)',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'horizontal',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
2024-06-24 22:39:04 -07:00
|
|
|
const expectedLine = 'xLineTo(-2.89, %, $abc6)'
|
2023-02-21 10:50:45 +11:00
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('angledLineToX w/o tag converts to xLineTo', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: bigExample,
|
|
|
|
callToSwap: 'angledLineToX([291, 6.66], %)',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'horizontal',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
|
|
|
const expectedLine = 'xLineTo(6.66, %) // angledLineToX'
|
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('angledLineToY with tag converts to yLineTo', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: bigExample,
|
2024-11-25 09:21:55 +13:00
|
|
|
callToSwap: 'angledLineToY({ angle = 330, to = 2.53 }, %, $abc7)',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'vertical',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
2024-06-24 22:39:04 -07:00
|
|
|
const expectedLine = 'yLineTo(2.53, %, $abc7)'
|
2023-02-21 10:50:45 +11:00
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('angledLineToY w/o tag converts to yLineTo', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: bigExample,
|
|
|
|
callToSwap: 'angledLineToY([228, 2.14], %)',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'vertical',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
|
|
|
const expectedLine = 'yLineTo(2.14, %) // angledLineToY'
|
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-11-01 17:34:54 -05:00
|
|
|
describe('testing swapping out sketch calls with xLine/xLineTo while keeping variable/identifiers intact', () => {
|
2023-07-10 15:15:07 +10:00
|
|
|
// Enable rotations #152
|
2023-02-21 10:50:45 +11:00
|
|
|
const variablesExampleArr = [
|
2024-10-02 14:19:40 -05:00
|
|
|
`lineX = -1`,
|
|
|
|
`lineToX = -1.3`,
|
|
|
|
`angledLineAngle = 207`,
|
|
|
|
`angledLineOfXLengthX = 0.8`,
|
|
|
|
`angledLineOfYLengthY = 0.89`,
|
|
|
|
`angledLineToXx = -1.86`,
|
|
|
|
`angledLineToYy = -0.76`,
|
|
|
|
`part001 = startSketchOn('XY')`,
|
2023-10-05 14:27:48 -07:00
|
|
|
` |> startProfileAt([0, 0], %)`,
|
2023-07-10 15:15:07 +10:00
|
|
|
// ` |> rx(90, %)`,
|
KCL: Use keyword arguments for line, lineTo, extrude and close (#5249)
Part of #4600.
PR: https://github.com/KittyCAD/modeling-app/pull/4826
# Changes to KCL stdlib
- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@sketch, length)`
Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:
```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```
Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as
```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```
Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main
The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:
```
line\(([^=]*), %\)
line(end = $1)
line\((.*), %, (.*)\)
line(end = $1, tag = $2)
lineTo\((.*), %\)
line(endAbsolute = $1)
lineTo\((.*), %, (.*)\)
line(endAbsolute = $1, tag = $2)
extrude\((.*), %\)
extrude(length = $1)
extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)
close\(%, (.*)\)
close(tag = $1)
```
# Selected notes from commits before I squash them all
* Fix test 'yRelative to horizontal distance'
Fixes:
- Make a lineTo helper
- Fix pathToNode to go through the labeled arg .arg property
* Fix test by changing lookups into transformMap
Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.
So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.
* Stop asserting on exact source ranges
When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.
* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.
* Fixed another queryAst test
There were 2 problems:
- Test was looking for the old style of `line` call to choose an offset
for pathToNode
- Test assumed that the `tag` param was always the third one, but in
a kwarg call, you have to look it up by label
* Fix test: traverse was not handling CallExpressionKw
* Fix another test, addTagKw
addTag helper was not aware of kw args.
* Convert close from positional to kwargs
If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.
But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.
The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.
* Fix the last test
Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.
Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.
* Fix many bad regex find-replaces
I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.
Fixes this.
* Fixes test 'Basic sketch › code pane closed at start'
Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.
This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.
* Fix: Optional args given with wrong type were silently ignored
Optional args don't have to be given. But if the user gives them, they
should be the right type.
Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.
Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: Kevin Nadro <kevin@zoo.dev>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-04 08:31:43 -06:00
|
|
|
` |> line(endAbsolute = [1, 1])`,
|
|
|
|
` |> line(end = [lineX, 2.13])`,
|
|
|
|
` |> line(endAbsolute = [lineToX, 2.85])`,
|
2023-02-21 10:50:45 +11:00
|
|
|
` |> angledLine([angledLineAngle, 1.64], %)`,
|
|
|
|
` |> angledLineOfXLength([329, angledLineOfXLengthX], %)`,
|
|
|
|
` |> angledLineOfYLength([222, angledLineOfYLengthY], %)`,
|
|
|
|
` |> angledLineToX([330, angledLineToXx], %)`,
|
|
|
|
` |> angledLineToY([217, angledLineToYy], %)`,
|
KCL: Use keyword arguments for line, lineTo, extrude and close (#5249)
Part of #4600.
PR: https://github.com/KittyCAD/modeling-app/pull/4826
# Changes to KCL stdlib
- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@sketch, length)`
Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:
```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```
Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as
```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```
Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main
The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:
```
line\(([^=]*), %\)
line(end = $1)
line\((.*), %, (.*)\)
line(end = $1, tag = $2)
lineTo\((.*), %\)
line(endAbsolute = $1)
lineTo\((.*), %, (.*)\)
line(endAbsolute = $1, tag = $2)
extrude\((.*), %\)
extrude(length = $1)
extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)
close\(%, (.*)\)
close(tag = $1)
```
# Selected notes from commits before I squash them all
* Fix test 'yRelative to horizontal distance'
Fixes:
- Make a lineTo helper
- Fix pathToNode to go through the labeled arg .arg property
* Fix test by changing lookups into transformMap
Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.
So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.
* Stop asserting on exact source ranges
When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.
* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.
* Fixed another queryAst test
There were 2 problems:
- Test was looking for the old style of `line` call to choose an offset
for pathToNode
- Test assumed that the `tag` param was always the third one, but in
a kwarg call, you have to look it up by label
* Fix test: traverse was not handling CallExpressionKw
* Fix another test, addTagKw
addTag helper was not aware of kw args.
* Convert close from positional to kwargs
If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.
But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.
The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.
* Fix the last test
Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.
Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.
* Fix many bad regex find-replaces
I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.
Fixes this.
* Fixes test 'Basic sketch › code pane closed at start'
Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.
This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.
* Fix: Optional args given with wrong type were silently ignored
Optional args don't have to be given. But if the user gives them, they
should be the right type.
Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.
Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: Kevin Nadro <kevin@zoo.dev>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-04 08:31:43 -06:00
|
|
|
` |> line(end = [0.89, -0.1])`,
|
2023-02-21 10:50:45 +11:00
|
|
|
]
|
|
|
|
const varExample = variablesExampleArr.join('\n')
|
2023-06-22 16:43:33 +10:00
|
|
|
it('line keeps variable when converted to xLine', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: varExample,
|
KCL: Use keyword arguments for line, lineTo, extrude and close (#5249)
Part of #4600.
PR: https://github.com/KittyCAD/modeling-app/pull/4826
# Changes to KCL stdlib
- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@sketch, length)`
Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:
```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```
Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as
```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```
Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main
The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:
```
line\(([^=]*), %\)
line(end = $1)
line\((.*), %, (.*)\)
line(end = $1, tag = $2)
lineTo\((.*), %\)
line(endAbsolute = $1)
lineTo\((.*), %, (.*)\)
line(endAbsolute = $1, tag = $2)
extrude\((.*), %\)
extrude(length = $1)
extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)
close\(%, (.*)\)
close(tag = $1)
```
# Selected notes from commits before I squash them all
* Fix test 'yRelative to horizontal distance'
Fixes:
- Make a lineTo helper
- Fix pathToNode to go through the labeled arg .arg property
* Fix test by changing lookups into transformMap
Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.
So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.
* Stop asserting on exact source ranges
When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.
* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.
* Fixed another queryAst test
There were 2 problems:
- Test was looking for the old style of `line` call to choose an offset
for pathToNode
- Test assumed that the `tag` param was always the third one, but in
a kwarg call, you have to look it up by label
* Fix test: traverse was not handling CallExpressionKw
* Fix another test, addTagKw
addTag helper was not aware of kw args.
* Convert close from positional to kwargs
If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.
But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.
The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.
* Fix the last test
Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.
Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.
* Fix many bad regex find-replaces
I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.
Fixes this.
* Fixes test 'Basic sketch › code pane closed at start'
Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.
This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.
* Fix: Optional args given with wrong type were silently ignored
Optional args don't have to be given. But if the user gives them, they
should be the right type.
Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.
Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: Kevin Nadro <kevin@zoo.dev>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-04 08:31:43 -06:00
|
|
|
callToSwap: 'line(end = [lineX, 2.13])',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'horizontal',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
|
|
|
const expectedLine = 'xLine(lineX, %)'
|
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('lineTo keeps variable when converted to xLineTo', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: varExample,
|
KCL: Use keyword arguments for line, lineTo, extrude and close (#5249)
Part of #4600.
PR: https://github.com/KittyCAD/modeling-app/pull/4826
# Changes to KCL stdlib
- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@sketch, length)`
Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:
```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```
Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as
```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```
Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main
The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:
```
line\(([^=]*), %\)
line(end = $1)
line\((.*), %, (.*)\)
line(end = $1, tag = $2)
lineTo\((.*), %\)
line(endAbsolute = $1)
lineTo\((.*), %, (.*)\)
line(endAbsolute = $1, tag = $2)
extrude\((.*), %\)
extrude(length = $1)
extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)
close\(%, (.*)\)
close(tag = $1)
```
# Selected notes from commits before I squash them all
* Fix test 'yRelative to horizontal distance'
Fixes:
- Make a lineTo helper
- Fix pathToNode to go through the labeled arg .arg property
* Fix test by changing lookups into transformMap
Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.
So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.
* Stop asserting on exact source ranges
When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.
* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.
* Fixed another queryAst test
There were 2 problems:
- Test was looking for the old style of `line` call to choose an offset
for pathToNode
- Test assumed that the `tag` param was always the third one, but in
a kwarg call, you have to look it up by label
* Fix test: traverse was not handling CallExpressionKw
* Fix another test, addTagKw
addTag helper was not aware of kw args.
* Convert close from positional to kwargs
If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.
But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.
The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.
* Fix the last test
Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.
Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.
* Fix many bad regex find-replaces
I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.
Fixes this.
* Fixes test 'Basic sketch › code pane closed at start'
Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.
This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.
* Fix: Optional args given with wrong type were silently ignored
Optional args don't have to be given. But if the user gives them, they
should be the right type.
Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.
Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: Kevin Nadro <kevin@zoo.dev>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-04 08:31:43 -06:00
|
|
|
callToSwap: 'line(endAbsolute = [lineToX, 2.85])',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'horizontal',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
|
|
|
const expectedLine = 'xLineTo(lineToX, %)'
|
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('angledLineOfXLength keeps variable when converted to xLine', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: varExample,
|
|
|
|
callToSwap: 'angledLineOfXLength([329, angledLineOfXLengthX], %)',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'horizontal',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
|
|
|
const expectedLine = 'xLine(angledLineOfXLengthX, %)'
|
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('angledLineOfYLength keeps variable when converted to yLine', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: varExample,
|
|
|
|
callToSwap: 'angledLineOfYLength([222, angledLineOfYLengthY], %)',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'vertical',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
2023-03-02 21:19:11 +11:00
|
|
|
const expectedLine = 'yLine(-angledLineOfYLengthY, %)'
|
2023-02-21 10:50:45 +11:00
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('angledLineToX keeps variable when converted to xLineTo', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: varExample,
|
|
|
|
callToSwap: 'angledLineToX([330, angledLineToXx], %)',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'horizontal',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
|
|
|
const expectedLine = 'xLineTo(angledLineToXx, %)'
|
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('angledLineToY keeps variable when converted to yLineTo', async () => {
|
|
|
|
const { newCode, originalRange } = await testingSwapSketchFnCall({
|
2023-02-21 10:50:45 +11:00
|
|
|
inputCode: varExample,
|
|
|
|
callToSwap: 'angledLineToY([217, angledLineToYy], %)',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'vertical',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
|
|
|
const expectedLine = 'yLineTo(angledLineToYy, %)'
|
|
|
|
expect(newCode).toContain(expectedLine)
|
|
|
|
// new line should start at the same place as the old line
|
|
|
|
expect(originalRange[0]).toBe(newCode.indexOf(expectedLine))
|
|
|
|
})
|
|
|
|
|
2023-06-22 16:43:33 +10:00
|
|
|
it('trying to convert angledLineToY to xLineTo should not work because of the variable', async () => {
|
2023-02-21 10:50:45 +11:00
|
|
|
const illegalConvert = () =>
|
|
|
|
testingSwapSketchFnCall({
|
|
|
|
inputCode: varExample,
|
|
|
|
callToSwap: 'angledLineToY([217, angledLineToYy], %)',
|
2023-03-02 21:19:11 +11:00
|
|
|
constraintType: 'horizontal',
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
await expect(illegalConvert).rejects.toThrowError('no callback helper')
|
2023-02-21 10:50:45 +11:00
|
|
|
})
|
|
|
|
})
|
2023-03-20 07:09:19 +11:00
|
|
|
|
|
|
|
describe('testing getSketchSegmentIndexFromSourceRange', () => {
|
|
|
|
const code = `
|
2024-10-02 14:19:40 -05:00
|
|
|
part001 = startSketchOn('XY')
|
2023-10-05 14:27:48 -07:00
|
|
|
|> startProfileAt([0, 0.04], %) // segment-in-start
|
KCL: Use keyword arguments for line, lineTo, extrude and close (#5249)
Part of #4600.
PR: https://github.com/KittyCAD/modeling-app/pull/4826
# Changes to KCL stdlib
- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@sketch, length)`
Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:
```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```
Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as
```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```
Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main
The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:
```
line\(([^=]*), %\)
line(end = $1)
line\((.*), %, (.*)\)
line(end = $1, tag = $2)
lineTo\((.*), %\)
line(endAbsolute = $1)
lineTo\((.*), %, (.*)\)
line(endAbsolute = $1, tag = $2)
extrude\((.*), %\)
extrude(length = $1)
extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)
close\(%, (.*)\)
close(tag = $1)
```
# Selected notes from commits before I squash them all
* Fix test 'yRelative to horizontal distance'
Fixes:
- Make a lineTo helper
- Fix pathToNode to go through the labeled arg .arg property
* Fix test by changing lookups into transformMap
Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.
So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.
* Stop asserting on exact source ranges
When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.
* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.
* Fixed another queryAst test
There were 2 problems:
- Test was looking for the old style of `line` call to choose an offset
for pathToNode
- Test assumed that the `tag` param was always the third one, but in
a kwarg call, you have to look it up by label
* Fix test: traverse was not handling CallExpressionKw
* Fix another test, addTagKw
addTag helper was not aware of kw args.
* Convert close from positional to kwargs
If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.
But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.
The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.
* Fix the last test
Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.
Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.
* Fix many bad regex find-replaces
I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.
Fixes this.
* Fixes test 'Basic sketch › code pane closed at start'
Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.
This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.
* Fix: Optional args given with wrong type were silently ignored
Optional args don't have to be given. But if the user gives them, they
should be the right type.
Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.
Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: Kevin Nadro <kevin@zoo.dev>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-04 08:31:43 -06:00
|
|
|
|> line(end = [0, 0.4])
|
2023-03-20 07:09:19 +11:00
|
|
|
|> xLine(3.48, %)
|
KCL: Use keyword arguments for line, lineTo, extrude and close (#5249)
Part of #4600.
PR: https://github.com/KittyCAD/modeling-app/pull/4826
# Changes to KCL stdlib
- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@sketch, length)`
Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:
```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```
Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as
```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```
Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main
The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:
```
line\(([^=]*), %\)
line(end = $1)
line\((.*), %, (.*)\)
line(end = $1, tag = $2)
lineTo\((.*), %\)
line(endAbsolute = $1)
lineTo\((.*), %, (.*)\)
line(endAbsolute = $1, tag = $2)
extrude\((.*), %\)
extrude(length = $1)
extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)
close\(%, (.*)\)
close(tag = $1)
```
# Selected notes from commits before I squash them all
* Fix test 'yRelative to horizontal distance'
Fixes:
- Make a lineTo helper
- Fix pathToNode to go through the labeled arg .arg property
* Fix test by changing lookups into transformMap
Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.
So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.
* Stop asserting on exact source ranges
When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.
* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.
* Fixed another queryAst test
There were 2 problems:
- Test was looking for the old style of `line` call to choose an offset
for pathToNode
- Test assumed that the `tag` param was always the third one, but in
a kwarg call, you have to look it up by label
* Fix test: traverse was not handling CallExpressionKw
* Fix another test, addTagKw
addTag helper was not aware of kw args.
* Convert close from positional to kwargs
If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.
But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.
The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.
* Fix the last test
Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.
Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.
* Fix many bad regex find-replaces
I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.
Fixes this.
* Fixes test 'Basic sketch › code pane closed at start'
Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.
This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.
* Fix: Optional args given with wrong type were silently ignored
Optional args don't have to be given. But if the user gives them, they
should be the right type.
Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.
Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: Kevin Nadro <kevin@zoo.dev>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-04 08:31:43 -06:00
|
|
|
|> line(end = [2.14, 1.35]) // normal-segment
|
2024-03-01 17:16:18 -08:00
|
|
|
|> xLine(3.54, %)`
|
2023-06-22 16:43:33 +10:00
|
|
|
it('normal case works', async () => {
|
2024-12-06 13:57:31 +13:00
|
|
|
const execState = await enginelessExecutor(assertParse(code))
|
2023-03-20 07:09:19 +11:00
|
|
|
const index = code.indexOf('// normal-segment') - 7
|
2024-09-27 15:44:44 -07:00
|
|
|
const sg = sketchFromKclValue(
|
2025-02-12 10:22:56 +13:00
|
|
|
execState.variables['part001'],
|
Remove KclValue::SketchGroup variant (#3446)
We can store Rust types like `SketchGroup` as their own variant of `KclValue`, or as `KclValue::UserVal`. Sometimes we store in one and try to read from the other, which fails. This causes bugs, like #3338.
Instead, we should use either ::SketchGroup or ::UserVal, and stop using the other. If we stopped using ::UserVal, we'd need a new variant for every Rust type we wanted to build, including user-defined types. So I don't think that's practical.
Instead, we should store every KCL value by de/serializing it into UserVal. This is a first step along that path, removing just the SketchGroup variants. If it goes well, we can remove the other specialized variants too.
My only concern is there might be performance implications from how frequently we convert between serde_json::Value and Rust types via Serde. But I'm not too worried -- there's no parsing JSON strings, just traversing serde_json::Value trees. This isn't great for performance but I think it'll probably be miniscule in comparison to doing all the API calls.
2024-08-21 11:06:48 -05:00
|
|
|
'part001'
|
2024-09-27 15:44:44 -07:00
|
|
|
) as Sketch
|
2025-01-17 14:34:36 -05:00
|
|
|
const _segment = getSketchSegmentFromSourceRange(
|
|
|
|
sg,
|
|
|
|
topLevelRange(index, index)
|
|
|
|
)
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(_segment)) throw _segment
|
|
|
|
const { __geoMeta, ...segment } = _segment.segment
|
2023-03-20 07:09:19 +11:00
|
|
|
expect(segment).toEqual({
|
2024-02-11 12:59:00 +11:00
|
|
|
type: 'ToPoint',
|
2023-03-20 07:09:19 +11:00
|
|
|
to: [5.62, 1.79],
|
|
|
|
from: [3.48, 0.44],
|
2024-06-24 14:45:07 -07:00
|
|
|
tag: null,
|
2023-03-20 07:09:19 +11:00
|
|
|
})
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
it('verify it works when the segment is in the `start` property', async () => {
|
2024-12-06 13:57:31 +13:00
|
|
|
const execState = await enginelessExecutor(assertParse(code))
|
2023-03-20 07:09:19 +11:00
|
|
|
const index = code.indexOf('// segment-in-start') - 7
|
2024-06-24 11:45:40 -04:00
|
|
|
const _segment = getSketchSegmentFromSourceRange(
|
2025-02-12 10:22:56 +13:00
|
|
|
sketchFromKclValue(execState.variables['part001'], 'part001') as Sketch,
|
2025-01-17 14:34:36 -05:00
|
|
|
topLevelRange(index, index)
|
2024-06-24 11:45:40 -04:00
|
|
|
)
|
|
|
|
if (err(_segment)) throw _segment
|
|
|
|
const { __geoMeta, ...segment } = _segment.segment
|
2023-09-12 18:10:27 -07:00
|
|
|
expect(segment).toEqual({
|
|
|
|
to: [0, 0.04],
|
|
|
|
from: [0, 0.04],
|
2024-06-24 14:45:07 -07:00
|
|
|
tag: null,
|
2024-02-11 12:59:00 +11:00
|
|
|
type: 'Base',
|
2023-09-12 18:10:27 -07:00
|
|
|
})
|
2023-03-20 07:09:19 +11:00
|
|
|
})
|
|
|
|
})
|