Socket
Socket
Sign inDemoInstall

@typescript-eslint/eslint-plugin

Package Overview
Dependencies
Maintainers
2
Versions
3773
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@typescript-eslint/eslint-plugin - npm Package Compare versions

Comparing version 8.0.2-alpha.9 to 8.0.2-alpha.10

dist/util/getFixOrSuggest.js

39

dist/rules/no-unsafe-return.js

@@ -40,5 +40,5 @@ "use strict";

messages: {
unsafeReturn: 'Unsafe return of an {{type}} typed value.',
unsafeReturn: 'Unsafe return of a value of type {{type}}.',
unsafeReturnThis: [
'Unsafe return of an `{{type}}` typed value. `this` is typed as `any`.',
'Unsafe return of a value of type `{{type}}`. `this` is typed as `any`.',
'You can try to fix this by turning on the `noImplicitThis` compiler option, or adding a `this` parameter to the function.',

@@ -72,3 +72,4 @@ ].join('\n'),

const tsNode = services.esTreeNodeToTSNodeMap.get(returnNode);
const anyType = (0, util_1.isAnyOrAnyArrayTypeDiscriminated)(tsNode, checker);
const type = checker.getTypeAtLocation(tsNode);
const anyType = (0, util_1.discriminateAnyType)(type, checker, services.program, tsNode);
const functionNode = getParentFunctionNode(returnNode);

@@ -92,10 +93,21 @@ /* istanbul ignore if */ if (!functionNode) {

}
const callSignatures = tsutils.getCallSignaturesOfType(functionType);
// If there is an explicit type annotation *and* that type matches the actual
// function return type, we shouldn't complain (it's intentional, even if unsafe)
if (functionTSNode.type) {
for (const signature of tsutils.getCallSignaturesOfType(functionType)) {
if (returnNodeType === signature.getReturnType() ||
(0, util_1.isTypeFlagSet)(signature.getReturnType(), ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
for (const signature of callSignatures) {
const signatureReturnType = signature.getReturnType();
if (returnNodeType === signatureReturnType ||
(0, util_1.isTypeFlagSet)(signatureReturnType, ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
return;
}
if (functionNode.async) {
const awaitedSignatureReturnType = checker.getAwaitedType(signatureReturnType);
const awaitedReturnNodeType = checker.getAwaitedType(returnNodeType);
if (awaitedReturnNodeType === awaitedSignatureReturnType ||
(awaitedSignatureReturnType &&
(0, util_1.isTypeFlagSet)(awaitedSignatureReturnType, ts.TypeFlags.Any | ts.TypeFlags.Unknown))) {
return;
}
}
}

@@ -106,3 +118,3 @@ }

// and the function is returning any or any[].
for (const signature of functionType.getCallSignatures()) {
for (const signature of callSignatures) {
const functionReturnType = signature.getReturnType();

@@ -117,3 +129,12 @@ if (anyType === util_1.AnyType.Any &&

}
const awaitedType = checker.getAwaitedType(functionReturnType);
if (awaitedType &&
anyType === util_1.AnyType.PromiseAny &&
(0, util_1.isTypeUnknownType)(awaitedType)) {
return;
}
}
if (anyType === util_1.AnyType.PromiseAny && !functionNode.async) {
return;
}
let messageId = 'unsafeReturn';

@@ -138,3 +159,5 @@ const isErrorType = tsutils.isIntrinsicErrorType(returnNodeType);

? '`any`'
: '`any[]`',
: anyType === util_1.AnyType.PromiseAny
? '`Promise<any>`'
: '`any[]`',
},

@@ -141,0 +164,0 @@ });

@@ -136,3 +136,31 @@ "use strict";

};
function getFixer(sourceCode, parserServices, operator, options, chain) {
/**
* Returns the range that needs to be reported from the chain.
* @param chain The chain of logical expressions.
* @param boundary The boundary range that the range to report cannot fall outside.
* @param sourceCode The source code to get tokens.
* @returns The range to report.
*/
function getReportRange(chain, boundary, sourceCode) {
const leftNode = chain[0].node;
const rightNode = chain[chain.length - 1].node;
let leftMost = (0, util_1.nullThrows)(sourceCode.getFirstToken(leftNode), util_1.NullThrowsReasons.MissingToken('any token', leftNode.type));
let rightMost = (0, util_1.nullThrows)(sourceCode.getLastToken(rightNode), util_1.NullThrowsReasons.MissingToken('any token', rightNode.type));
while (leftMost.range[0] > boundary[0]) {
const token = sourceCode.getTokenBefore(leftMost);
if (!token || !(0, util_1.isOpeningParenToken)(token) || token.range[0] < boundary[0]) {
break;
}
leftMost = token;
}
while (rightMost.range[1] < boundary[1]) {
const token = sourceCode.getTokenAfter(rightMost);
if (!token || !(0, util_1.isClosingParenToken)(token) || token.range[1] > boundary[1]) {
break;
}
rightMost = token;
}
return [leftMost.range[0], rightMost.range[1]];
}
function getReportDescriptor(sourceCode, parserServices, node, operator, options, chain) {
const lastOperand = chain[chain.length - 1];

@@ -273,6 +301,18 @@ let useSuggestionFixer;

}
const fix = fixer => fixer.replaceTextRange([chain[0].node.range[0], lastOperand.node.range[1]], newCode);
return useSuggestionFixer
? { suggest: [{ fix, messageId: 'optionalChainSuggest' }] }
: { fix };
const reportRange = getReportRange(chain, node.range, sourceCode);
const fix = fixer => fixer.replaceTextRange(reportRange, newCode);
return {
messageId: 'preferOptionalChain',
loc: {
start: sourceCode.getLocFromIndex(reportRange[0]),
end: sourceCode.getLocFromIndex(reportRange[1]),
},
...(0, util_1.getFixOrSuggest)({
useFix: !useSuggestionFixer,
suggestion: {
messageId: 'optionalChainSuggest',
fix,
},
}),
};
function flattenChainExpression(sourceCode, node) {

@@ -337,3 +377,3 @@ switch (node.type) {

}
function analyzeChain(context, parserServices, options, operator, chain) {
function analyzeChain(context, parserServices, options, node, operator, chain) {
// need at least 2 operands in a chain for it to be a chain

@@ -359,10 +399,3 @@ if (chain.length <= 1 ||

const subChainFlat = subChain.flat();
(0, checkNullishAndReport_1.checkNullishAndReport)(context, parserServices, options, subChainFlat.slice(0, -1).map(({ node }) => node), {
messageId: 'preferOptionalChain',
loc: {
start: subChainFlat[0].node.loc.start,
end: subChainFlat[subChainFlat.length - 1].node.loc.end,
},
...getFixer(context.sourceCode, parserServices, operator, options, subChainFlat),
});
(0, checkNullishAndReport_1.checkNullishAndReport)(context, parserServices, options, subChainFlat.slice(0, -1).map(({ node }) => node), getReportDescriptor(context.sourceCode, parserServices, node, operator, options, subChainFlat));
}

@@ -369,0 +402,0 @@ // we've reached the end of a chain of logical expressions

@@ -159,3 +159,3 @@ "use strict";

if (operand.type === "Invalid" /* OperandValidity.Invalid */) {
(0, analyzeChain_1.analyzeChain)(context, parserServices, options, node.operator, currentChain);
(0, analyzeChain_1.analyzeChain)(context, parserServices, options, node, node.operator, currentChain);
currentChain = [];

@@ -169,3 +169,3 @@ }

if (currentChain.length > 0) {
(0, analyzeChain_1.analyzeChain)(context, parserServices, options, node.operator, currentChain);
(0, analyzeChain_1.analyzeChain)(context, parserServices, options, node, node.operator, currentChain);
}

@@ -172,0 +172,0 @@ },

@@ -37,2 +37,3 @@ "use strict";

__exportStar(require("./isAssignee"), exports);
__exportStar(require("./getFixOrSuggest"), exports);
// this is done for convenience - saves migrating all of the old rules

@@ -39,0 +40,0 @@ __exportStar(require("@typescript-eslint/type-utils"), exports);

{
"name": "@typescript-eslint/eslint-plugin",
"version": "8.0.2-alpha.9",
"version": "8.0.2-alpha.10",
"description": "TypeScript plugin for ESLint",

@@ -63,6 +63,6 @@ "files": [

"@eslint-community/regexpp": "^4.10.0",
"@typescript-eslint/scope-manager": "8.0.2-alpha.9",
"@typescript-eslint/type-utils": "8.0.2-alpha.9",
"@typescript-eslint/utils": "8.0.2-alpha.9",
"@typescript-eslint/visitor-keys": "8.0.2-alpha.9",
"@typescript-eslint/scope-manager": "8.0.2-alpha.10",
"@typescript-eslint/type-utils": "8.0.2-alpha.10",
"@typescript-eslint/utils": "8.0.2-alpha.10",
"@typescript-eslint/visitor-keys": "8.0.2-alpha.10",
"graphemer": "^1.4.0",

@@ -78,4 +78,4 @@ "ignore": "^5.3.1",

"@types/natural-compare": "*",
"@typescript-eslint/rule-schema-to-typescript-types": "8.0.2-alpha.9",
"@typescript-eslint/rule-tester": "8.0.2-alpha.9",
"@typescript-eslint/rule-schema-to-typescript-types": "8.0.2-alpha.10",
"@typescript-eslint/rule-tester": "8.0.2-alpha.10",
"ajv": "^6.12.6",

@@ -82,0 +82,0 @@ "cross-env": "^7.0.3",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc