Skip to content
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
43 changes: 35 additions & 8 deletions packages/workflow/src/node-reference-parser-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,51 @@ const ACCESS_PATTERNS: AccessPattern[] = [
},
];

function prepareOldNodeName(nodeName: string) {
// if node name contains literal \ -> replace with \\
// since that's how it'll be written in a JS expression
const doubleSlashes = nodeName.replaceAll('\\', '\\\\');
// escape special characters for regex
const escaped = backslashEscape(doubleSlashes);
// quotes may or may not be escaped in the JS expression
// so we replace literal quotes with regexes handle that
return escaped.replace(/"/g, '(?:\\\\?")').replace(/'/g, "(?:\\\\?')");
}

function prepareNewNodeName(nodeName: string) {
// escape $ for replacement regex
const dollarEscaped = dollarEscape(nodeName);
// escape literal \ ' " characters
return dollarEscaped.replaceAll('\\', '\\\\').replaceAll('"', '\\"').replaceAll("'", "\\'");
}

export function applyAccessPatterns(expression: string, previousName: string, newName: string) {
// To not run the "expensive" regex stuff when it is not needed
// make a simple check first if it really contains the node-name
if (!expression.includes(previousName)) return expression;
// make a simple check first if it contains any of the access patterns
let noMatch = true;
for (const pattern of ACCESS_PATTERNS) {
if (expression.includes(pattern.checkPattern)) {
noMatch = false;
break;
}
}

if (noMatch) {
return expression;
}

// Really contains node-name (even though we do not know yet if really as $node-expression)
const escapedOldName = backslashEscape(previousName); // for match
const escapedNewName = dollarEscape(newName); // for replacement
const preparedOldName = prepareOldNodeName(previousName);
const preparedNewName = prepareNewNodeName(newName);

for (const pattern of ACCESS_PATTERNS) {
if (expression.includes(pattern.checkPattern)) {
expression = expression.replace(
new RegExp(pattern.replacePattern(escapedOldName), 'g'),
`$1${escapedNewName}$2`,
new RegExp(pattern.replacePattern(preparedOldName), 'g'),
`$1${preparedNewName}$2`,
);

if (pattern.customCallback) {
expression = pattern.customCallback(expression, newName, escapedNewName);
expression = pattern.customCallback(expression, newName, preparedNewName);
}
}
}
Expand Down
46 changes: 43 additions & 3 deletions packages/workflow/test/node-reference-parser-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,50 @@ describe('NodeReferenceParserUtils', () => {
expected: 'someRandomExpression("oldName")',
},
{
expression: '$("old\\"Name")',
previousName: 'old\\"Name',
expression: '$("someone\'s node")',
previousName: "someone's node",
newName: 'other node',
expected: '$("other node")',
},
{
expression: '$(\'some "node"\')',
previousName: 'some "node"',
newName: 'other node',
expected: "$('other node')",
},
{
expression: '$("test\\\\some")',
previousName: 'test\\some',
newName: 'other node',
expected: '$("other node")',
},
{
expression: '$("old name")',
previousName: 'old name',
newName: 'new "name"',
expected: '$("new \\"name\\"")',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused why:

new "name" ends up as '$("new \\"name\\"")' (double back slash)

When:

"new 'name'" returns '$("new \\\'name\\\'")' (triple back slash)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case with single quotes the third slash is needed to escape the quotes around name, since the expected string is itself is wrapped in single quotes

},
{
expression: '$("old name")',
previousName: 'old name',
newName: "new 'name'",
expected: '$("new \\\'name\\\'")',
},
{
expression: '$("test some")',
previousName: 'test some',
newName: 'other\\node',
expected: '$("other\\\\node")',
},
{
// $("old\"Na\\me\'") -> old"Na\me'
expression: '$("old\\"Na\\\\me\\\'")',
// old"Na\me'
previousName: 'old"Na\\me\'',
// n\'ew\"Name
newName: 'n\\\'ew\\"Name',
expected: '$("n\\\'ew\\"Name")',
// $("n\\\'ew\\\"Name") -> n\'ew\"Name
expected: '$("n\\\\\\\'ew\\\\\\"Name")',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one doesn't quite seem right, ew\\"Name should evaluate to ew\\\\\"Name, no? 5 \ not 6

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6 \ in ew\\\\\\"Name would evaluate to a string ew\\\"Name, so 3 \ in the result. Then, since it'll be evaluated again, \\\" will become literal \" characters (\\ + \") -> ew\"Name

In a case with 5 \ we'd have \\\\\" -> \\" (\\ + \\ + \"). But keep in mind that \\" is inside of a string in $("..."), so the " should be escaped by a 3rd \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a mess 😢

},
])(
'should correctly transform expression "$expression" with previousName "$previousName" and newName "$newName"',
Expand Down
Loading