2022-09-26 01:08:59 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file contains any locally defined ESLint rules. They are picked up by
|
|
|
|
* eslint-plugin-n8n-local-rules and exposed as 'n8n-local-rules/<rule-name>'.
|
|
|
|
*/
|
|
|
|
module.exports = {
|
|
|
|
/**
|
|
|
|
* A rule to detect calls to JSON.parse() that are not wrapped inside try/catch blocks.
|
|
|
|
*
|
|
|
|
* Valid:
|
|
|
|
* ```js
|
|
|
|
* try { JSON.parse(foo) } catch(err) { baz() }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Invalid:
|
|
|
|
* ```js
|
|
|
|
* JSON.parse(foo)
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* The pattern where an object is cloned with JSON.parse(JSON.stringify()) is allowed
|
|
|
|
* (abundant in the n8n codebase):
|
|
|
|
*
|
|
|
|
* Valid:
|
|
|
|
* ```js
|
|
|
|
* JSON.parse(JSON.stringify(foo))
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
'no-uncaught-json-parse': {
|
|
|
|
meta: {
|
|
|
|
type: 'problem',
|
|
|
|
docs: {
|
2022-10-24 03:48:16 -07:00
|
|
|
description:
|
|
|
|
'Calls to `JSON.parse()` must be replaced with `jsonParse()` from `n8n-workflow` or surrounded with a try/catch block.',
|
2022-09-26 01:08:59 -07:00
|
|
|
recommended: 'error',
|
|
|
|
},
|
|
|
|
schema: [],
|
|
|
|
messages: {
|
2022-10-24 03:48:16 -07:00
|
|
|
noUncaughtJsonParse:
|
|
|
|
'Use `jsonParse()` from `n8n-workflow` or surround the `JSON.parse()` call with a try/catch block.',
|
2022-09-26 01:08:59 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
defaultOptions: [],
|
|
|
|
create(context) {
|
|
|
|
return {
|
|
|
|
CallExpression(node) {
|
|
|
|
if (!isJsonParseCall(node)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-19 00:36:25 -07:00
|
|
|
if (isJsonStringifyCall(node)) {
|
2022-09-26 01:08:59 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context.getAncestors().find((node) => node.type === 'TryStatement') !== undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found a JSON.parse() call not wrapped into a try/catch, so report it
|
|
|
|
context.report({
|
|
|
|
messageId: 'noUncaughtJsonParse',
|
|
|
|
node,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
2022-10-19 00:36:25 -07:00
|
|
|
|
|
|
|
'no-json-parse-json-stringify': {
|
|
|
|
meta: {
|
|
|
|
type: 'problem',
|
|
|
|
docs: {
|
|
|
|
description:
|
|
|
|
'Calls to `JSON.parse(JSON.stringify(arg))` must be replaced with `deepCopy(arg)` from `n8n-workflow`.',
|
|
|
|
recommended: 'error',
|
|
|
|
},
|
|
|
|
messages: {
|
|
|
|
noJsonParseJsonStringify: 'Replace with `deepCopy({{ argText }})`',
|
|
|
|
},
|
|
|
|
fixable: 'code',
|
|
|
|
},
|
|
|
|
create(context) {
|
|
|
|
return {
|
|
|
|
CallExpression(node) {
|
|
|
|
if (isJsonParseCall(node) && isJsonStringifyCall(node)) {
|
|
|
|
const [callExpression] = node.arguments;
|
|
|
|
|
|
|
|
const { arguments: args } = callExpression;
|
|
|
|
|
|
|
|
if (!Array.isArray(args) || args.length !== 1) return;
|
|
|
|
|
|
|
|
const [arg] = args;
|
|
|
|
|
|
|
|
if (!arg) return;
|
|
|
|
|
|
|
|
const argText = context.getSourceCode().getText(arg);
|
|
|
|
|
|
|
|
context.report({
|
|
|
|
messageId: 'noJsonParseJsonStringify',
|
|
|
|
node,
|
|
|
|
data: { argText },
|
|
|
|
fix: (fixer) => fixer.replaceText(node, `deepCopy(${argText})`),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
2022-12-29 03:20:43 -08:00
|
|
|
|
|
|
|
'no-unneeded-backticks': {
|
|
|
|
meta: {
|
|
|
|
type: 'problem',
|
|
|
|
docs: {
|
|
|
|
description:
|
|
|
|
'Template literal backticks may only be used for string interpolation or multiline strings.',
|
|
|
|
recommended: 'error',
|
|
|
|
},
|
|
|
|
messages: {
|
|
|
|
noUneededBackticks: 'Use single or double quotes, not backticks',
|
|
|
|
},
|
|
|
|
fixable: 'code',
|
|
|
|
},
|
|
|
|
create(context) {
|
|
|
|
return {
|
|
|
|
TemplateLiteral(node) {
|
|
|
|
if (node.expressions.length > 0) return;
|
|
|
|
if (node.quasis.every((q) => q.loc.start.line !== q.loc.end.line)) return;
|
|
|
|
|
|
|
|
node.quasis.forEach((q) => {
|
|
|
|
const escaped = q.value.raw.replace(/(?<!\\)'/g, "\\'");
|
|
|
|
|
|
|
|
context.report({
|
|
|
|
messageId: 'noUneededBackticks',
|
|
|
|
node,
|
|
|
|
fix: (fixer) => fixer.replaceText(q, `'${escaped}'`),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
2022-09-26 01:08:59 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const isJsonParseCall = (node) =>
|
|
|
|
node.callee.type === 'MemberExpression' &&
|
|
|
|
node.callee.object.type === 'Identifier' &&
|
|
|
|
node.callee.object.name === 'JSON' &&
|
|
|
|
node.callee.property.type === 'Identifier' &&
|
|
|
|
node.callee.property.name === 'parse';
|
|
|
|
|
2022-10-19 00:36:25 -07:00
|
|
|
const isJsonStringifyCall = (node) => {
|
2022-09-26 01:08:59 -07:00
|
|
|
const parseArg = node.arguments?.[0];
|
|
|
|
return (
|
|
|
|
parseArg !== undefined &&
|
|
|
|
parseArg.type === 'CallExpression' &&
|
|
|
|
parseArg.callee.type === 'MemberExpression' &&
|
|
|
|
parseArg.callee.object.type === 'Identifier' &&
|
|
|
|
parseArg.callee.object.name === 'JSON' &&
|
|
|
|
parseArg.callee.property.type === 'Identifier' &&
|
|
|
|
parseArg.callee.property.name === 'stringify'
|
|
|
|
);
|
|
|
|
};
|