Skip to content

Commit 8f904e0

Browse files
authored
Enable the noImplicitThis tsc option (#14545)
* enable noImplicitThis * refactor: cache type annotation in path.data * update test fixtures
1 parent 1bc9949 commit 8f904e0

24 files changed

Lines changed: 192 additions & 93 deletions

File tree

packages/babel-core/src/config/full.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ function chain(a, b) {
494494
const fns = [a, b].filter(Boolean);
495495
if (fns.length <= 1) return fns[0];
496496

497-
return function (...args) {
497+
return function (this: unknown, ...args) {
498498
for (const fn of fns) {
499499
fn.apply(this, args);
500500
}

packages/babel-generator/src/generators/classes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export function _classMethodHead(this: Printer, node) {
199199
this._methodHead(node);
200200
}
201201

202-
export function StaticBlock(node: t.StaticBlock) {
202+
export function StaticBlock(this: Printer, node: t.StaticBlock) {
203203
this.word("static");
204204
this.space();
205205
this.token("{");

packages/babel-generator/src/generators/expressions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export function Import(this: Printer) {
219219
}
220220

221221
function buildYieldAwait(keyword: string) {
222-
return function (node: any) {
222+
return function (this: Printer, node: any) {
223223
this.word(keyword);
224224

225225
if (node.delegate) {
@@ -348,7 +348,7 @@ export function V8IntrinsicIdentifier(
348348
this.word(node.name);
349349
}
350350

351-
export function ModuleExpression(node: t.ModuleExpression) {
351+
export function ModuleExpression(this: Printer, node: t.ModuleExpression) {
352352
this.word("module");
353353
this.space();
354354
this.token("{");

packages/babel-generator/src/generators/flow.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export function DeclareFunction(
6868
this.semicolon();
6969
}
7070

71-
export function InferredPredicate(/*node: Object*/) {
71+
export function InferredPredicate(this: Printer) {
7272
this.token("%");
7373
this.word("checks");
7474
}
@@ -159,7 +159,7 @@ export function DeclareExportDeclaration(
159159
FlowExportDeclaration.apply(this, arguments);
160160
}
161161

162-
export function DeclareExportAllDeclaration(/*node: Object*/) {
162+
export function DeclareExportAllDeclaration(this: Printer) {
163163
this.word("declare");
164164
this.space();
165165
ExportAllDeclaration.apply(this, arguments);
@@ -258,7 +258,7 @@ export function EnumStringMember(this: Printer, node: t.EnumStringMember) {
258258
enumInitializedMember(this, node);
259259
}
260260

261-
function FlowExportDeclaration(node: any) {
261+
function FlowExportDeclaration(this: Printer, node: any) {
262262
if (node.declaration) {
263263
const declar = node.declaration;
264264
this.print(declar, node);
@@ -403,7 +403,7 @@ export function InterfaceDeclaration(
403403
this._interfaceish(node);
404404
}
405405

406-
function andSeparator() {
406+
function andSeparator(this: Printer) {
407407
this.space();
408408
this.token("&");
409409
this.space();
@@ -707,7 +707,7 @@ export function SymbolTypeAnnotation(this: Printer) {
707707
this.word("symbol");
708708
}
709709

710-
function orSeparator() {
710+
function orSeparator(this: Printer) {
711711
this.space();
712712
this.token("|");
713713
this.space();

packages/babel-generator/src/generators/jsx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function JSXElement(this: Printer, node: t.JSXElement) {
7575
this.print(node.closingElement, node);
7676
}
7777

78-
function spaceSeparator() {
78+
function spaceSeparator(this: Printer) {
7979
this.space();
8080
}
8181

packages/babel-generator/src/generators/modules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export function ExportDefaultDeclaration(
119119
ExportDeclaration.apply(this, arguments);
120120
}
121121

122-
function ExportDeclaration(node: any) {
122+
function ExportDeclaration(this: Printer, node: any) {
123123
if (node.declaration) {
124124
const declar = node.declaration;
125125
this.print(declar, node);

packages/babel-generator/src/generators/statements.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function WhileStatement(this: Printer, node: t.WhileStatement) {
9090
}
9191

9292
const buildForXStatement = function (op) {
93-
return function (node: any) {
93+
return function (this: Printer, node: any) {
9494
this.word("for");
9595
this.space();
9696
if (op === "of" && node.await) {
@@ -125,7 +125,7 @@ export function DoWhileStatement(this: Printer, node: t.DoWhileStatement) {
125125
}
126126

127127
function buildLabelStatement(prefix, key = "label") {
128-
return function (node: any) {
128+
return function (this: Printer, node: any) {
129129
this.word(prefix);
130130

131131
const label = node[key];
@@ -232,7 +232,7 @@ export function DebuggerStatement(this: Printer) {
232232
this.semicolon();
233233
}
234234

235-
function variableDeclarationIndent() {
235+
function variableDeclarationIndent(this: Printer) {
236236
// "let " or "var " indentation.
237237
this.token(",");
238238
this.newline();
@@ -241,7 +241,7 @@ function variableDeclarationIndent() {
241241
}
242242
}
243243

244-
function constDeclarationIndent() {
244+
function constDeclarationIndent(this: Printer) {
245245
// "const " indentation.
246246
this.token(",");
247247
this.newline();

packages/babel-generator/src/generators/typescript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export function TSNullKeyword(this: Printer) {
208208
export function TSNeverKeyword(this: Printer) {
209209
this.word("never");
210210
}
211-
export function TSIntrinsicKeyword() {
211+
export function TSIntrinsicKeyword(this: Printer) {
212212
this.word("intrinsic");
213213
}
214214

packages/babel-generator/src/printer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ type GeneratorFunctions = typeof generatorFunctions;
720720
interface Printer extends GeneratorFunctions {}
721721
export default Printer;
722722

723-
function commaSeparator() {
723+
function commaSeparator(this: Printer) {
724724
this.token(",");
725725
this.space();
726726
}

packages/babel-helper-replace-supers/src/index.ts

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
thisExpression,
1717
} from "@babel/types";
1818
import type * as t from "@babel/types";
19+
import type { File } from "@babel/core";
1920

2021
// TODO (Babel 8): Don't export this.
2122
export {
@@ -72,8 +73,32 @@ const unshadowSuperBindingVisitor = traverse.visitors.merge<{
7273
},
7374
]);
7475

76+
type SharedState = {
77+
file: File;
78+
scope: Scope;
79+
isDerivedConstructor: boolean;
80+
isStatic: boolean;
81+
isPrivateMethod: boolean;
82+
getObjectRef: Function;
83+
getSuperRef: Function;
84+
// we dont need boundGet here, but memberExpressionToFunctions handler needs it.
85+
boundGet: HandlerState["get"];
86+
};
87+
88+
type Handler = HandlerState<SharedState> & SharedState;
89+
type SuperMember = NodePath<
90+
| t.MemberExpression & {
91+
object: t.Super;
92+
property: Exclude<t.MemberExpression["property"], t.PrivateName>;
93+
}
94+
>;
95+
7596
const specHandlers = {
76-
memoise(superMember, count) {
97+
memoise(
98+
this: Handler & typeof specHandlers,
99+
superMember: SuperMember,
100+
count,
101+
) {
77102
const { scope, node } = superMember;
78103
const { computed, property } = node;
79104
if (!computed) {
@@ -88,7 +113,7 @@ const specHandlers = {
88113
this.memoiser.set(property, memo, count);
89114
},
90115

91-
prop(superMember) {
116+
prop(this: Handler & typeof specHandlers, superMember: SuperMember) {
92117
const { computed, property } = superMember.node;
93118
if (this.memoiser.has(property)) {
94119
return cloneNode(this.memoiser.get(property));
@@ -98,14 +123,18 @@ const specHandlers = {
98123
return cloneNode(property);
99124
}
100125

101-
return stringLiteral(property.name);
126+
return stringLiteral((property as t.Identifier).name);
102127
},
103128

104-
get(superMember) {
129+
get(this: Handler & typeof specHandlers, superMember: SuperMember) {
105130
return this._get(superMember, this._getThisRefs());
106131
},
107132

108-
_get(superMember, thisRefs) {
133+
_get(
134+
this: Handler & typeof specHandlers,
135+
superMember: SuperMember,
136+
thisRefs,
137+
) {
109138
const proto = getPrototypeOfExpression(
110139
this.getObjectRef(),
111140
this.isStatic,
@@ -119,7 +148,7 @@ const specHandlers = {
119148
]);
120149
},
121150

122-
_getThisRefs() {
151+
_getThisRefs(this: Handler & typeof specHandlers) {
123152
if (!this.isDerivedConstructor) {
124153
return { this: thisExpression() };
125154
}
@@ -130,7 +159,7 @@ const specHandlers = {
130159
};
131160
},
132161

133-
set(superMember, value) {
162+
set(this: Handler & typeof specHandlers, superMember: SuperMember, value) {
134163
const thisRefs = this._getThisRefs();
135164
const proto = getPrototypeOfExpression(
136165
this.getObjectRef(),
@@ -147,13 +176,16 @@ const specHandlers = {
147176
]);
148177
},
149178

150-
destructureSet(superMember) {
179+
destructureSet(
180+
this: Handler & typeof specHandlers,
181+
superMember: SuperMember,
182+
) {
151183
throw superMember.buildCodeFrameError(
152184
`Destructuring to a super field is not supported yet.`,
153185
);
154186
},
155187

156-
call(superMember, args) {
188+
call(this: Handler & typeof specHandlers, superMember: SuperMember, args) {
157189
const thisRefs = this._getThisRefs();
158190
return optimiseCall(
159191
this._get(superMember, thisRefs),
@@ -163,7 +195,11 @@ const specHandlers = {
163195
);
164196
},
165197

166-
optionalCall(superMember, args) {
198+
optionalCall(
199+
this: Handler & typeof specHandlers,
200+
superMember: SuperMember,
201+
args,
202+
) {
167203
const thisRefs = this._getThisRefs();
168204
return optimiseCall(
169205
this._get(superMember, thisRefs),
@@ -177,7 +213,7 @@ const specHandlers = {
177213
const looseHandlers = {
178214
...specHandlers,
179215

180-
prop(superMember) {
216+
prop(this: Handler & typeof specHandlers, superMember: SuperMember) {
181217
const { property } = superMember.node;
182218
if (this.memoiser.has(property)) {
183219
return cloneNode(this.memoiser.get(property));
@@ -186,7 +222,7 @@ const looseHandlers = {
186222
return cloneNode(property);
187223
},
188224

189-
get(superMember) {
225+
get(this: Handler & typeof specHandlers, superMember: SuperMember) {
190226
const { isStatic, getSuperRef } = this;
191227
const { computed } = superMember.node;
192228
const prop = this.prop(superMember);
@@ -206,7 +242,7 @@ const looseHandlers = {
206242
return memberExpression(object, prop, computed);
207243
},
208244

209-
set(superMember, value) {
245+
set(this: Handler & typeof specHandlers, superMember: SuperMember, value) {
210246
const { computed } = superMember.node;
211247
const prop = this.prop(superMember);
212248

@@ -217,18 +253,25 @@ const looseHandlers = {
217253
);
218254
},
219255

220-
destructureSet(superMember) {
256+
destructureSet(
257+
this: Handler & typeof specHandlers,
258+
superMember: SuperMember,
259+
) {
221260
const { computed } = superMember.node;
222261
const prop = this.prop(superMember);
223262

224263
return memberExpression(thisExpression(), prop, computed);
225264
},
226265

227-
call(superMember, args) {
266+
call(this: Handler & typeof specHandlers, superMember: SuperMember, args) {
228267
return optimiseCall(this.get(superMember), thisExpression(), args, false);
229268
},
230269

231-
optionalCall(superMember, args) {
270+
optionalCall(
271+
this: Handler & typeof specHandlers,
272+
superMember: SuperMember,
273+
args,
274+
) {
232275
return optimiseCall(this.get(superMember), thisExpression(), args, true);
233276
},
234277
};

0 commit comments

Comments
 (0)