eslint-plugin-lingui
Advanced tools
Comparing version 0.8.2 to 0.8.3
@@ -69,8 +69,40 @@ "use strict"; | ||
} | ||
function unwrapTSAsExpression(node) { | ||
while (node.type === utils_1.TSESTree.AST_NODE_TYPES.TSAsExpression) { | ||
node = node.expression; | ||
function isAcceptableExpression(node) { | ||
switch (node.type) { | ||
case utils_1.TSESTree.AST_NODE_TYPES.Literal: | ||
case utils_1.TSESTree.AST_NODE_TYPES.TemplateLiteral: | ||
case utils_1.TSESTree.AST_NODE_TYPES.LogicalExpression: | ||
case utils_1.TSESTree.AST_NODE_TYPES.BinaryExpression: | ||
case utils_1.TSESTree.AST_NODE_TYPES.ConditionalExpression: | ||
case utils_1.TSESTree.AST_NODE_TYPES.UnaryExpression: | ||
case utils_1.TSESTree.AST_NODE_TYPES.TSAsExpression: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
return node; | ||
} | ||
function isAssignedToIgnoredVariable(node, isIgnoredName) { | ||
let current = node; | ||
let parent = current.parent; | ||
while (parent && isAcceptableExpression(parent)) { | ||
current = parent; | ||
parent = parent.parent; | ||
} | ||
if (!parent) | ||
return false; | ||
if (parent.type === utils_1.TSESTree.AST_NODE_TYPES.VariableDeclarator && parent.init === current) { | ||
const variableDeclarator = parent; | ||
if ((0, helpers_1.isIdentifier)(variableDeclarator.id) && isIgnoredName(variableDeclarator.id.name)) { | ||
return true; | ||
} | ||
} | ||
else if (parent.type === utils_1.TSESTree.AST_NODE_TYPES.AssignmentExpression && | ||
parent.right === current) { | ||
const assignmentExpression = parent; | ||
if ((0, helpers_1.isIdentifier)(assignmentExpression.left) && isIgnoredName(assignmentExpression.left.name)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
exports.name = 'no-unlocalized-strings'; | ||
@@ -181,2 +213,3 @@ exports.rule = (0, create_rule_1.createRule)({ | ||
} | ||
/* istanbul ignore next */ | ||
default: | ||
@@ -186,2 +219,19 @@ return false; | ||
} | ||
/** | ||
* Helper function to determine if a node is inside an ignored property. | ||
*/ | ||
function isInsideIgnoredProperty(node) { | ||
let parent = node.parent; | ||
while (parent) { | ||
if (parent.type === utils_1.TSESTree.AST_NODE_TYPES.Property) { | ||
const key = parent.key; | ||
if (((0, helpers_1.isIdentifier)(key) && isIgnoredName(key.name)) || | ||
(((0, helpers_1.isLiteral)(key) || (0, helpers_1.isTemplateLiteral)(key)) && isIgnoredName((0, helpers_1.getText)(key)))) { | ||
return true; | ||
} | ||
} | ||
parent = parent.parent; | ||
} | ||
return false; | ||
} | ||
const ignoredJSXSymbols = ['←', ' ', '·']; | ||
@@ -211,2 +261,3 @@ const ignoredMethodsOnTypes = (option === null || option === void 0 ? void 0 : option.ignoreMethodsOnTypes) || []; | ||
default: | ||
/* istanbul ignore next */ | ||
return false; | ||
@@ -221,2 +272,17 @@ } | ||
}; | ||
function isLiteralInsideJSX(node) { | ||
let parent = node.parent; | ||
let insideJSXExpression = false; | ||
while (parent) { | ||
if (parent.type === utils_1.TSESTree.AST_NODE_TYPES.JSXExpressionContainer) { | ||
insideJSXExpression = true; | ||
} | ||
if (parent.type === utils_1.TSESTree.AST_NODE_TYPES.JSXElement && insideJSXExpression) { | ||
return true; | ||
} | ||
parent = parent.parent; | ||
} | ||
/* istanbul ignore next */ | ||
return false; | ||
} | ||
const processTextNode = (node) => { | ||
@@ -228,2 +294,3 @@ visited.add(node); | ||
} | ||
// First, handle the JSXText case directly | ||
if (node.type === utils_1.TSESTree.AST_NODE_TYPES.JSXText) { | ||
@@ -233,2 +300,12 @@ context.report({ node, messageId: 'forJsxText' }); | ||
} | ||
// If it's not JSXText, it might be a Literal or TemplateLiteral. | ||
// Check if it's inside JSX. | ||
if (isLiteralInsideJSX(node)) { | ||
// If it's a Literal/TemplateLiteral inside a JSXExpressionContainer within JSXElement, | ||
// treat it like JSX text and report with `forJsxText`. | ||
context.report({ node, messageId: 'forJsxText' }); | ||
return; | ||
} | ||
/* istanbul ignore next */ | ||
// If neither JSXText nor a Literal inside JSX, fall back to default messageId. | ||
context.report({ node, messageId: 'default' }); | ||
@@ -304,14 +381,2 @@ }; | ||
}, | ||
'Property > :matches(Literal,TemplateLiteral,TSAsExpression)'(node) { | ||
const parent = node.parent; | ||
if (((0, helpers_1.isIdentifier)(parent.key) && isIgnoredName(parent.key.name)) || | ||
(((0, helpers_1.isLiteral)(parent.key) || (0, helpers_1.isTemplateLiteral)(parent.key)) && | ||
isIgnoredName((0, helpers_1.getText)(parent.key)))) { | ||
// Unwrap TSAsExpression nodes | ||
const unwrappedNode = unwrapTSAsExpression(node); | ||
if ((0, helpers_1.isLiteral)(unwrappedNode) || (0, helpers_1.isTemplateLiteral)(unwrappedNode)) { | ||
visited.add(unwrappedNode); | ||
} | ||
} | ||
}, | ||
'MemberExpression[computed=true] > :matches(Literal,TemplateLiteral)'(node) { | ||
@@ -370,3 +435,2 @@ // obj["key with space"] | ||
'Literal:exit'(node) { | ||
// visited and passed linting | ||
if (visited.has(node)) | ||
@@ -379,13 +443,9 @@ return; | ||
return; | ||
// | ||
// TYPESCRIPT | ||
// todo: that doesn't work | ||
// if (tsService) { | ||
// const typeObj = tsService.getTypeAtLocation(node.parent) | ||
// | ||
// // var a: 'abc' = 'abc' | ||
// if (typeObj.isStringLiteral() && typeObj.symbol) { | ||
// return | ||
// } | ||
// } | ||
if (isAssignedToIgnoredVariable(node, isIgnoredName)) { | ||
return; // Do not report this literal | ||
} | ||
// New check: if the literal is inside an ignored property, do not report | ||
if (isInsideIgnoredProperty(node)) { | ||
return; | ||
} | ||
context.report({ node, messageId: 'default' }); | ||
@@ -399,2 +459,9 @@ }, | ||
return; | ||
if (isAssignedToIgnoredVariable(node, isIgnoredName)) { | ||
return; // Do not report this template literal | ||
} | ||
// New check: if the template literal is inside an ignored property, do not report | ||
if (isInsideIgnoredProperty(node)) { | ||
return; | ||
} | ||
context.report({ node, messageId: 'default' }); | ||
@@ -401,0 +468,0 @@ }, |
{ | ||
"name": "eslint-plugin-lingui", | ||
"version": "0.8.2", | ||
"version": "0.8.3", | ||
"description": "ESLint plugin for Lingui", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
86946
1345