Skip to content

Commit

Permalink
fix(editor): Capture indexed access expressions when building complet…
Browse files Browse the repository at this point in the history
…ions (#8331)
  • Loading branch information
ivov authored Jan 17, 2024
1 parent 7cdbb42 commit 159b328
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ describe('Resolution-based completions', () => {
});
});

describe('indexed access completions', () => {
test('should return string completions for indexed access that resolves to string literal: {{ "abc"[0].| }}', () => {
// @ts-expect-error Spied function is mistyped
resolveParameterSpy.mockReturnValueOnce('a');

expect(completions('{{ "abc"[0].| }}')).toHaveLength(
natives('string').length + extensions('string').length,
);
});
});

describe('complex expression completions', () => {
const resolveParameterSpy = vi.spyOn(workflowHelpers, 'resolveParameter');
const { $input } = mockProxy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ const regexes = {
doubleQuoteStringLiteral: /(".+")\.([^"{\s])*/, // "abc".
dateLiteral: /\(?new Date\(\(?.*?\)\)?\.([^{\s])*/, // new Date(). or (new Date()).
arrayLiteral: /(\[.+\])\.([^{\s])*/, // [1, 2, 3].
indexedAccess: /([^{\s]+\[.+\])\.([^{\s])*/, // 'abc'[0]. or 'abc'.split('')[0] or similar ones
objectLiteral: /\(\{.*\}\)\.([^{\s])*/, // ({}).

mathGlobal: /Math\.([^{\s])*/, // Math.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ export function longestCommonPrefix(...strings: string[]) {
// suggestions can be matched based on it.
function extractSubExpression(userInput: string): string {
const dollarSignIndex = userInput.indexOf('$');
// If it's not a dollar sign expression just strip parentheses
if (dollarSignIndex === -1) {
userInput = userInput.replace(/^.+(\(|\[|{)/, '');
return userInput;
} else if (!stringLiteralRegex.test(userInput)) {
// If there is a dollar sign in the input and input is not a string literal,
// extract part of following the last $
Expand Down

0 comments on commit 159b328

Please sign in to comment.