@typescript-eslint/eslint-plugin
Advanced tools
Comparing version 8.21.1-alpha.8 to 8.21.1-alpha.9
@@ -141,2 +141,22 @@ "use strict"; | ||
} | ||
function getTypeAnnotationForViolatingNode(node, type, initializerType) { | ||
const annotation = checker.typeToString(type); | ||
// verify the about-to-be-added type annotation is in-scope | ||
if (tsutils.isTypeFlagSet(initializerType, ts.TypeFlags.EnumLiteral)) { | ||
const scope = context.sourceCode.getScope(node); | ||
const variable = utils_1.ASTUtils.findVariable(scope, annotation); | ||
if (variable == null) { | ||
return null; | ||
} | ||
const definition = variable.defs.find(def => def.isTypeDefinition); | ||
if (definition == null) { | ||
return null; | ||
} | ||
const definitionType = services.getTypeAtLocation(definition.node); | ||
if (definitionType !== type) { | ||
return null; | ||
} | ||
} | ||
return annotation; | ||
} | ||
return { | ||
@@ -172,2 +192,29 @@ [`${functionScopeBoundaries}:exit`](node) { | ||
})(); | ||
const typeAnnotation = (() => { | ||
if (esNode.type !== utils_1.AST_NODE_TYPES.PropertyDefinition) { | ||
return null; | ||
} | ||
if (esNode.typeAnnotation || !esNode.value) { | ||
return null; | ||
} | ||
if (nameNode.type !== utils_1.AST_NODE_TYPES.Identifier) { | ||
return null; | ||
} | ||
const hasConstructorModifications = finalizedClassScope.memberHasConstructorModifications(nameNode.name); | ||
if (!hasConstructorModifications) { | ||
return null; | ||
} | ||
const violatingType = services.getTypeAtLocation(esNode); | ||
const initializerType = services.getTypeAtLocation(esNode.value); | ||
// if the RHS is a literal, its type would be narrowed, while the | ||
// type of the initializer (which isn't `readonly`) would be the | ||
// widened type | ||
if (initializerType === violatingType) { | ||
return null; | ||
} | ||
if (!tsutils.isLiteralType(initializerType)) { | ||
return null; | ||
} | ||
return getTypeAnnotationForViolatingNode(esNode, violatingType, initializerType); | ||
})(); | ||
context.report({ | ||
@@ -179,3 +226,8 @@ ...reportNodeOrLoc, | ||
}, | ||
fix: fixer => fixer.insertTextBefore(nameNode, 'readonly '), | ||
*fix(fixer) { | ||
yield fixer.insertTextBefore(nameNode, 'readonly '); | ||
if (typeAnnotation) { | ||
yield fixer.insertTextAfter(nameNode, `: ${typeAnnotation}`); | ||
} | ||
}, | ||
}); | ||
@@ -216,2 +268,3 @@ } | ||
memberVariableModifications = new Set(); | ||
memberVariableWithConstructorModifications = new Set(); | ||
privateModifiableMembers = new Map(); | ||
@@ -257,2 +310,3 @@ privateModifiableStatics = new Map(); | ||
this.constructorScopeDepth === DIRECTLY_INSIDE_CONSTRUCTOR) { | ||
this.memberVariableWithConstructorModifications.add(node.name.text); | ||
return; | ||
@@ -340,3 +394,6 @@ } | ||
} | ||
memberHasConstructorModifications(name) { | ||
return this.memberVariableWithConstructorModifications.has(name); | ||
} | ||
} | ||
//# sourceMappingURL=prefer-readonly.js.map |
{ | ||
"name": "@typescript-eslint/eslint-plugin", | ||
"version": "8.21.1-alpha.8", | ||
"version": "8.21.1-alpha.9", | ||
"description": "TypeScript plugin for ESLint", | ||
@@ -64,6 +64,6 @@ "files": [ | ||
"@eslint-community/regexpp": "^4.10.0", | ||
"@typescript-eslint/scope-manager": "8.21.1-alpha.8", | ||
"@typescript-eslint/type-utils": "8.21.1-alpha.8", | ||
"@typescript-eslint/utils": "8.21.1-alpha.8", | ||
"@typescript-eslint/visitor-keys": "8.21.1-alpha.8", | ||
"@typescript-eslint/scope-manager": "8.21.1-alpha.9", | ||
"@typescript-eslint/type-utils": "8.21.1-alpha.9", | ||
"@typescript-eslint/utils": "8.21.1-alpha.9", | ||
"@typescript-eslint/visitor-keys": "8.21.1-alpha.9", | ||
"graphemer": "^1.4.0", | ||
@@ -79,4 +79,4 @@ "ignore": "^5.3.1", | ||
"@types/natural-compare": "*", | ||
"@typescript-eslint/rule-schema-to-typescript-types": "8.21.1-alpha.8", | ||
"@typescript-eslint/rule-tester": "8.21.1-alpha.8", | ||
"@typescript-eslint/rule-schema-to-typescript-types": "8.21.1-alpha.9", | ||
"@typescript-eslint/rule-tester": "8.21.1-alpha.9", | ||
"ajv": "^6.12.6", | ||
@@ -83,0 +83,0 @@ "cross-env": "^7.0.3", |
Sorry, the diff of this file is not supported yet
2839837
32721
+ Added@eslint/config-array@0.19.2(transitive)
+ Added@eslint/object-schema@2.1.6(transitive)
+ Added@typescript-eslint/scope-manager@8.21.1-alpha.9(transitive)
+ Added@typescript-eslint/type-utils@8.21.1-alpha.9(transitive)
+ Added@typescript-eslint/types@8.21.1-alpha.9(transitive)
+ Added@typescript-eslint/typescript-estree@8.21.1-alpha.9(transitive)
+ Added@typescript-eslint/utils@8.21.1-alpha.9(transitive)
+ Added@typescript-eslint/visitor-keys@8.21.1-alpha.9(transitive)
- Removed@eslint/config-array@0.19.1(transitive)
- Removed@eslint/object-schema@2.1.5(transitive)
- Removed@typescript-eslint/scope-manager@8.21.1-alpha.8(transitive)
- Removed@typescript-eslint/type-utils@8.21.1-alpha.8(transitive)
- Removed@typescript-eslint/types@8.21.1-alpha.8(transitive)
- Removed@typescript-eslint/typescript-estree@8.21.1-alpha.8(transitive)
- Removed@typescript-eslint/utils@8.21.1-alpha.8(transitive)
- Removed@typescript-eslint/visitor-keys@8.21.1-alpha.8(transitive)