Skip to content

Commit

Permalink
Remove uses of ts.getMutableClone
Browse files Browse the repository at this point in the history
This function is deprecated and is not currently slated to be replaced.

The new code is much more verbose, but there doesn't appear to be any
real alternative.

See microsoft/TypeScript#40507
  • Loading branch information
edsrzf committed Dec 5, 2020
1 parent 3c4f354 commit 279f656
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 20 deletions.
102 changes: 91 additions & 11 deletions packages/ts-migrate-plugins/src/plugins/jsdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Options = {
const jsDocPlugin: Plugin<Options> = {
name: 'jsdoc',
run({ sourceFile, text, options }) {
const result = ts.transform<ts.SourceFile>(sourceFile, [jsDocTransformerFactory(options)]);
const result = ts.transform(sourceFile, [jsDocTransformerFactory(options)]);
const newSourceFile = result.transformed[0];
if (newSourceFile === sourceFile) {
return text;
Expand All @@ -69,15 +69,20 @@ const jsDocTransformerFactory = ({
: factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword);
const typeMap: TypeMap = { ...defaultTypeMap, ...optionsTypeMap };

function visit<T extends ts.Node>(origNode: T): T {
return (file: ts.SourceFile) => ts.visitNode(file, visit);

function visit(origNode: ts.Node): ts.Node {
const node = ts.visitEachChild(origNode, visit, context);
if (ts.isFunctionLike(node)) {
return visitFunctionLike(node, ts.isClassDeclaration(origNode.parent));
}
return node;
}

function visitFunctionLike<T extends ts.SignatureDeclaration>(node: T, insideClass: boolean): T {
function visitFunctionLike(
node: ts.SignatureDeclaration,
insideClass: boolean,
): ts.SignatureDeclaration {
const modifiers =
ts.isMethodDeclaration(node) && insideClass
? modifiersFromJSDoc(node, factory)
Expand All @@ -92,11 +97,88 @@ const jsDocTransformerFactory = ({
return node;
}

const newNode = ts.getMutableClone(node) as any;
newNode.modifiers = factory.createNodeArray(modifiers);
newNode.parameters = factory.createNodeArray(parameters);
newNode.type = returnType;
return newNode;
const newModifiers = factory.createNodeArray(modifiers);
const newParameters = factory.createNodeArray(parameters);
const newType = returnType;

switch (node.kind) {
case ts.SyntaxKind.FunctionDeclaration:
return factory.updateFunctionDeclaration(
node,
node.decorators,
newModifiers,
node.asteriskToken,
node.name,
node.typeParameters,
newParameters,
newType,
node.body,
);
case ts.SyntaxKind.MethodDeclaration:
return factory.updateMethodDeclaration(
node,
node.decorators,
newModifiers,
node.asteriskToken,
node.name,
node.questionToken,
node.typeParameters,
newParameters,
newType,
node.body,
);
case ts.SyntaxKind.Constructor:
return factory.updateConstructorDeclaration(
node,
node.decorators,
newModifiers,
newParameters,
node.body,
);
case ts.SyntaxKind.GetAccessor:
return factory.updateGetAccessorDeclaration(
node,
node.decorators,
newModifiers,
node.name,
newParameters,
newType,
node.body,
);
case ts.SyntaxKind.SetAccessor:
return factory.updateSetAccessorDeclaration(
node,
node.decorators,
newModifiers,
node.name,
newParameters,
node.body,
);
case ts.SyntaxKind.FunctionExpression:
return factory.updateFunctionExpression(
node,
newModifiers,
node.asteriskToken,
node.name,
node.typeParameters,
newParameters,
newType,
node.body,
);
case ts.SyntaxKind.ArrowFunction:
return factory.updateArrowFunction(
node,
newModifiers,
node.typeParameters,
newParameters,
newType,
node.equalsGreaterThanToken,
node.body,
);
default:
// Should be impossible.
return node;
}
}

function visitParameters(
Expand Down Expand Up @@ -204,7 +286,7 @@ const jsDocTransformerFactory = ({
function visitJSDocNullableType(node: ts.JSDocNullableType) {
return factory.createUnionTypeNode([
ts.visitNode(node.type, visitJSDocType),
factory.createKeywordTypeNode(ts.SyntaxKind.NullKeyword as any),
factory.createLiteralTypeNode(factory.createToken(ts.SyntaxKind.NullKeyword)),
]);
}

Expand Down Expand Up @@ -335,8 +417,6 @@ const jsDocTransformerFactory = ({
ts.setEmitFlags(indexSignature, ts.EmitFlags.SingleLine);
return indexSignature;
}

return visit;
};

const accessibilityMask =
Expand Down
69 changes: 60 additions & 9 deletions packages/ts-migrate-plugins/src/plugins/member-accessibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ type Options = {
const memberAccessibilityPlugin: Plugin<Options> = {
name: 'member-accessibility',
run({ sourceFile, text, options }) {
const result = ts.transform<ts.SourceFile>(sourceFile, [
memberAccessibilityTransformerFactory(options),
]);
const result = ts.transform(sourceFile, [memberAccessibilityTransformerFactory(options)]);
const newSourceFile = result.transformed[0];
if (newSourceFile === sourceFile) {
return text;
Expand Down Expand Up @@ -54,11 +52,11 @@ const memberAccessibilityTransformerFactory = (options: Options) => (

if (defaultAccessibility === 0 && !privateRegex && !protectedRegex && !publicRegex) {
// Nothing to do. Don't bother traversing the AST.
return <T extends ts.Node>(node: T) => node;
return (file: ts.SourceFile) => file;
}
return visit;
return (file: ts.SourceFile) => ts.visitNode(file, visit);

function visit<T extends ts.Node>(origNode: T): T {
function visit(origNode: ts.Node): ts.Node {
const node = ts.visitEachChild(origNode, visit, context);
if (ts.isClassElement(node) && node.name && ts.isIdentifier(node.name)) {
const modifierFlags = ts.getCombinedModifierFlags(node);
Expand All @@ -77,11 +75,64 @@ const memberAccessibilityTransformerFactory = (options: Options) => (
accessibilityFlag = ts.ModifierFlags.Public;
}

const newNode = ts.getMutableClone(node) as any;
newNode.modifiers = factory.createNodeArray(
const modifiers = factory.createNodeArray(
factory.createModifiersFromModifierFlags(modifierFlags | accessibilityFlag),
);
return newNode;
switch (node.kind) {
case ts.SyntaxKind.PropertyDeclaration: {
const propertyNode = node as ts.PropertyDeclaration;
return factory.updatePropertyDeclaration(
propertyNode,
propertyNode.decorators,
modifiers,
propertyNode.name,
propertyNode.questionToken,
propertyNode.type,
propertyNode.initializer,
);
}
case ts.SyntaxKind.MethodDeclaration: {
const methodNode = node as ts.MethodDeclaration;
return factory.updateMethodDeclaration(
methodNode,
methodNode.decorators,
modifiers,
methodNode.asteriskToken,
methodNode.name,
methodNode.questionToken,
methodNode.typeParameters,
methodNode.parameters,
methodNode.type,
methodNode.body,
);
}
case ts.SyntaxKind.GetAccessor: {
const accessorNode = node as ts.GetAccessorDeclaration;
return factory.updateGetAccessorDeclaration(
accessorNode,
accessorNode.decorators,
modifiers,
accessorNode.name,
accessorNode.parameters,
accessorNode.type,
accessorNode.body,
);
}
case ts.SyntaxKind.SetAccessor: {
const accessorNode = node as ts.SetAccessorDeclaration;
return factory.updateSetAccessorDeclaration(
accessorNode,
accessorNode.decorators,
modifiers,
accessorNode.name,
accessorNode.parameters,
accessorNode.body,
);
}
default:
// Should be impossible.
return node;
}
}
return node;
}
Expand Down

0 comments on commit 279f656

Please sign in to comment.