-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(editor) Handle optional chaining of expressions. (#4992)
- Added `optionallyChained` field to `JSPropertyAccess` and `JSElementAccess`. - Updated `jsxAttributeToValue` to handle optionally chained versions of the two expression types that support them. - `createJSElementAccess` and `createJSPropertyAccess` now check to see if the expression has the `questionDotToken` property and set `optionallyChained` appropriately, with the old handling for this property being removed. - Updated `jsxAttributeToExpression` to add in the `QuestionDotToken` token if `optionallyChained` is set. - Fixed an issue with `outermostWrapInBraces` where it should only wrap a value if the `expressionContext` value is `jsx`. - Added `jsxAttributeToValue` tests for identifier, property and element accesses. - Refactored out some exception handling into an outer function for `jsxAttributeToValue`. - Added `resultOrError` utility function.
- Loading branch information
1 parent
44c39a4
commit d9411e8
Showing
13 changed files
with
805 additions
and
145 deletions.
There are no files selected for viewing
150 changes: 100 additions & 50 deletions
150
editor/src/components/canvas/__snapshots__/ui-jsx-canvas.spec.tsx.snap
Large diffs are not rendered by default.
Oops, something went wrong.
318 changes: 318 additions & 0 deletions
318
editor/src/components/canvas/ui-jsx-canvas-renderer/jsx-element-child-to-text.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,318 @@ | ||
import { isLeft } from '../../../core/shared/either' | ||
import type { JSElementAccess } from '../../../core/shared/element-template' | ||
import { | ||
emptyComments, | ||
isJSElementAccess, | ||
isJSXElement, | ||
isUtopiaJSXComponent, | ||
jsElementAccess, | ||
jsIdentifier, | ||
jsPropertyAccess, | ||
} from '../../../core/shared/element-template' | ||
import { fromField, fromTypeGuard, traverseArray } from '../../../core/shared/optics/optic-creators' | ||
import { toFirst } from '../../../core/shared/optics/optic-utilities' | ||
import type { Optic } from '../../../core/shared/optics/optics' | ||
import type { ParsedTextFile } from '../../../core/shared/project-file-types' | ||
import { isParseSuccess } from '../../../core/shared/project-file-types' | ||
import { emptySet } from '../../../core/shared/set-utils' | ||
import { lintAndParse } from '../../../core/workers/parser-printer/parser-printer' | ||
import { jsxElementChildToText } from './jsx-element-child-to-text' | ||
|
||
describe('jsxElementChildToText', () => { | ||
it('identifier (jsx and outermost)', () => { | ||
const identifier = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const actualResult = jsxElementChildToText(identifier, null, null, 'jsx', 'outermost') | ||
expect(actualResult).toEqual('{anIdentifier}') | ||
}) | ||
it('identifier (javascript and outermost)', () => { | ||
const identifier = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const actualResult = jsxElementChildToText(identifier, null, null, 'javascript', 'outermost') | ||
expect(actualResult).toEqual('anIdentifier') | ||
}) | ||
it('identifier (jsx and inner)', () => { | ||
const identifier = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const actualResult = jsxElementChildToText(identifier, null, null, 'jsx', 'inner') | ||
expect(actualResult).toEqual('anIdentifier') | ||
}) | ||
it('identifier (javascript and inner)', () => { | ||
const identifier = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const actualResult = jsxElementChildToText(identifier, null, null, 'javascript', 'inner') | ||
expect(actualResult).toEqual('anIdentifier') | ||
}) | ||
it('property access (not optionally chained, jsx and outermost)', () => { | ||
const onValue = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const propertyAccess = jsPropertyAccess( | ||
onValue, | ||
'someProperty', | ||
'cba', | ||
null, | ||
emptyComments, | ||
'anIdentifier.someProperty', | ||
'not-optionally-chained', | ||
) | ||
const actualResult = jsxElementChildToText(propertyAccess, null, null, 'jsx', 'outermost') | ||
expect(actualResult).toEqual('{anIdentifier.someProperty}') | ||
}) | ||
it('property access (not optionally chained, javascript and outermost)', () => { | ||
const onValue = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const propertyAccess = jsPropertyAccess( | ||
onValue, | ||
'someProperty', | ||
'cba', | ||
null, | ||
emptyComments, | ||
'anIdentifier.someProperty', | ||
'not-optionally-chained', | ||
) | ||
const actualResult = jsxElementChildToText( | ||
propertyAccess, | ||
null, | ||
null, | ||
'javascript', | ||
'outermost', | ||
) | ||
expect(actualResult).toEqual('anIdentifier.someProperty') | ||
}) | ||
it('property access (not optionally chained, jsx and inner)', () => { | ||
const onValue = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const propertyAccess = jsPropertyAccess( | ||
onValue, | ||
'someProperty', | ||
'cba', | ||
null, | ||
emptyComments, | ||
'anIdentifier.someProperty', | ||
'not-optionally-chained', | ||
) | ||
const actualResult = jsxElementChildToText(propertyAccess, null, null, 'jsx', 'inner') | ||
expect(actualResult).toEqual('anIdentifier.someProperty') | ||
}) | ||
it('property access (not optionally chained, javascript and inner)', () => { | ||
const onValue = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const propertyAccess = jsPropertyAccess( | ||
onValue, | ||
'someProperty', | ||
'cba', | ||
null, | ||
emptyComments, | ||
'anIdentifier.someProperty', | ||
'not-optionally-chained', | ||
) | ||
const actualResult = jsxElementChildToText(propertyAccess, null, null, 'javascript', 'inner') | ||
expect(actualResult).toEqual('anIdentifier.someProperty') | ||
}) | ||
it('property access (optionally chained, jsx and outermost)', () => { | ||
const onValue = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const propertyAccess = jsPropertyAccess( | ||
onValue, | ||
'someProperty', | ||
'cba', | ||
null, | ||
emptyComments, | ||
'anIdentifier?.someProperty', | ||
'optionally-chained', | ||
) | ||
const actualResult = jsxElementChildToText(propertyAccess, null, null, 'jsx', 'outermost') | ||
expect(actualResult).toEqual('{anIdentifier?.someProperty}') | ||
}) | ||
it('property access (optionally chained, javascript and outermost)', () => { | ||
const onValue = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const propertyAccess = jsPropertyAccess( | ||
onValue, | ||
'someProperty', | ||
'cba', | ||
null, | ||
emptyComments, | ||
'anIdentifier?.someProperty', | ||
'optionally-chained', | ||
) | ||
const actualResult = jsxElementChildToText( | ||
propertyAccess, | ||
null, | ||
null, | ||
'javascript', | ||
'outermost', | ||
) | ||
expect(actualResult).toEqual('anIdentifier?.someProperty') | ||
}) | ||
it('property access (optionally chained, jsx and inner)', () => { | ||
const onValue = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const propertyAccess = jsPropertyAccess( | ||
onValue, | ||
'someProperty', | ||
'cba', | ||
null, | ||
emptyComments, | ||
'anIdentifier?.someProperty', | ||
'optionally-chained', | ||
) | ||
const actualResult = jsxElementChildToText(propertyAccess, null, null, 'jsx', 'inner') | ||
expect(actualResult).toEqual('anIdentifier?.someProperty') | ||
}) | ||
it('property access (optionally chained, javascript and inner)', () => { | ||
const onValue = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const propertyAccess = jsPropertyAccess( | ||
onValue, | ||
'someProperty', | ||
'cba', | ||
null, | ||
emptyComments, | ||
'anIdentifier?.someProperty', | ||
'optionally-chained', | ||
) | ||
const actualResult = jsxElementChildToText(propertyAccess, null, null, 'javascript', 'inner') | ||
expect(actualResult).toEqual('anIdentifier?.someProperty') | ||
}) | ||
it('element access (not optionally chained, jsx and outermost)', () => { | ||
const onValue = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const element = jsIdentifier('elementIdentifier', 'xyz', null, emptyComments) | ||
const elementAccess = jsElementAccess( | ||
onValue, | ||
element, | ||
'cba', | ||
null, | ||
emptyComments, | ||
'anIdentifier[elementIdentifier]', | ||
'not-optionally-chained', | ||
) | ||
const actualResult = jsxElementChildToText(elementAccess, null, null, 'jsx', 'outermost') | ||
expect(actualResult).toEqual('{anIdentifier[elementIdentifier]}') | ||
}) | ||
it('element access (not optionally chained, javascript and outermost)', () => { | ||
const onValue = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const element = jsIdentifier('elementIdentifier', 'xyz', null, emptyComments) | ||
const elementAccess = jsElementAccess( | ||
onValue, | ||
element, | ||
'cba', | ||
null, | ||
emptyComments, | ||
'anIdentifier[elementIdentifier]', | ||
'not-optionally-chained', | ||
) | ||
const actualResult = jsxElementChildToText(elementAccess, null, null, 'javascript', 'outermost') | ||
expect(actualResult).toEqual('anIdentifier[elementIdentifier]') | ||
}) | ||
it('element access (not optionally chained, jsx and inner)', () => { | ||
const onValue = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const element = jsIdentifier('elementIdentifier', 'xyz', null, emptyComments) | ||
const elementAccess = jsElementAccess( | ||
onValue, | ||
element, | ||
'cba', | ||
null, | ||
emptyComments, | ||
'anIdentifier[elementIdentifier]', | ||
'not-optionally-chained', | ||
) | ||
const actualResult = jsxElementChildToText(elementAccess, null, null, 'jsx', 'inner') | ||
expect(actualResult).toEqual('anIdentifier[elementIdentifier]') | ||
}) | ||
it('element access (not optionally chained, javascript and inner)', () => { | ||
const onValue = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const element = jsIdentifier('elementIdentifier', 'xyz', null, emptyComments) | ||
const elementAccess = jsElementAccess( | ||
onValue, | ||
element, | ||
'cba', | ||
null, | ||
emptyComments, | ||
'anIdentifier[elementIdentifier]', | ||
'not-optionally-chained', | ||
) | ||
const actualResult = jsxElementChildToText(elementAccess, null, null, 'javascript', 'inner') | ||
expect(actualResult).toEqual('anIdentifier[elementIdentifier]') | ||
}) | ||
it('element access (optionally chained, jsx and outermost)', () => { | ||
const onValue = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const element = jsIdentifier('elementIdentifier', 'xyz', null, emptyComments) | ||
const elementAccess = jsElementAccess( | ||
onValue, | ||
element, | ||
'cba', | ||
null, | ||
emptyComments, | ||
'anIdentifier?.[elementIdentifier]', | ||
'optionally-chained', | ||
) | ||
const actualResult = jsxElementChildToText(elementAccess, null, null, 'jsx', 'outermost') | ||
expect(actualResult).toEqual('{anIdentifier?.[elementIdentifier]}') | ||
}) | ||
it('element access (optionally chained, javascript and outermost)', () => { | ||
const onValue = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const element = jsIdentifier('elementIdentifier', 'xyz', null, emptyComments) | ||
const elementAccess = jsElementAccess( | ||
onValue, | ||
element, | ||
'cba', | ||
null, | ||
emptyComments, | ||
'anIdentifier?.[elementIdentifier]', | ||
'optionally-chained', | ||
) | ||
const actualResult = jsxElementChildToText(elementAccess, null, null, 'javascript', 'outermost') | ||
expect(actualResult).toEqual('anIdentifier?.[elementIdentifier]') | ||
}) | ||
it('element access (optionally chained, jsx and inner)', () => { | ||
const onValue = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const element = jsIdentifier('elementIdentifier', 'xyz', null, emptyComments) | ||
const elementAccess = jsElementAccess( | ||
onValue, | ||
element, | ||
'cba', | ||
null, | ||
emptyComments, | ||
'anIdentifier?.[elementIdentifier]', | ||
'optionally-chained', | ||
) | ||
const actualResult = jsxElementChildToText(elementAccess, null, null, 'jsx', 'inner') | ||
expect(actualResult).toEqual('anIdentifier?.[elementIdentifier]') | ||
}) | ||
it('element access (optionally chained, javascript and inner)', () => { | ||
const onValue = jsIdentifier('anIdentifier', 'abc', null, emptyComments) | ||
const element = jsIdentifier('elementIdentifier', 'xyz', null, emptyComments) | ||
const elementAccess = jsElementAccess( | ||
onValue, | ||
element, | ||
'cba', | ||
null, | ||
emptyComments, | ||
'anIdentifier?.[elementIdentifier]', | ||
'optionally-chained', | ||
) | ||
const actualResult = jsxElementChildToText(elementAccess, null, null, 'javascript', 'inner') | ||
expect(actualResult).toEqual('anIdentifier?.[elementIdentifier]') | ||
}) | ||
it('complicated case', () => { | ||
const parsedResult: ParsedTextFile = lintAndParse( | ||
'test.js', | ||
`const TestComponent = (props) => <div>{something()?.[another().property?.deeperProperty]}</div>`, | ||
null, | ||
emptySet(), | ||
'do-not-trim-bounds', | ||
'do-not-apply-steganography', | ||
) | ||
const toExpressionOptic: Optic<ParsedTextFile, JSElementAccess> = fromTypeGuard(isParseSuccess) | ||
.compose(fromField('topLevelElements')) | ||
.compose(traverseArray()) | ||
.compose(fromTypeGuard(isUtopiaJSXComponent)) | ||
.compose(fromField('rootElement')) | ||
.compose(fromTypeGuard(isJSXElement)) | ||
.compose(fromField('children')) | ||
.compose(traverseArray()) | ||
.compose(fromTypeGuard(isJSElementAccess)) | ||
const jsExpression = toFirst(toExpressionOptic, parsedResult) | ||
if (isLeft(jsExpression)) { | ||
throw new Error(`Unable to obtain expression from ${JSON.stringify(parsedResult, null, 2)}`) | ||
} else { | ||
const actualResult = jsxElementChildToText( | ||
jsExpression.value, | ||
null, | ||
null, | ||
'javascript', | ||
'outermost', | ||
) | ||
expect(actualResult).toEqual(`something()?.[another().property?.deeperProperty]`) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.