* 🔥 Remove test extensions * 🚧 Add test description * 📘 Expand types * ⚡ Export extensions * ⚡ Export collection * ⚡ Mark all proxies * ✏️ Rename for clarity * ⚡ Export from barrel * ✨ Create datatype completions * ⚡ Mount datatype completions * 🧪 Adjust tests * ⚡ Add `path` prop * 🔥 Remove `()` from completion labels * ⚡ Filter out completions for pseudo-proxies * 🐛 Fix method error * ⚡ Add metrics * ✏️ Improve naming * ✨ Start completion on empty resolvable * ✨ Implement completion previews * ⚡ Break out completion manager * ⚡ Implement in expression editor modal * ✏️ Improve naming * ⚡ Filter out irrelevant completions * ✨ Add preview hint * ✏️ Improve comments * 🎨 Style preview hint * ⚡ Expand `hasNoParams` * ⚡ Add spacing for readability * ⚡ Add error codes * ✏️ Add comment * 🐛 Fix Esc behavior * ⚡ Parse Unicode * ⚡ Throw on invalid `DateTime` * ⚡ Fix second root completion detection * ⚡ Switch message at completable prefix position * 🐛 Fix function names for non-dev build * 🐛 Fix `json` handling * 🔥 Comment out previews * ♻️ Apply feedback * 🔥 Remove extensions * 🚚 Rename extensions * ⚡ Adjust some implementations * 🔥 Remove dummy extensions * 🐛 Fix object regex * ♻️ Apply feedback * ✏️ Fix typos * ✏️ Add `fn is not a function` message * 🔥 Remove check * ✨ Add `isNotEmpty` for objects * 🚚 Rename `global` to `alpha` * 🔥 Remove `encrypt` * ⚡ Restore `is not a function` error * ⚡ Support `week` on `extract()` * 🧪 Fix tests * ⚡ Add validation to some string extensions * ⚡ Validate number arrays in some extensions * 🧪 Fix tests * ✏️ Improve error message * ⏪ Revert extensions framework changes * 🧹 Previews cleanup * ⚡ Condense blank completions * ⚡ Refactor dollar completions * ⚡ Refactor non-dollar completions * ⚡ Refactor Luxon completions * ⚡ Refactor datatype completions * ⚡ Use `DATETIMEUNIT_MAP` * ✏️ Update test description * ⏪ Revert "Use `DATETIMEUNIT_MAP`" This reverts commit 472a77df5cd789905d162f3c3db02ac767b89b4e. * 🧪 Add tests * ♻️ Restore generic extensions * 🔥 Remove logs * 🧪 Expand tests * ✨ Add `Math` completions * ✏️ List breaking change * ⚡ Add doc tooltips * 🐛 Fix node selector regex * 🐛 Fix `context` resolution * 🐛 Allow dollar completions in args * ⚡ Make numeric array methods context-dependent * 📝 Adjust docs * 🐛 Fix selector ref * ⚡ Surface error for valid URL * 🐛 Disallow whitespace in `isEmail` check * 🧪 Fix test for `isUrl` * ⚡ Add comma validator in `toFloat` * ⚡ Add validation to `$jmespath()` * ⏪ Revert valid URL error * ⚡ Adjust `$jmespath()` validation * 🧪 Adjust `isUrl` test * ⚡ Remove `{}` and `[]` from compact * ✏️ Update docs * 🚚 Rename `stripTags` to `removeTags` * ⚡ Do not inject whitespace inside resolvable * ⚡ Make completions aware of `()` * ✏️ Add note * ⚡ Update sorting * ⚡ Hide active node name from node selector * 🔥 Remove `length()` and its aliases * ⚡ Validate non-zero for `chunk` * ✏️ Reword all error messages * 🐛 Fix `$now` and `$today` * ⚡ Simplify with `stripExcessParens` * ⚡ Fold luxon into datatype * 🧪 Clean up tests * 🔥 Remove tests for removed methods * 👕 Fix type * ⬆️ Upgrade lang pack * ⏪ Undo change to `vitest` command * 🔥 Remove unused method * ⚡ Separate `return` line * ✏️ Improve description * 🧪 Expand tests for initial-only completions * 🧪 Add bracket-aware completions * ⚡ Make check for `all()` stricter * ✏️ Adjust explanatory comments * 🔥 Remove unneded copy * 🔥 Remove outdated comment * ⚡ Make naming consistent * ✏️ Update comments * ⚡ Improve URL scheme check * ✏️ Add comment * 🚚 Move extension * ✏️ Update `BREAKING-CHANGES.md` * ✏️ Update upcoming version * ✏️ Fix grammar * ✏️ Shorten message * 🐛 Fix `Esc` behavior * 🐛 Fix `isNumeric` * ✨ Support native methods * 🧪 Skip Pinia tests * ✏️ Shorten description * 🔥 Remove outdated comment * 🧪 Unskip Pinia tests * ✏️ Add comments * 🧪 Expand tests to natives * ✏️ Add clarifying comments * ⚡ Use `setTimeout` to make telemetry non-blocking * 🐛 Account for no active node in cred modal * ✨ Resolve without workflow * 🔥 Remove `Esc` handling on NDV * ⚡ Use `isDateTime` * 🚚 Move `unique` to next phase This array extension takes optional args. * ⚡ Merge export * 🧪 Fix tests * ⏪ Restore check * ✏️ Make breaking change description more accurate * 🧪 Fix e2e tests
224 lines
6.1 KiB
TypeScript
224 lines
6.1 KiB
TypeScript
import mixins from 'vue-typed-mixins';
|
|
import { Expression, ExpressionExtensions } from 'n8n-workflow';
|
|
import { mapStores } from 'pinia';
|
|
import { ensureSyntaxTree } from '@codemirror/language';
|
|
|
|
import { workflowHelpers } from '@/mixins/workflowHelpers';
|
|
import { useNDVStore } from '@/stores/ndv';
|
|
import { EXPRESSION_EDITOR_PARSER_TIMEOUT } from '@/constants';
|
|
|
|
import type { PropType } from 'vue';
|
|
import type { EditorView } from '@codemirror/view';
|
|
import type { TargetItem } from '@/Interface';
|
|
import type { Html, Plaintext, RawSegment, Resolvable, Segment } from '@/types/expressions';
|
|
|
|
export const expressionManager = mixins(workflowHelpers).extend({
|
|
props: {
|
|
targetItem: {
|
|
type: Object as PropType<TargetItem | null>,
|
|
},
|
|
},
|
|
data() {
|
|
return {
|
|
editor: {} as EditorView,
|
|
};
|
|
},
|
|
watch: {
|
|
targetItem() {
|
|
setTimeout(() => {
|
|
this.$emit('change', {
|
|
value: this.unresolvedExpression,
|
|
segments: this.displayableSegments,
|
|
});
|
|
});
|
|
},
|
|
},
|
|
computed: {
|
|
...mapStores(useNDVStore),
|
|
|
|
unresolvedExpression(): string {
|
|
return this.segments.reduce((acc, segment) => {
|
|
acc += segment.kind === 'resolvable' ? segment.resolvable : segment.plaintext;
|
|
|
|
return acc;
|
|
}, '=');
|
|
},
|
|
|
|
hoveringItem(): TargetItem | undefined {
|
|
return this.ndvStore.hoveringItem ?? undefined;
|
|
},
|
|
|
|
resolvableSegments(): Resolvable[] {
|
|
return this.segments.filter((s): s is Resolvable => s.kind === 'resolvable');
|
|
},
|
|
|
|
plaintextSegments(): Plaintext[] {
|
|
return this.segments.filter((s): s is Plaintext => s.kind === 'plaintext');
|
|
},
|
|
|
|
expressionExtensionNames(): Set<string> {
|
|
return new Set(
|
|
ExpressionExtensions.reduce<string[]>((acc, cur) => {
|
|
return [...acc, ...Object.keys(cur.functions)];
|
|
}, []),
|
|
);
|
|
},
|
|
|
|
htmlSegments(): Html[] {
|
|
return this.segments.filter((s): s is Html => s.kind !== 'resolvable');
|
|
},
|
|
|
|
segments(): Segment[] {
|
|
const rawSegments: RawSegment[] = [];
|
|
|
|
const fullTree = ensureSyntaxTree(
|
|
this.editor.state,
|
|
this.editor.state.doc.length,
|
|
EXPRESSION_EDITOR_PARSER_TIMEOUT,
|
|
);
|
|
|
|
if (fullTree === null) {
|
|
throw new Error(`Failed to parse expression: ${this.editor.state.doc.toString()}`);
|
|
}
|
|
|
|
fullTree.cursor().iterate((node) => {
|
|
if (node.type.name === 'Program') return;
|
|
|
|
rawSegments.push({
|
|
from: node.from,
|
|
to: node.to,
|
|
text: this.editor.state.sliceDoc(node.from, node.to),
|
|
token: node.type.name,
|
|
});
|
|
});
|
|
|
|
return rawSegments.reduce<Segment[]>((acc, segment) => {
|
|
const { from, to, text, token } = segment;
|
|
|
|
if (token === 'Plaintext') {
|
|
acc.push({ kind: 'plaintext', from, to, plaintext: text });
|
|
|
|
return acc;
|
|
}
|
|
|
|
const { resolved, error, fullError } = this.resolve(text, this.hoveringItem);
|
|
|
|
acc.push({ kind: 'resolvable', from, to, resolvable: text, resolved, error, fullError });
|
|
|
|
return acc;
|
|
}, []);
|
|
},
|
|
|
|
/**
|
|
* Segments to display in the output of an expression editor.
|
|
*
|
|
* Some segments are not displayed when they are _part_ of the result,
|
|
* but displayed when they are the _entire_ result:
|
|
*
|
|
* - `This is a {{ [] }} test` displays as `This is a test`.
|
|
* - `{{ [] }}` displays as `[Array: []]`.
|
|
*
|
|
* Some segments display differently based on context:
|
|
*
|
|
* Date displays as
|
|
* - `Mon Nov 14 2022 17:26:13 GMT+0100 (CST)` when part of the result
|
|
* - `[Object: "2022-11-14T17:26:13.130Z"]` when the entire result
|
|
*
|
|
* Only needed in order to mimic behavior of `ParameterInputHint`.
|
|
*/
|
|
displayableSegments(): Segment[] {
|
|
return this.segments
|
|
.map((s) => {
|
|
if (this.segments.length <= 1 || s.kind !== 'resolvable') return s;
|
|
|
|
if (typeof s.resolved === 'string' && /\[Object: "\d{4}-\d{2}-\d{2}T/.test(s.resolved)) {
|
|
const utcDateString = s.resolved.replace(/(\[Object: "|\"\])/g, '');
|
|
s.resolved = new Date(utcDateString).toString();
|
|
}
|
|
|
|
if (typeof s.resolved === 'string' && /\[Array:\s\[.+\]\]/.test(s.resolved)) {
|
|
s.resolved = s.resolved.replace(/(\[Array: \[|\])/g, '');
|
|
}
|
|
|
|
return s;
|
|
})
|
|
.filter((s) => {
|
|
if (
|
|
this.segments.length > 1 &&
|
|
s.kind === 'resolvable' &&
|
|
typeof s.resolved === 'string' &&
|
|
(s.resolved === '[Array: []]' ||
|
|
s.resolved === this.$locale.baseText('expressionModalInput.empty'))
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
});
|
|
},
|
|
},
|
|
methods: {
|
|
isEmptyExpression(resolvable: string) {
|
|
return /\{\{\s*\}\}/.test(resolvable);
|
|
},
|
|
|
|
resolve(resolvable: string, targetItem?: TargetItem) {
|
|
const result: { resolved: unknown; error: boolean; fullError: Error | null } = {
|
|
resolved: undefined,
|
|
error: false,
|
|
fullError: null,
|
|
};
|
|
|
|
try {
|
|
if (!useNDVStore().activeNode) {
|
|
// e.g. credential modal
|
|
result.resolved = Expression.resolveWithoutWorkflow(resolvable);
|
|
} else {
|
|
result.resolved = this.resolveExpression('=' + resolvable, undefined, {
|
|
targetItem: targetItem ?? undefined,
|
|
inputNodeName: this.ndvStore.ndvInputNodeName,
|
|
inputRunIndex: this.ndvStore.ndvInputRunIndex,
|
|
inputBranchIndex: this.ndvStore.ndvInputBranchIndex,
|
|
});
|
|
}
|
|
} catch (error) {
|
|
result.resolved = `[${error.message}]`;
|
|
result.error = true;
|
|
result.fullError = error;
|
|
}
|
|
|
|
if (result.resolved === '') {
|
|
result.resolved = this.$locale.baseText('expressionModalInput.empty');
|
|
}
|
|
|
|
if (result.resolved === undefined && this.isEmptyExpression(resolvable)) {
|
|
result.resolved = this.$locale.baseText('expressionModalInput.empty');
|
|
}
|
|
|
|
if (result.resolved === undefined) {
|
|
result.resolved = this.isUncalledExpressionExtension(resolvable)
|
|
? this.$locale.baseText('expressionEditor.uncalledFunction')
|
|
: this.$locale.baseText('expressionModalInput.undefined');
|
|
|
|
result.error = true;
|
|
}
|
|
|
|
if (typeof result.resolved === 'number' && isNaN(result.resolved)) {
|
|
result.resolved = this.$locale.baseText('expressionModalInput.null');
|
|
}
|
|
|
|
return result;
|
|
},
|
|
|
|
isUncalledExpressionExtension(resolvable: string) {
|
|
const end = resolvable
|
|
.replace(/^{{|}}$/g, '')
|
|
.trim()
|
|
.split('.')
|
|
.pop();
|
|
|
|
return end !== undefined && this.expressionExtensionNames.has(end);
|
|
},
|
|
},
|
|
});
|