@rehearsal/codefixes
Advanced tools
Comparing version 2.0.0-beta to 2.0.1-beta
@@ -0,1 +1,2 @@ | ||
import ts from 'typescript'; | ||
import type { CodeFixAction } from 'typescript'; | ||
@@ -6,4 +7,6 @@ import type { CodeFix, DiagnosticWithContext } from '../types.js'; | ||
getCodeAction(diagnostic: DiagnosticWithContext): CodeFixAction | undefined; | ||
filterFunctionParams(fix: CodeFixAction, diagnostic: DiagnosticWithContext): CodeFixAction | undefined; | ||
filterUnresolvedTypes(fix: CodeFixAction, diagnostic: DiagnosticWithContext): CodeFixAction | undefined; | ||
findTargetNode(positionToAddType: number, diagnostic: DiagnosticWithContext): ts.Node | undefined; | ||
findTargetJSDocType(node: ts.Node): ts.TypeNode | undefined; | ||
} | ||
//# sourceMappingURL=annotateWithStrictTypeFromJSDoc.d.ts.map |
@@ -5,3 +5,3 @@ import { canTypeBeResolved, findNodeEndsAtPosition } from '@rehearsal/ts-utils'; | ||
import { TypescriptCodeFixCollection } from '../typescript-codefix-collection.js'; | ||
const { isFunctionDeclaration, isMethodDeclaration, isPropertyDeclaration, isVariableDeclaration, getJSDocType, } = ts; | ||
const { isFunctionDeclaration, isMethodDeclaration, isParameter, isPropertyDeclaration, isVariableDeclaration, getJSDocParameterTags, getJSDocReturnType, getJSDocType, } = ts; | ||
export class AnnotateWithStrictTypeFromJSDoc { | ||
@@ -26,7 +26,7 @@ constructor() { | ||
isVariableDeclaration(diagnostic.node.parent)) { | ||
return this.filterFunctionParams(fix, diagnostic); | ||
return this.filterUnresolvedTypes(fix, diagnostic); | ||
} | ||
return fix; | ||
} | ||
filterFunctionParams(fix, diagnostic) { | ||
filterUnresolvedTypes(fix, diagnostic) { | ||
const safeChanges = []; | ||
@@ -36,5 +36,5 @@ for (const changes of fix.changes) { | ||
for (const textChanges of changes.textChanges) { | ||
const node = findNodeEndsAtPosition(diagnostic.file, textChanges.span.start); | ||
const node = this.findTargetNode(textChanges.span.start, diagnostic); | ||
if (node) { | ||
const typeNode = getJSDocType(node) || getJSDocType(node.parent); | ||
const typeNode = this.findTargetJSDocType(node); | ||
if (typeNode && !canTypeBeResolved(diagnostic.checker, typeNode)) { | ||
@@ -55,3 +55,29 @@ continue; | ||
} | ||
findTargetNode(positionToAddType, diagnostic) { | ||
const braces = diagnostic.service.getBraceMatchingAtPosition(diagnostic.file.fileName, positionToAddType - 1); | ||
if (braces.length) { | ||
return findNodeEndsAtPosition(diagnostic.file, braces[0].start); | ||
} | ||
return findNodeEndsAtPosition(diagnostic.file, positionToAddType); | ||
} | ||
findTargetJSDocType(node) { | ||
/* | ||
TypeScript's parser incorrectly parses JSDoc in some cases (nodes have wrong `kind` value) | ||
that causing "Cannot read properties of undefined (reading 'kind')" issue | ||
the `try ... catch` is to handle those cases | ||
*/ | ||
try { | ||
if (isMethodDeclaration(node.parent) || isFunctionDeclaration(node.parent)) { | ||
return getJSDocReturnType(node.parent); | ||
} | ||
if (isParameter(node.parent)) { | ||
return getJSDocParameterTags(node.parent).find((tag) => tag.name.getText() === node.getText())?.typeExpression?.type; | ||
} | ||
return getJSDocType(node) || getJSDocType(node.parent); | ||
} | ||
catch (_) { | ||
return undefined; | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=annotateWithStrictTypeFromJSDoc.js.map |
@@ -47,2 +47,4 @@ import ts from 'typescript'; | ||
hint = hint.replace(/{[^}]+}/gm, (key) => replacements[key] || key); | ||
// Make sure the hint is a single line | ||
hint = hint.replace(/(\r\n|\n|\r)/gm, '. '); | ||
return hint; | ||
@@ -49,0 +51,0 @@ } |
@@ -6,2 +6,3 @@ import { dirname, resolve } from 'node:path'; | ||
import { isCodeFixSupported } from './safe-codefixes.js'; | ||
import { Diagnostics } from './diagnosticInformationMap.generated.js'; | ||
const __filename = fileURLToPath(import.meta.url); | ||
@@ -28,3 +29,14 @@ const __dirname = dirname(__filename); | ||
}; | ||
const fixes = languageService.getCodeFixesAtPosition(diagnostic.file.fileName, diagnostic.start, diagnostic.start + diagnostic.length, [diagnostic.code], this.getFormatCodeSettingsForFile(diagnostic.file.fileName), userPreferences); | ||
let fixes = []; | ||
try { | ||
fixes = languageService.getCodeFixesAtPosition(diagnostic.file.fileName, diagnostic.start, diagnostic.start + diagnostic.length, [diagnostic.code], this.getFormatCodeSettingsForFile(diagnostic.file.fileName), userPreferences); | ||
} | ||
catch (e) { | ||
const hideError = diagnostic.code == Diagnostics.TS2345.code && | ||
e instanceof TypeError && | ||
e.message.includes(`Cannot read properties of undefined (reading 'flags')`); | ||
if (!hideError) { | ||
throw e; | ||
} | ||
} | ||
const filteredCodeFixes = []; | ||
@@ -31,0 +43,0 @@ for (let fix of fixes) { |
{ | ||
"name": "@rehearsal/codefixes", | ||
"version": "2.0.0-beta", | ||
"version": "2.0.1-beta", | ||
"description": "Rehearsal Dependency Codefixes Collection", | ||
@@ -32,3 +32,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@rehearsal/ts-utils": "2.0.0-beta" | ||
"@rehearsal/ts-utils": "2.0.1-beta" | ||
}, | ||
@@ -43,4 +43,6 @@ "devDependencies": { | ||
"tmp": "^0.2.1", | ||
"vitest": "^0.29.8", | ||
"@rehearsal/service": "2.0.0-beta" | ||
"vitest": "^0.30.0", | ||
"@rehearsal/migrate": "2.0.1-beta", | ||
"@rehearsal/reporter": "2.0.1-beta", | ||
"@rehearsal/service": "2.0.1-beta" | ||
}, | ||
@@ -47,0 +49,0 @@ "peerDependencies": { |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
16658
1381173
11
80
+ Added@rehearsal/ts-utils@2.0.1-beta(transitive)
- Removed@rehearsal/ts-utils@2.0.0-beta(transitive)