Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Asset Selection] Make submitting an invalid query with only a single value convert to a key substring filter. #28435

Merged
merged 2 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {isUnmatchedValueQuery} from '../isUnmatchedValueQuery';

describe('isUnmatchedValueQuery', () => {
it('should return true for unmatched value queries', () => {
expect(isUnmatchedValueQuery('key:"value"')).toBe(false);
expect(isUnmatchedValueQuery('one two')).toBe(false);
expect(isUnmatchedValueQuery('one')).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {useCallback} from 'react';
import {useAssetSelectionAutoCompleteProvider as defaultUseAssetSelectionAutoCompleteProvider} from 'shared/asset-selection/input/useAssetSelectionAutoCompleteProvider.oss';

import {assetSelectionSyntaxSupportedAttributes, unsupportedAttributeMessages} from './util';
Expand All @@ -8,6 +9,8 @@ import {SelectionAutoCompleteInput} from '../../selection/SelectionInput';
import {createSelectionLinter} from '../../selection/createSelectionLinter';
import {AssetSelectionLexer} from '../generated/AssetSelectionLexer';
import {AssetSelectionParser} from '../generated/AssetSelectionParser';
import {isUnmatchedValueQuery} from '../isUnmatchedValueQuery';
import {parseAssetSelectionQuery} from '../parseAssetSelectionQuery';

export interface AssetSelectionInputProps {
assets: AssetGraphQueryItem[];
Expand All @@ -30,7 +33,7 @@ const defaultLinter = createSelectionLinter({

export const AssetSelectionInput = ({
value,
onChange,
onChange: _onChange,
assets,
linter = defaultLinter,
useAssetSelectionAutoComplete = defaultUseAssetSelectionAutoCompleteProvider,
Expand All @@ -39,6 +42,17 @@ export const AssetSelectionInput = ({
}: AssetSelectionInputProps) => {
const {useAutoComplete} = useAssetSelectionAutoComplete(assets);

const onChange = useCallback(
(value: string) => {
if (parseAssetSelectionQuery([], value) instanceof Error && isUnmatchedValueQuery(value)) {
_onChange(`key:"*${value}*"`);
} else {
_onChange(value);
}
},
[_onChange],
);

return (
<SelectionAutoCompleteInput
id="asset-selection-input"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {CharStreams, CommonTokenStream} from 'antlr4ts';
import {AbstractParseTreeVisitor} from 'antlr4ts/tree/AbstractParseTreeVisitor';

import {AntlrInputErrorListener} from './parseAssetSelectionQuery';
import {SelectionAutoCompleteLexer} from '../selection/generated/SelectionAutoCompleteLexer';
import {
SelectionAutoCompleteParser,
UnmatchedValueContext,
} from '../selection/generated/SelectionAutoCompleteParser';
import {SelectionAutoCompleteVisitor} from '../selection/generated/SelectionAutoCompleteVisitor';

class UnmatchedValueQueryVisitor
extends AbstractParseTreeVisitor<void>
implements SelectionAutoCompleteVisitor<void>
{
public isUnmatchedValue: boolean;

defaultResult() {}

constructor(private query: string) {
super();
this.isUnmatchedValue = false;
}

visitUnmatchedValue(ctx: UnmatchedValueContext) {
if (ctx.start.startIndex === 0 && ctx.stop?.stopIndex === this.query.length - 1) {
// If a single attribute expression is the entire query, it's not a complex
this.isUnmatchedValue = true;
}
}
}

export const isUnmatchedValueQuery = (query: string) => {
try {
const lexer = new SelectionAutoCompleteLexer(CharStreams.fromString(query));
lexer.removeErrorListeners();
lexer.addErrorListener(new AntlrInputErrorListener());

const tokenStream = new CommonTokenStream(lexer);

const parser = new SelectionAutoCompleteParser(tokenStream);
parser.removeErrorListeners();
parser.addErrorListener(new AntlrInputErrorListener());

const tree = parser.start();

const visitor = new UnmatchedValueQueryVisitor(query);
visitor.visit(tree);
return visitor.isUnmatchedValue;
} catch {
return false;
}
};