-
Notifications
You must be signed in to change notification settings - Fork 14k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: CodeNodeEditor walk cannot read properties of null (#11129)
Co-authored-by: Elias Meire <[email protected]>
- Loading branch information
1 parent
40dd02f
commit d99e0a7
Showing
2 changed files
with
61 additions
and
13 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
packages/editor-ui/src/components/CodeNodeEditor/utils.test.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,44 @@ | ||
import * as esprima from 'esprima-next'; | ||
import { walk } from './utils'; | ||
|
||
describe('CodeNodeEditor utils', () => { | ||
describe('walk', () => { | ||
it('should find the correct syntax nodes', () => { | ||
const code = `const x = 'a' | ||
function f(arg) { | ||
arg['b'] = 1 | ||
return arg | ||
} | ||
const y = f({ a: 'c' }) | ||
`; | ||
const program = esprima.parse(code); | ||
const stringLiterals = walk( | ||
program, | ||
(node) => node.type === esprima.Syntax.Literal && typeof node.value === 'string', | ||
); | ||
expect(stringLiterals).toEqual([ | ||
new esprima.Literal('a', "'a'"), | ||
new esprima.Literal('b', "'b'"), | ||
new esprima.Literal('c', "'c'"), | ||
]); | ||
}); | ||
|
||
it('should handle null syntax nodes', () => { | ||
// ,, in [1,,2] results in a `null` ArrayExpressionElement | ||
const code = 'const fn = () => [1,,2]'; | ||
const program = esprima.parse(code); | ||
const arrayExpressions = walk( | ||
program, | ||
(node) => node.type === esprima.Syntax.ArrayExpression, | ||
); | ||
expect(arrayExpressions).toEqual([ | ||
new esprima.ArrayExpression([ | ||
new esprima.Literal(1, '1'), | ||
null, | ||
new esprima.Literal(2, '2'), | ||
]), | ||
]); | ||
}); | ||
}); | ||
}); |
30 changes: 17 additions & 13 deletions
30
packages/editor-ui/src/components/CodeNodeEditor/utils.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