forked from ivov/eslint-plugin-n8n-nodes-base
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
node-param-operation-option-description-wrong-for-get-many
(i…
…vov#119) * Create `node-param-operation-option-description-wrong-for-get-many` * Restore version
- Loading branch information
Showing
9 changed files
with
279 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
docs/rules/node-param-operation-option-description-wrong-for-get-many.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
[//]: # "File generated from a template. Do not edit this file directly." | ||
|
||
# node-param-operation-option-description-wrong-for-get-many | ||
|
||
The property `description` in a Get Many option in an Operation node parameter must mention `many` instead of `all`. | ||
|
||
📋 This rule is part of the `plugin:n8n-nodes-base/nodes` config. | ||
|
||
🔧 Run ESLint with `--fix` option to autofix the issue flagged by this rule. | ||
|
||
## Examples | ||
|
||
❌ Example of **incorrect** code: | ||
|
||
```js | ||
const test = { | ||
displayName: "Operation", | ||
name: "operation", | ||
type: "options", | ||
noDataExpression: true, | ||
displayOptions: { | ||
show: { | ||
resource: ["entity"], | ||
}, | ||
}, | ||
default: "getAll", | ||
options: [ | ||
{ | ||
name: "Get Many", | ||
value: "getAll", | ||
description: "Retrieve all entities", | ||
action: "Get many entities", | ||
}, | ||
], | ||
}; | ||
``` | ||
|
||
✅ Example of **correct** code: | ||
|
||
```js | ||
const test = { | ||
displayName: "Operation", | ||
name: "operation", | ||
type: "options", | ||
noDataExpression: true, | ||
displayOptions: { | ||
show: { | ||
resource: ["entity"], | ||
}, | ||
}, | ||
default: "getAll", | ||
options: [ | ||
{ | ||
name: "Get Many", | ||
value: "getAll", | ||
description: "Retrieve many entities", | ||
action: "Get many entities", | ||
}, | ||
], | ||
}; | ||
``` | ||
|
||
## Links | ||
|
||
- [Rule source](../../lib/rules/node-param-operation-option-description-wrong-for-get-many.ts) | ||
- [Test source](../../tests/node-param-operation-option-description-wrong-for-get-many.test.ts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
lib/rules/node-param-operation-option-description-wrong-for-get-many.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { utils } from "../ast/utils"; | ||
import { id } from "../ast/identifiers"; | ||
import { getters } from "../ast/getters"; | ||
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils"; | ||
|
||
export default utils.createRule({ | ||
name: utils.getRuleName(module), | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: | ||
"The property `description` in a Get Many option in an Operation node parameter must mention `many` instead of `all`.", | ||
recommended: "error", | ||
}, | ||
fixable: "code", | ||
schema: [], | ||
messages: { | ||
changeToGetMany: "Change to '{{ newDescription }}' [autofixable]", | ||
}, | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
ObjectExpression(node) { | ||
if (!id.nodeParam.isOperation(node)) return; | ||
|
||
const options = getters.nodeParam.getOptions(node); | ||
|
||
if (!options) return; | ||
|
||
// skip `options: [...].sort()`, see EditImage.node.ts | ||
if (!Array.isArray(options.ast.value.elements)) return; | ||
|
||
const getAllOption = options.ast.value.elements.find( | ||
getters.nodeParam.getGetAllOption | ||
); | ||
|
||
if (!getAllOption) return; | ||
|
||
const descriptionNode = | ||
getAllOption.properties.find(isOptionDescription); | ||
|
||
if (!descriptionNode) return; | ||
|
||
const { value: description } = descriptionNode.value; | ||
|
||
if (description.includes(" all ")) { | ||
const [start, end] = description.split(" all "); | ||
|
||
const newDescription = [start, "many", end].join(" "); | ||
|
||
const fixed = utils.keyValue("description", newDescription); | ||
|
||
context.report({ | ||
messageId: "changeToGetMany", | ||
node: descriptionNode, | ||
fix: (fixer) => fixer.replaceText(descriptionNode, fixed), | ||
data: { newDescription }, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
function isOptionDescription( | ||
property: TSESTree.ObjectLiteralElement | ||
): property is TSESTree.Property & { value: { value: string } } { | ||
return ( | ||
property.type === AST_NODE_TYPES.Property && | ||
property.computed === false && | ||
property.key.type === AST_NODE_TYPES.Identifier && | ||
property.key.name === "description" && | ||
property.value.type === AST_NODE_TYPES.Literal && | ||
typeof property.value.value === "string" | ||
); | ||
} |
Oops, something went wrong.