From 80ddf07a9f029bfc8f86953d4ccd2222c2080c69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Wed, 14 Sep 2022 12:08:13 +0200 Subject: [PATCH] Add `node-param-default-missing` exception for `modes` (#129) --- README.md | 2 +- docs/rules/node-param-default-missing.md | 71 +++++++++++++++++++++++- lib/rules/node-param-default-missing.ts | 14 ++++- tests/node-param-default-missing.test.ts | 71 ++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4acebf6..ce9f020 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ In the `community` ruleset, the five `*-still-default` rules allow you to define | [node-param-collection-type-item-required](docs/rules/node-param-collection-type-item-required.md) | Items in collection-type node parameter must not have a `required` property. | Yes | | [node-param-collection-type-unsorted-items](docs/rules/node-param-collection-type-unsorted-items.md) | Items in collection-type node parameter must be alphabetized by `name` if five or more than five. | Yes | | [node-param-color-type-unused](docs/rules/node-param-color-type-unused.md) | `string`-type color-related node parameter must be `color`-type. | Yes | -| [node-param-default-missing](docs/rules/node-param-default-missing.md) | `default` must be present in a node parameter. | Yes | +| [node-param-default-missing](docs/rules/node-param-default-missing.md) | `default` must be present in a node parameter, except in node parameters under `modes`. | Yes | | [node-param-default-wrong-for-boolean](docs/rules/node-param-default-wrong-for-boolean.md) | `default` for boolean-type node parameter must be a boolean. | Yes | | [node-param-default-wrong-for-collection](docs/rules/node-param-default-wrong-for-collection.md) | `default` for collection-type node parameter must be an object. | Yes | | [node-param-default-wrong-for-fixed-collection](docs/rules/node-param-default-wrong-for-fixed-collection.md) | `default` for fixed-collection-type node parameter must be an object. | Yes | diff --git a/docs/rules/node-param-default-missing.md b/docs/rules/node-param-default-missing.md index 659162e..a3ce851 100644 --- a/docs/rules/node-param-default-missing.md +++ b/docs/rules/node-param-default-missing.md @@ -2,7 +2,7 @@ # node-param-default-missing -`default` must be present in a node parameter. +`default` must be present in a node parameter, except in node parameters under `modes`. 📋 This rule is part of the `plugin:n8n-nodes-base/nodes` config. @@ -76,6 +76,75 @@ const test = { type: "number", default: -1, }; + +const test = { + displayName: "Board", + name: "boardIdRLC", + type: "resourceLocator", + default: { mode: "list", value: "" }, + required: true, + displayOptions: { + show: { + operation: ["get", "delete", "update"], + resource: ["board"], + "@version": [2], + }, + }, + description: "The ID of the board", + modes: [ + { + displayName: "From List", + name: "list", + type: "list", + hint: "Select a board from the list", + placeholder: "Choose...", + initType: "board", + typeOptions: { + searchListMethod: "searchBoards", + searchFilterRequired: true, + searchable: true, + }, + }, + { + displayName: "ID", + name: "id", + type: "string", + hint: "Enter Board Id", + validation: [ + { + type: "regex", + properties: { + regex: "[a-zA-Z0-9]+", + errorMessage: "ID value cannot be empty", + }, + }, + ], + placeholder: "KdEAAdde", + url: "=https://trello.com/b/{{$value}}", + }, + { + displayName: "By URL", + name: "url", + type: "string", + hint: "Enter board URL", + placeholder: "https://trello.com/b/e123456/board-name", + validation: [ + { + type: "regex", + properties: { + regex: "http(s)?://trello.com/b/([a-zA-Z0-9]+)/[a-zA-Z0-9]+", + errorMessage: + "URL has to be in the format: http(s)://trello.com/b//", + }, + }, + ], + extractValue: { + type: "regex", + regex: "https://trello.com/b/([a-zA-Z0-9]+)", + }, + }, + ], +}; ``` ## Links diff --git a/lib/rules/node-param-default-missing.ts b/lib/rules/node-param-default-missing.ts index 54e4da4..397aec4 100644 --- a/lib/rules/node-param-default-missing.ts +++ b/lib/rules/node-param-default-missing.ts @@ -1,4 +1,4 @@ -import { TSESTree } from "@typescript-eslint/utils"; +import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils"; import { utils } from "../ast/utils"; import { id } from "../ast/identifiers"; import { getters } from "../ast/getters"; @@ -8,7 +8,7 @@ export default utils.createRule({ meta: { type: "problem", docs: { - description: "`default` must be present in a node parameter.", + description: "`default` must be present in a node parameter, except in node parameters under `modes`.", recommended: "error", }, fixable: "code", @@ -23,6 +23,16 @@ export default utils.createRule({ ObjectExpression(node) { if (!id.isNodeParameter(node, { skipKeys: ["default"] })) return; + if (node.parent?.parent) { + if ( + node.parent.parent.type === AST_NODE_TYPES.Property && + node.parent.parent.key.type === AST_NODE_TYPES.Identifier && + node.parent.parent.key.name === "modes" + ) { + return; + } + } + const type = getters.nodeParam.getType(node); // insertion point if (!type) return; diff --git a/tests/node-param-default-missing.test.ts b/tests/node-param-default-missing.test.ts index 81a146e..941d4f9 100644 --- a/tests/node-param-default-missing.test.ts +++ b/tests/node-param-default-missing.test.ts @@ -51,6 +51,77 @@ ruleTester().run(getRuleName(module), rule, { default: -1, };`, }, + { + code: outdent` + const test = { + displayName: 'Board', + name: 'boardIdRLC', + type: 'resourceLocator', + default: { mode: 'list', value: '' }, + required: true, + displayOptions: { + show: { + operation: ['get', 'delete', 'update'], + resource: ['board'], + '@version': [2], + }, + }, + description: 'The ID of the board', + modes: [ + { + displayName: 'From List', + name: 'list', + type: 'list', + hint: 'Select a board from the list', + placeholder: 'Choose...', + initType: 'board', + typeOptions: { + searchListMethod: 'searchBoards', + searchFilterRequired: true, + searchable: true, + }, + }, + { + displayName: 'ID', + name: 'id', + type: 'string', + hint: 'Enter Board Id', + validation: [ + { + type: 'regex', + properties: { + regex: '[a-zA-Z0-9]+', + errorMessage: 'ID value cannot be empty', + }, + }, + ], + placeholder: 'KdEAAdde', + url: '=https://trello.com/b/{{$value}}', + }, + { + displayName: 'By URL', + name: 'url', + type: 'string', + hint: 'Enter board URL', + placeholder: 'https://trello.com/b/e123456/board-name', + validation: [ + { + type: 'regex', + properties: { + regex: 'http(s)?://trello.com/b/([a-zA-Z0-9]+)/[a-zA-Z0-9]+', + errorMessage: + 'URL has to be in the format: http(s)://trello.com/b//', + }, + }, + ], + extractValue: { + type: 'regex', + regex: 'https://trello.com/b/([a-zA-Z0-9]+)', + }, + }, + ], + };`, + } ], invalid: [ {