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

fix: Expression extension failing with optional chaining #5370

Merged
merged 4 commits into from
Feb 9, 2023

Conversation

valya
Copy link
Contributor

@valya valya commented Feb 6, 2023

No description provided.

@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team labels Feb 6, 2023
@valya valya force-pushed the pay-105-failed-to-extend-syntax-error-on branch from dcb7fd9 to cd553d4 Compare February 6, 2023 10:35
Comment on lines +86 to +105
function parseWithEsprimaNext(source: string, options?: any): any {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const ast = esprimaParse(source, {
loc: true,
locations: true,
comment: true,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
range: getOption(options, 'range', false),
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
tolerant: getOption(options, 'tolerant', true),
tokens: true,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
jsx: getOption(options, 'jsx', false),
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
sourceType: getOption(options, 'sourceType', 'module'),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);

return ast;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor details: We could type options as esprima.Config and assert the return types of getOption (instead of ESLint exceptions) and return the result directly without assigning.


let currentChain = 1;

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
Copy link
Contributor

Choose a reason for hiding this comment

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

Not needed apparently.

Comment on lines +137 to +139
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
typeof window !== 'object' ? 'global' : 'window',
Copy link
Contributor

Choose a reason for hiding this comment

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

If we define Window under global the exceptions should not be needed.

Comment on lines +155 to +165
const buildCancelCheckWrapper = (node: ExpressionKind): ExpressionKind => {
return types.builders.conditionalExpression(
types.builders.binaryExpression(
'===',
cancelMemberExpression,
types.builders.booleanLiteral(true),
),
undefinedIdentifier,
node,
);
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we define functions like outside of visitChainExpression()? To avoid defining them on every run and to slim down this visitor method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They're defined in there to avoid having to pass in a bunch of arguments or create a bunch of expressions multiple times. This is less of an issue with the current implementation and was more of an issue earlier on. I still prefer this way as I think it makes the rest of the code a lot cleaner.

Speaking of defining them on every run. I think we should add a cache (like tmpl does) for expressions because we're performing these transforms every time an extended expression is run. My main concern is the size of the cache, I'm not sure if it'd be a problem on the backend but it might cause issues over long period of time on the frontend. Although we're likely talking in the 0-10MB range over a long session of editing expressions.

// Just disabling all eslint rules for now.
// eslint-disable-next-line
return inputAny[functionName](...args);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can make the exceptions to @typescript-eslint/no-unsafe-return and @typescript-eslint/no-explicit-any (and any others that happen frequently here) file-level for better readability? Files doing AST manipulation are rare so this should be okay.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm personally not a fan of it just because I like to be reminded when I'm using any but if you think it'd help with readability then I'm OK doing it at a file level.

@valya valya merged commit c7b58e0 into master Feb 9, 2023
@valya valya deleted the pay-105-failed-to-extend-syntax-error-on branch February 9, 2023 13:57
@n8n-assistant n8n-assistant bot added the Upcoming Release Will be part of the upcoming release label Feb 9, 2023
janober pushed a commit that referenced this pull request Feb 9, 2023
* wip

* fix: working optional chaining polyfill

* fix: polyfill optional chaining on extended functions

* test: add optional chaining tests
@janober
Copy link
Member

janober commented Feb 11, 2023

Got released with n8n@0.215.1

@janober janober removed the Upcoming Release Will be part of the upcoming release label Feb 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants