Skip to content

Commit fd3fda6

Browse files
committed
Replace as unknown as casts with as BaseType as SpecificType pattern
The intermediate `as unknown` cast is unnecessary when going through a common base type. Using `as Statement as JS.Import` instead of `as unknown as JS.Import` is both more readable and self-documenting.
1 parent b278318 commit fd3fda6

7 files changed

Lines changed: 14 additions & 14 deletions

File tree

rewrite-javascript/rewrite/src/javascript/cleanup/prefer-optional-chain.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class PreferOptionalChain extends Recipe {
5454
// Check if the false part is undefined
5555
// Note: We only convert when the false part is undefined, not null,
5656
// because optional chaining returns undefined (not null) when the target is nullish.
57-
const falsePart = visited.falsePart as unknown as Expression;
57+
const falsePart = visited.falsePart as Expression;
5858
const isUndefinedFalse = falsePart.kind === J.Kind.Identifier &&
5959
(falsePart as J.Identifier).simpleName === 'undefined';
6060

@@ -63,7 +63,7 @@ export class PreferOptionalChain extends Recipe {
6363
}
6464

6565
// Check if the true part accesses a property/method on the condition
66-
const truePart = visited.truePart as unknown as Expression;
66+
const truePart = visited.truePart as Expression;
6767
const result = this.extractOptionalChainTarget(truePart, conditionName);
6868

6969
if (!result) {
@@ -118,7 +118,7 @@ export class PreferOptionalChain extends Recipe {
118118
// Handle MethodInvocation: foo.bar()
119119
if (expr.kind === J.Kind.MethodInvocation) {
120120
const methodInvocation = expr as J.MethodInvocation;
121-
const selectExpr = methodInvocation.select as unknown as Expression | undefined;
121+
const selectExpr = methodInvocation.select as Expression | undefined;
122122
if (selectExpr?.kind === J.Kind.Identifier) {
123123
const select = selectExpr as J.Identifier;
124124
if (select.simpleName === targetName) {

rewrite-javascript/rewrite/src/javascript/cleanup/use-object-property-shorthand.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {Recipe} from "../../recipe";
1818
import {TreeVisitor} from "../../visitor";
1919
import {ExecutionContext} from "../../execution";
2020
import {JavaScriptVisitor} from "../visitor";
21-
import {Expression, J} from "../../java";
21+
import {Expression, J, Statement} from "../../java";
2222
import {JS} from "../tree";
2323
import {create as produce} from "mutative";
2424
import {findMarker} from "../../markers";
@@ -54,8 +54,8 @@ export class UseObjectPropertyShorthand extends Recipe {
5454
// For tree types, the padded value IS the element (intersection type)
5555
const simplifiedBindings = visited.bindings.elements.map(right => {
5656
if (right.kind === JS.Kind.BindingElement) {
57-
const binding = right as unknown as JS.BindingElement;
58-
const propNameExpr = binding.propertyName as unknown as Expression | undefined;
57+
const binding = right as J as JS.BindingElement;
58+
const propNameExpr = binding.propertyName as Expression | undefined;
5959
if (propNameExpr?.kind === J.Kind.Identifier) {
6060
const propName = (propNameExpr as J.Identifier).simpleName;
6161

@@ -111,8 +111,8 @@ export class UseObjectPropertyShorthand extends Recipe {
111111

112112
const simplifiedStatements = statements.map(stmt => {
113113
if (stmt.kind === JS.Kind.PropertyAssignment) {
114-
const prop = stmt as unknown as JS.PropertyAssignment;
115-
const nameExpr = prop.name as unknown as Expression;
114+
const prop = stmt as Statement as JS.PropertyAssignment;
115+
const nameExpr = prop.name as Expression;
116116
if (nameExpr.kind === J.Kind.Identifier) {
117117
const propName = (nameExpr as J.Identifier).simpleName;
118118

rewrite-javascript/rewrite/src/javascript/migrate/es6/remove-duplicate-object-keys.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class RemoveDuplicateObjectKeys extends Recipe {
5151
for (let i = 0; i < statements.length; i++) {
5252
const stmt = statements[i];
5353
if (stmt.kind === JS.Kind.PropertyAssignment) {
54-
const prop = stmt as unknown as JS.PropertyAssignment;
54+
const prop = stmt as Statement as JS.PropertyAssignment;
5555
const propName = this.getPropertyName(prop);
5656
propertyNames.push(propName);
5757

@@ -98,7 +98,7 @@ export class RemoveDuplicateObjectKeys extends Recipe {
9898
}
9999

100100
private getPropertyName(prop: JS.PropertyAssignment): string | null {
101-
const name = prop.name as unknown as Expression;
101+
const name = prop.name as Expression;
102102

103103
// Handle identifier: { foo: 1 }
104104
if (name.kind === J.Kind.Identifier) {

rewrite-javascript/rewrite/src/javascript/recipes/change-import.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export class ChangeImport extends Recipe {
130130

131131
// First pass: check if the old import exists and capture any alias
132132
for (const statement of cu.statements) {
133-
const stmt = statement as unknown as Statement;
133+
const stmt = statement as Statement;
134134
if (stmt.kind === JS.Kind.Import) {
135135
const jsImport = stmt as JS.Import;
136136
const aliasInfo = this.checkForOldImport(jsImport);

rewrite-javascript/rewrite/src/javascript/templating/placeholder-replacement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class PlaceholderReplacementVisitor extends JavaScriptVisitor<any> {
106106
override async visitBlock(block: J.Block, p: any): Promise<J | undefined> {
107107
const hasPlaceholder = block.statements.some(stmt => {
108108
if (stmt.kind === JS.Kind.ExpressionStatement) {
109-
const exprStmt = stmt as unknown as JS.ExpressionStatement;
109+
const exprStmt = stmt as Statement as JS.ExpressionStatement;
110110
return this.isPlaceholder(exprStmt.expression);
111111
}
112112
return this.isPlaceholder(stmt);

rewrite-javascript/rewrite/src/javascript/templating/rewrite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ export function flattenBlock<P>(
273273
const newStatements: typeof parentBlock.statements = [];
274274

275275
for (const stmt of parentBlock.statements) {
276-
const stmtElement = stmt as unknown as Statement;
276+
const stmtElement = stmt as Statement;
277277
if (stmtElement === block || stmtElement.id === block.id) {
278278
// Splice in the inner block's statements
279279
for (let i = 0; i < block.statements.length; i++) {

rewrite-javascript/rewrite/src/javascript/templating/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ export class PlaceholderUtils {
300300
if (body.statements.length === 0) {
301301
throw new Error(`${contextName} function body is empty`);
302302
} else if (body.statements.length === 1) {
303-
const stmt = body.statements[0] as unknown as Statement;
303+
const stmt = body.statements[0] as Statement;
304304

305305
// Single expression statement → extract the expression
306306
if (stmt.kind === JS.Kind.ExpressionStatement) {

0 commit comments

Comments
 (0)