Skip to content

Commit

Permalink
Enable the noImplicitThis tsc option (#14545)
Browse files Browse the repository at this point in the history
* enable noImplicitThis

* refactor: cache type annotation in path.data

* update test fixtures
  • Loading branch information
JLHwung committed May 22, 2022
1 parent 1bc9949 commit 8f904e0
Show file tree
Hide file tree
Showing 24 changed files with 192 additions and 93 deletions.
2 changes: 1 addition & 1 deletion packages/babel-core/src/config/full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ function chain(a, b) {
const fns = [a, b].filter(Boolean);
if (fns.length <= 1) return fns[0];

return function (...args) {
return function (this: unknown, ...args) {
for (const fn of fns) {
fn.apply(this, args);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/src/generators/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export function _classMethodHead(this: Printer, node) {
this._methodHead(node);
}

export function StaticBlock(node: t.StaticBlock) {
export function StaticBlock(this: Printer, node: t.StaticBlock) {
this.word("static");
this.space();
this.token("{");
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-generator/src/generators/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export function Import(this: Printer) {
}

function buildYieldAwait(keyword: string) {
return function (node: any) {
return function (this: Printer, node: any) {
this.word(keyword);

if (node.delegate) {
Expand Down Expand Up @@ -348,7 +348,7 @@ export function V8IntrinsicIdentifier(
this.word(node.name);
}

export function ModuleExpression(node: t.ModuleExpression) {
export function ModuleExpression(this: Printer, node: t.ModuleExpression) {
this.word("module");
this.space();
this.token("{");
Expand Down
10 changes: 5 additions & 5 deletions packages/babel-generator/src/generators/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function DeclareFunction(
this.semicolon();
}

export function InferredPredicate(/*node: Object*/) {
export function InferredPredicate(this: Printer) {
this.token("%");
this.word("checks");
}
Expand Down Expand Up @@ -159,7 +159,7 @@ export function DeclareExportDeclaration(
FlowExportDeclaration.apply(this, arguments);
}

export function DeclareExportAllDeclaration(/*node: Object*/) {
export function DeclareExportAllDeclaration(this: Printer) {
this.word("declare");
this.space();
ExportAllDeclaration.apply(this, arguments);
Expand Down Expand Up @@ -258,7 +258,7 @@ export function EnumStringMember(this: Printer, node: t.EnumStringMember) {
enumInitializedMember(this, node);
}

function FlowExportDeclaration(node: any) {
function FlowExportDeclaration(this: Printer, node: any) {
if (node.declaration) {
const declar = node.declaration;
this.print(declar, node);
Expand Down Expand Up @@ -403,7 +403,7 @@ export function InterfaceDeclaration(
this._interfaceish(node);
}

function andSeparator() {
function andSeparator(this: Printer) {
this.space();
this.token("&");
this.space();
Expand Down Expand Up @@ -707,7 +707,7 @@ export function SymbolTypeAnnotation(this: Printer) {
this.word("symbol");
}

function orSeparator() {
function orSeparator(this: Printer) {
this.space();
this.token("|");
this.space();
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/src/generators/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function JSXElement(this: Printer, node: t.JSXElement) {
this.print(node.closingElement, node);
}

function spaceSeparator() {
function spaceSeparator(this: Printer) {
this.space();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/src/generators/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export function ExportDefaultDeclaration(
ExportDeclaration.apply(this, arguments);
}

function ExportDeclaration(node: any) {
function ExportDeclaration(this: Printer, node: any) {
if (node.declaration) {
const declar = node.declaration;
this.print(declar, node);
Expand Down
8 changes: 4 additions & 4 deletions packages/babel-generator/src/generators/statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function WhileStatement(this: Printer, node: t.WhileStatement) {
}

const buildForXStatement = function (op) {
return function (node: any) {
return function (this: Printer, node: any) {
this.word("for");
this.space();
if (op === "of" && node.await) {
Expand Down Expand Up @@ -125,7 +125,7 @@ export function DoWhileStatement(this: Printer, node: t.DoWhileStatement) {
}

function buildLabelStatement(prefix, key = "label") {
return function (node: any) {
return function (this: Printer, node: any) {
this.word(prefix);

const label = node[key];
Expand Down Expand Up @@ -232,7 +232,7 @@ export function DebuggerStatement(this: Printer) {
this.semicolon();
}

function variableDeclarationIndent() {
function variableDeclarationIndent(this: Printer) {
// "let " or "var " indentation.
this.token(",");
this.newline();
Expand All @@ -241,7 +241,7 @@ function variableDeclarationIndent() {
}
}

function constDeclarationIndent() {
function constDeclarationIndent(this: Printer) {
// "const " indentation.
this.token(",");
this.newline();
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/src/generators/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export function TSNullKeyword(this: Printer) {
export function TSNeverKeyword(this: Printer) {
this.word("never");
}
export function TSIntrinsicKeyword() {
export function TSIntrinsicKeyword(this: Printer) {
this.word("intrinsic");
}

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ type GeneratorFunctions = typeof generatorFunctions;
interface Printer extends GeneratorFunctions {}
export default Printer;

function commaSeparator() {
function commaSeparator(this: Printer) {
this.token(",");
this.space();
}
75 changes: 59 additions & 16 deletions packages/babel-helper-replace-supers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
thisExpression,
} from "@babel/types";
import type * as t from "@babel/types";
import type { File } from "@babel/core";

// TODO (Babel 8): Don't export this.
export {
Expand Down Expand Up @@ -72,8 +73,32 @@ const unshadowSuperBindingVisitor = traverse.visitors.merge<{
},
]);

type SharedState = {
file: File;
scope: Scope;
isDerivedConstructor: boolean;
isStatic: boolean;
isPrivateMethod: boolean;
getObjectRef: Function;
getSuperRef: Function;
// we dont need boundGet here, but memberExpressionToFunctions handler needs it.
boundGet: HandlerState["get"];
};

type Handler = HandlerState<SharedState> & SharedState;
type SuperMember = NodePath<
| t.MemberExpression & {
object: t.Super;
property: Exclude<t.MemberExpression["property"], t.PrivateName>;
}
>;

const specHandlers = {
memoise(superMember, count) {
memoise(
this: Handler & typeof specHandlers,
superMember: SuperMember,
count,
) {
const { scope, node } = superMember;
const { computed, property } = node;
if (!computed) {
Expand All @@ -88,7 +113,7 @@ const specHandlers = {
this.memoiser.set(property, memo, count);
},

prop(superMember) {
prop(this: Handler & typeof specHandlers, superMember: SuperMember) {
const { computed, property } = superMember.node;
if (this.memoiser.has(property)) {
return cloneNode(this.memoiser.get(property));
Expand All @@ -98,14 +123,18 @@ const specHandlers = {
return cloneNode(property);
}

return stringLiteral(property.name);
return stringLiteral((property as t.Identifier).name);
},

get(superMember) {
get(this: Handler & typeof specHandlers, superMember: SuperMember) {
return this._get(superMember, this._getThisRefs());
},

_get(superMember, thisRefs) {
_get(
this: Handler & typeof specHandlers,
superMember: SuperMember,
thisRefs,
) {
const proto = getPrototypeOfExpression(
this.getObjectRef(),
this.isStatic,
Expand All @@ -119,7 +148,7 @@ const specHandlers = {
]);
},

_getThisRefs() {
_getThisRefs(this: Handler & typeof specHandlers) {
if (!this.isDerivedConstructor) {
return { this: thisExpression() };
}
Expand All @@ -130,7 +159,7 @@ const specHandlers = {
};
},

set(superMember, value) {
set(this: Handler & typeof specHandlers, superMember: SuperMember, value) {
const thisRefs = this._getThisRefs();
const proto = getPrototypeOfExpression(
this.getObjectRef(),
Expand All @@ -147,13 +176,16 @@ const specHandlers = {
]);
},

destructureSet(superMember) {
destructureSet(
this: Handler & typeof specHandlers,
superMember: SuperMember,
) {
throw superMember.buildCodeFrameError(
`Destructuring to a super field is not supported yet.`,
);
},

call(superMember, args) {
call(this: Handler & typeof specHandlers, superMember: SuperMember, args) {
const thisRefs = this._getThisRefs();
return optimiseCall(
this._get(superMember, thisRefs),
Expand All @@ -163,7 +195,11 @@ const specHandlers = {
);
},

optionalCall(superMember, args) {
optionalCall(
this: Handler & typeof specHandlers,
superMember: SuperMember,
args,
) {
const thisRefs = this._getThisRefs();
return optimiseCall(
this._get(superMember, thisRefs),
Expand All @@ -177,7 +213,7 @@ const specHandlers = {
const looseHandlers = {
...specHandlers,

prop(superMember) {
prop(this: Handler & typeof specHandlers, superMember: SuperMember) {
const { property } = superMember.node;
if (this.memoiser.has(property)) {
return cloneNode(this.memoiser.get(property));
Expand All @@ -186,7 +222,7 @@ const looseHandlers = {
return cloneNode(property);
},

get(superMember) {
get(this: Handler & typeof specHandlers, superMember: SuperMember) {
const { isStatic, getSuperRef } = this;
const { computed } = superMember.node;
const prop = this.prop(superMember);
Expand All @@ -206,7 +242,7 @@ const looseHandlers = {
return memberExpression(object, prop, computed);
},

set(superMember, value) {
set(this: Handler & typeof specHandlers, superMember: SuperMember, value) {
const { computed } = superMember.node;
const prop = this.prop(superMember);

Expand All @@ -217,18 +253,25 @@ const looseHandlers = {
);
},

destructureSet(superMember) {
destructureSet(
this: Handler & typeof specHandlers,
superMember: SuperMember,
) {
const { computed } = superMember.node;
const prop = this.prop(superMember);

return memberExpression(thisExpression(), prop, computed);
},

call(superMember, args) {
call(this: Handler & typeof specHandlers, superMember: SuperMember, args) {
return optimiseCall(this.get(superMember), thisExpression(), args, false);
},

optionalCall(superMember, args) {
optionalCall(
this: Handler & typeof specHandlers,
superMember: SuperMember,
args,
) {
return optimiseCall(this.get(superMember), thisExpression(), args, true);
},
};
Expand Down
21 changes: 16 additions & 5 deletions packages/babel-helper-simple-access/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ import {
sequenceExpression,
unaryExpression,
} from "@babel/types";
import type { NodePath } from "@babel/traverse";

import type * as t from "@babel/types";
import type { NodePath, Scope, Visitor } from "@babel/traverse";

type State = {
scope: Scope;
bindingNames: Set<string>;
seen: WeakSet<t.Node>;
includeUpdateExpression: boolean;
};
export default function simplifyAccess(
path: NodePath,
bindingNames,
Expand All @@ -25,7 +32,7 @@ export default function simplifyAccess(
});
}

const simpleAssignmentVisitor = {
const simpleAssignmentVisitor: Visitor<State> = {
// TODO(Babel 8): Remove UpdateExpression
UpdateExpression: {
exit(path) {
Expand Down Expand Up @@ -61,7 +68,7 @@ const simpleAssignmentVisitor = {
"=",
identifier(localName),
binaryExpression(
path.node.operator[0],
path.node.operator[0] as "+" | "-",
unaryExpression("+", arg.node),
numericLiteral(1),
),
Expand All @@ -76,8 +83,9 @@ const simpleAssignmentVisitor = {
path.scope.push({ id: old });

const binary = binaryExpression(
path.node.operator[0],
path.node.operator[0] as "+" | "-",
identifier(varName),
// todo: support bigint
numericLiteral(1),
);

Expand Down Expand Up @@ -126,6 +134,7 @@ const simpleAssignmentVisitor = {
// (foo &&= bar) => (foo && foo = bar)
path.replaceWith(
logicalExpression(
// @ts-expect-error Guarded by LOGICAL_OPERATORS.includes
operator,
path.node.left,
assignmentExpression(
Expand All @@ -138,6 +147,8 @@ const simpleAssignmentVisitor = {
} else {
// (foo += bar) => (foo = foo + bar)
path.node.right = binaryExpression(
// @ts-expect-error An assignment expression operator removing "=" must
// be a valid binary operator
operator,
cloneNode(path.node.left),
path.node.right,
Expand Down
Loading

0 comments on commit 8f904e0

Please sign in to comment.