|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils'; |
| 9 | +import { RuleCreator } from '@typescript-eslint/utils/eslint-utils'; |
| 10 | + |
| 11 | +/** Check if an expression is a call to nls.localize() */ |
| 12 | +const isNlsLocalizeCall = (expr: TSESTree.Expression): boolean => |
| 13 | + expr.type === AST_NODE_TYPES.CallExpression && |
| 14 | + expr.callee.type === AST_NODE_TYPES.MemberExpression && |
| 15 | + expr.callee.object.type === AST_NODE_TYPES.Identifier && |
| 16 | + expr.callee.object.name === 'nls' && |
| 17 | + expr.callee.property.type === AST_NODE_TYPES.Identifier && |
| 18 | + expr.callee.property.name === 'localize'; |
| 19 | + |
| 20 | +/** Collect all string literal nodes in an expression that would be returned as error message */ |
| 21 | +const collectStringLiteralNodes = ( |
| 22 | + expr: TSESTree.Expression | null | undefined, |
| 23 | + out: TSESTree.Node[] |
| 24 | +): void => { |
| 25 | + if (!expr) return; |
| 26 | + if (isNlsLocalizeCall(expr)) return; |
| 27 | + if (expr.type === AST_NODE_TYPES.Literal && typeof expr.value === 'string') { |
| 28 | + out.push(expr); |
| 29 | + return; |
| 30 | + } |
| 31 | + if (expr.type === AST_NODE_TYPES.TemplateLiteral && !expr.expressions.some(isNlsLocalizeCall)) { |
| 32 | + out.push(expr); |
| 33 | + return; |
| 34 | + } |
| 35 | + if (expr.type === AST_NODE_TYPES.ConditionalExpression) { |
| 36 | + collectStringLiteralNodes(expr.consequent, out); |
| 37 | + collectStringLiteralNodes(expr.alternate, out); |
| 38 | + return; |
| 39 | + } |
| 40 | + if (expr.type === AST_NODE_TYPES.LogicalExpression) { |
| 41 | + collectStringLiteralNodes(expr.left, out); |
| 42 | + collectStringLiteralNodes(expr.right, out); |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +/** Check if this is vscode.window.showInputBox or window.showInputBox */ |
| 47 | +const isShowInputBoxCall = (node: TSESTree.CallExpression): boolean => { |
| 48 | + if (node.callee.type !== AST_NODE_TYPES.MemberExpression) return false; |
| 49 | + const callee = node.callee; |
| 50 | + if (callee.property.type !== AST_NODE_TYPES.Identifier || callee.property.name !== 'showInputBox') { |
| 51 | + return false; |
| 52 | + } |
| 53 | + if (callee.object.type === AST_NODE_TYPES.MemberExpression) { |
| 54 | + return ( |
| 55 | + callee.object.object.type === AST_NODE_TYPES.Identifier && |
| 56 | + callee.object.object.name === 'vscode' && |
| 57 | + callee.object.property.type === AST_NODE_TYPES.Identifier && |
| 58 | + callee.object.property.name === 'window' |
| 59 | + ); |
| 60 | + } |
| 61 | + if (callee.object.type === AST_NODE_TYPES.Identifier) { |
| 62 | + return callee.object.name === 'window'; |
| 63 | + } |
| 64 | + return false; |
| 65 | +}; |
| 66 | + |
| 67 | +/** Find the validateInput property in showInputBox options */ |
| 68 | +const findValidateInputProperty = (obj: TSESTree.ObjectExpression): TSESTree.Property | undefined => { |
| 69 | + const p = obj.properties.find( |
| 70 | + prop => |
| 71 | + prop.type === AST_NODE_TYPES.Property && |
| 72 | + prop.key.type === AST_NODE_TYPES.Identifier && |
| 73 | + prop.key.name === 'validateInput' |
| 74 | + ); |
| 75 | + return p?.type === AST_NODE_TYPES.Property ? p : undefined; |
| 76 | +}; |
| 77 | + |
| 78 | +/** Recursively collect all ReturnStatement nodes from a block or statement */ |
| 79 | +const collectReturnStatements = ( |
| 80 | + node: TSESTree.Node, |
| 81 | + out: TSESTree.ReturnStatement[] |
| 82 | +): void => { |
| 83 | + if (node.type === AST_NODE_TYPES.ReturnStatement) { |
| 84 | + out.push(node); |
| 85 | + return; |
| 86 | + } |
| 87 | + if (node.type === AST_NODE_TYPES.BlockStatement) { |
| 88 | + for (const stmt of node.body) { |
| 89 | + collectReturnStatements(stmt, out); |
| 90 | + } |
| 91 | + return; |
| 92 | + } |
| 93 | + if (node.type === AST_NODE_TYPES.IfStatement) { |
| 94 | + collectReturnStatements(node.consequent, out); |
| 95 | + if (node.alternate) collectReturnStatements(node.alternate, out); |
| 96 | + return; |
| 97 | + } |
| 98 | + if (node.type === AST_NODE_TYPES.SwitchCase) { |
| 99 | + for (const stmt of node.consequent) { |
| 100 | + collectReturnStatements(stmt, out); |
| 101 | + } |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +/** Traverse function body for ReturnStatement nodes and collect string literals */ |
| 106 | +const checkValidateInputFunction = ( |
| 107 | + fn: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression, |
| 108 | + context: { report: (opts: { node: TSESTree.Node; messageId: 'noLiteral' }) => void } |
| 109 | +): void => { |
| 110 | + const body = fn.body; |
| 111 | + const returnStmts: TSESTree.ReturnStatement[] = []; |
| 112 | + if (body.type === AST_NODE_TYPES.BlockStatement) { |
| 113 | + for (const stmt of body.body) { |
| 114 | + collectReturnStatements(stmt, returnStmts); |
| 115 | + } |
| 116 | + } else { |
| 117 | + returnStmts.push({ type: AST_NODE_TYPES.ReturnStatement, argument: body } as TSESTree.ReturnStatement); |
| 118 | + } |
| 119 | + |
| 120 | + for (const stmt of returnStmts) { |
| 121 | + if (!stmt.argument) continue; |
| 122 | + const literals: TSESTree.Node[] = []; |
| 123 | + collectStringLiteralNodes(stmt.argument as TSESTree.Expression, literals); |
| 124 | + for (const node of literals) { |
| 125 | + context.report({ node, messageId: 'noLiteral' }); |
| 126 | + } |
| 127 | + } |
| 128 | +}; |
| 129 | + |
| 130 | +export const noVscodeValidateinputLiterals = RuleCreator.withoutDocs({ |
| 131 | + meta: { |
| 132 | + type: 'problem', |
| 133 | + docs: { |
| 134 | + description: |
| 135 | + 'Disallow string literals in showInputBox validateInput - use nls.localize() for error messages' |
| 136 | + }, |
| 137 | + schema: [], |
| 138 | + messages: { |
| 139 | + noLiteral: |
| 140 | + "showInputBox validateInput must use nls.localize('message_key') for error messages, not string literals. Add the message to i18n.ts and use nls.localize() to reference it." |
| 141 | + } |
| 142 | + }, |
| 143 | + defaultOptions: [], |
| 144 | + create: context => ({ |
| 145 | + CallExpression: (node: TSESTree.CallExpression): void => { |
| 146 | + if (!isShowInputBoxCall(node)) return; |
| 147 | + |
| 148 | + const optionsArg = node.arguments[0]; |
| 149 | + if (optionsArg?.type !== AST_NODE_TYPES.ObjectExpression) return; |
| 150 | + |
| 151 | + const validateInputProp = findValidateInputProperty(optionsArg); |
| 152 | + if (!validateInputProp?.value) return; |
| 153 | + |
| 154 | + const value = validateInputProp.value; |
| 155 | + if (value.type === AST_NODE_TYPES.Identifier) return; // External function - skip |
| 156 | + |
| 157 | + if ( |
| 158 | + value.type === AST_NODE_TYPES.ArrowFunctionExpression || |
| 159 | + value.type === AST_NODE_TYPES.FunctionExpression |
| 160 | + ) { |
| 161 | + checkValidateInputFunction(value, context); |
| 162 | + } |
| 163 | + } |
| 164 | + }) |
| 165 | +}); |
0 commit comments