Fix to not filter out empty modules from the Feature Tree (#6224)

This commit is contained in:
Jonathan Tran
2025-04-08 19:56:27 -04:00
committed by GitHub
parent 4c6ef841bb
commit a98d5aa2fb
2 changed files with 51 additions and 7 deletions

View File

@ -27,12 +27,31 @@ function userCall(name: string): Operation {
sourceRange: defaultSourceRange(), sourceRange: defaultSourceRange(),
} }
} }
function userReturn(): Operation { function userReturn(): Operation {
return { return {
type: 'GroupEnd', type: 'GroupEnd',
} }
} }
function moduleBegin(name: string): Operation {
return {
type: 'GroupBegin',
group: {
type: 'ModuleInstance',
name,
moduleId: 0,
},
sourceRange: defaultSourceRange(),
}
}
function moduleEnd(): Operation {
return {
type: 'GroupEnd',
}
}
describe('operations filtering', () => { describe('operations filtering', () => {
it('drops stdlib operations inside a user-defined function call', async () => { it('drops stdlib operations inside a user-defined function call', async () => {
const operations = [ const operations = [
@ -65,6 +84,25 @@ describe('operations filtering', () => {
const actual = filterOperations(operations) const actual = filterOperations(operations)
expect(actual).toEqual([stdlib('std1'), stdlib('std2'), stdlib('std3')]) expect(actual).toEqual([stdlib('std1'), stdlib('std2'), stdlib('std3')])
}) })
it('does not drop module instances that contain no operations', async () => {
const operations = [
stdlib('std1'),
moduleBegin('foo'),
moduleEnd(),
stdlib('std2'),
moduleBegin('bar'),
moduleEnd(),
stdlib('std3'),
]
const actual = filterOperations(operations)
expect(actual).toEqual([
stdlib('std1'),
moduleBegin('foo'),
stdlib('std2'),
moduleBegin('bar'),
stdlib('std3'),
])
})
it('preserves user-defined function calls at the end of the list', async () => { it('preserves user-defined function calls at the end of the list', async () => {
const operations = [stdlib('std1'), userCall('foo')] const operations = [stdlib('std1'), userCall('foo')]
const actual = filterOperations(operations) const actual = filterOperations(operations)

View File

@ -1168,7 +1168,7 @@ export function filterOperations(operations: Operation[]): Operation[] {
* for use in the feature tree UI * for use in the feature tree UI
*/ */
const operationFilters = [ const operationFilters = [
isNotGroupWithNoOperations, isNotUserFunctionWithNoOperations,
isNotInsideGroup, isNotInsideGroup,
isNotGroupEnd, isNotGroupEnd,
] ]
@ -1202,22 +1202,28 @@ function isNotInsideGroup(operations: Operation[]): Operation[] {
/** /**
* A filter to exclude GroupBegin operations and their corresponding GroupEnd * A filter to exclude GroupBegin operations and their corresponding GroupEnd
* that don't have any operations inside them from a list of operations. * that don't have any operations inside them from a list of operations, if it's
* a function call.
*/ */
function isNotGroupWithNoOperations(operations: Operation[]): Operation[] { function isNotUserFunctionWithNoOperations(
operations: Operation[]
): Operation[] {
return operations.filter((op, index) => { return operations.filter((op, index) => {
if ( if (
op.type === 'GroupBegin' && op.type === 'GroupBegin' &&
// If this is a begin at the end of the array, it's preserved. op.group.type === 'FunctionCall' &&
// If this is a "begin" at the end of the array, it's preserved.
index < operations.length - 1 && index < operations.length - 1 &&
operations[index + 1].type === 'GroupEnd' operations[index + 1].type === 'GroupEnd'
) )
return false return false
const previousOp = index > 0 ? operations[index - 1] : undefined
if ( if (
op.type === 'GroupEnd' && op.type === 'GroupEnd' &&
// If this is an end at the beginning of the array, it's preserved. // If this is an "end" at the beginning of the array, it's preserved.
index > 0 && previousOp !== undefined &&
operations[index - 1].type === 'GroupBegin' previousOp.type === 'GroupBegin' &&
previousOp.group.type === 'FunctionCall'
) )
return false return false