Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't transform declare class in plugin-proposal-class-properties #13854

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -98,6 +98,11 @@ export function createClassFeaturePlugin({

verifyUsedFeatures(path, this.file);

if ((path.node as t.ClassDeclaration).declare) {
// TypeScript ambient declaration; dont transform
return;
}
forivall marked this conversation as resolved.
Show resolved Hide resolved

const loose = isLoose(this.file, feature);

let constructor: NodePath<t.ClassMethod>;
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-plugin-transform-typescript/src/const-enum.ts
Expand Up @@ -3,8 +3,9 @@ import type { NodePath } from "@babel/traverse";

import { translateEnumValues } from "./enum";

export type NodePathConstEnum = NodePath<t.TSEnumDeclaration & { const: true }>;
export default function transpileConstEnum(
path: NodePath<t.TSEnumDeclaration & { const: true }>,
path: NodePathConstEnum,
t: typeof import("@babel/types"),
) {
const { name } = path.node.id;
Expand Down
56 changes: 45 additions & 11 deletions packages/babel-plugin-transform-typescript/src/index.ts
@@ -1,12 +1,13 @@
import { declare } from "@babel/helper-plugin-utils";
import syntaxTypeScript from "@babel/plugin-syntax-typescript";
import { types as t, template } from "@babel/core";
import type { File as BabelFile } from "@babel/core";
import { injectInitialization } from "@babel/helper-create-class-features-plugin";

import transpileConstEnum from "./const-enum";
import transpileConstEnum, { NodePathConstEnum } from "./const-enum";
forivall marked this conversation as resolved.
Show resolved Hide resolved
import transpileEnum from "./enum";
import transpileNamespace from "./namespace";
import type { NodePath } from "@babel/traverse";
import type { NodePath, Visitor } from "@babel/traverse";

function isInType(path) {
switch (path.parent.type) {
Expand Down Expand Up @@ -50,8 +51,31 @@ function isGlobalType(path, name) {
function registerGlobalType(programNode, name) {
GLOBAL_TYPES.get(programNode).add(name);
}

export default declare((api, opts) => {
export interface Options {
/** @default true */
allowNamespaces?: boolean;
/** @default "React.createElement" */
jsxPragma?: string;
/** @default "React.Fragment" */
jsxPragmaFrag?: string;
onlyRemoveTypeImports?: boolean;
optimizeConstEnums?: boolean;
allowDeclareFields?: boolean;
}
type ConfigAPI = { assertVersion: (range: string | number) => void };
interface Plugin {
name: string;
visitor: Visitor<{ file: BabelFile }>;
forivall marked this conversation as resolved.
Show resolved Hide resolved
inherits: typeof syntaxTypeScript;
}
type ExtraNodeProps = {
declare?: unknown;
accessibility?: unknown;
abstract?: unknown;
optional?: unknown;
override?: unknown;
};
export default declare((api: ConfigAPI, opts: Options): Plugin => {
api.assertVersion(7);

const JSX_PRAGMA_REGEX = /\*?\s*@jsx((?:Frag)?)\s+([^\s]+)/;
Expand All @@ -70,7 +94,11 @@ export default declare((api, opts) => {
}

const classMemberVisitors = {
field(path) {
field(
path: NodePath<
(t.ClassPrivateProperty | t.ClassProperty) & ExtraNodeProps
>,
) {
const { node } = path;

if (!process.env.BABEL_8_BREAKING) {
Expand Down Expand Up @@ -302,7 +330,10 @@ export default declare((api, opts) => {
(stmt.isTSModuleDeclaration({ declare: true }) &&
stmt.get("id").isIdentifier())
) {
registerGlobalType(programNode, stmt.node.id.name);
registerGlobalType(
programNode,
(stmt.node.id as t.Identifier).name,
forivall marked this conversation as resolved.
Show resolved Hide resolved
);
}
}
},
Expand Down Expand Up @@ -336,10 +367,11 @@ export default declare((api, opts) => {
// Also, currently @babel/parser sets exportKind to "value" for
// export interface A {}
// etc.
type S = typeof path.node.specifiers[number] & { local?: t.Identifier };
if (
!path.node.source &&
path.node.specifiers.length > 0 &&
path.node.specifiers.every(({ local }) =>
path.node.specifiers.every(({ local }: S) =>
isGlobalType(path, local.name),
forivall marked this conversation as resolved.
Show resolved Hide resolved
)
) {
Expand All @@ -352,7 +384,9 @@ export default declare((api, opts) => {

ExportSpecifier(path) {
// remove type exports
if (!path.parent.source && isGlobalType(path, path.node.local.name)) {
type Parent = t.ExportDeclaration & { source?: t.StringLiteral };
const parent = path.parent as Parent;
if (!parent.source && isGlobalType(path, path.node.local.name)) {
path.remove();
}
},
Expand Down Expand Up @@ -406,7 +440,7 @@ export default declare((api, opts) => {
},

Class(path) {
const { node } = path;
const { node }: { node: typeof path.node & ExtraNodeProps } = path;

if (node.typeParameters) node.typeParameters = null;
if (node.superTypeParameters) node.superTypeParameters = null;
Expand Down Expand Up @@ -469,7 +503,7 @@ export default declare((api, opts) => {

TSEnumDeclaration(path) {
if (optimizeConstEnums && path.node.const) {
transpileConstEnum(path, t);
transpileConstEnum(path as NodePathConstEnum, t);
forivall marked this conversation as resolved.
Show resolved Hide resolved
} else {
transpileEnum(path, t);
}
Expand Down Expand Up @@ -510,7 +544,7 @@ export default declare((api, opts) => {
},

TSAsExpression(path) {
let { node } = path;
let { node }: { node: t.Expression } = path;
forivall marked this conversation as resolved.
Show resolved Hide resolved
do {
node = node.expression;
} while (t.isTSAsExpression(node));
Expand Down