microsoft / microsoft/TypeScript

Documentation: "Creating and Printing a TypeScript AST" example does not work after v4.9.5

オープン
#58,247 コメント 0 件 リアクション 2 件 担当者 1 名 GitHub で見る

@rbuckton がすでに取り組んでいます。

2024年4月19日 から。

Docs
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

🔎 Search Terms

"Creating and Printing a TypeScript AST"

🕗 Version & Regression Information

This example from the docs no loner works after v4.9.5

⏯ Playground Link

No response

💻 Code

The code from this section of the docs:

import ts = require("typescript");

function makeFactorialFunction() {
  const functionName = ts.factory.createIdentifier("factorial");
  const paramName = ts.factory.createIdentifier("n");
  const parameter = ts.factory.createParameterDeclaration(
    /*decorators*/ undefined,
    /*modifiers*/ undefined,
    /*dotDotDotToken*/ undefined,
    paramName
  );

  const condition = ts.factory.createBinaryExpression(paramName, ts.SyntaxKind.LessThanEqualsToken, ts.factory.createNumericLiteral(1));
  const ifBody = ts.factory.createBlock([ts.factory.createReturnStatement(ts.factory.createNumericLiteral(1))], /*multiline*/ true);

  const decrementedArg = ts.factory.createBinaryExpression(paramName, ts.SyntaxKind.MinusToken, ts.factory.createNumericLiteral(1));
  const recurse = ts.factory.createBinaryExpression(paramName, ts.SyntaxKind.AsteriskToken, ts.factory.createCallExpression(functionName, /*typeArgs*/ undefined, [decrementedArg]));
  const statements = [ts.factory.createIfStatement(condition, ifBody), ts.factory.createReturnStatement(recurse)];

  return ts.factory.createFunctionDeclaration(
    /*decorators*/ undefined,
    /*modifiers*/ [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)],
    /*asteriskToken*/ undefined,
    functionName,
    /*typeParameters*/ undefined,
    [parameter],
    /*returnType*/ ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
    ts.factory.createBlock(statements, /*multiline*/ true)
  );
}

const resultFile = ts.createSourceFile("someFileName.ts", "", ts.ScriptTarget.Latest, /*setParentNodes*/ false, ts.ScriptKind.TS);
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });

const result = printer.printNode(ts.EmitHint.Unspecified, makeFactorialFunction(), resultFile);
console.log(result);
🙁 Actual behavior
/path/to/playground/node_modules/typescript/lib/typescript.js:110025
      Debug.fail(`Unhandled SyntaxKind: ${Debug.formatSyntaxKind(node.kind)}.`);
            ^

Error: Debug Failure. Unhandled SyntaxKind: Unknown.
    at pipelineEmitWithHintWorker (/path/to/playground/node_modules/typescript/lib/typescript.js:110025:13)
    at pipelineEmitWithHint (/path/to/playground/node_modules/typescript/lib/typescript.js:109566:9)
    at pipelineEmitWithComments (/path/to/playground/node_modules/typescript/lib/typescript.js:113345:7)
    at pipelineEmit (/path/to/playground/node_modules/typescript/lib/typescript.js:109515:7)
    at emit (/path/to/playground/node_modules/typescript/lib/typescript.js:109483:7)
    at emitFunctionDeclarationOrExpression (/path/to/playground/node_modules/typescript/lib/typescript.js:111284:7)
    at emitFunctionDeclaration (/path/to/playground/node_modules/typescript/lib/typescript.js:111274:7)
    at pipelineEmitWithHintWorker (/path/to/playground/node_modules/typescript/lib/typescript.js:109746:20)
    at pipelineEmitWithHint (/path/to/playground/node_modules/typescript/lib/typescript.js:109566:9)
    at pipelineEmitWithComments (/path/to/playground/node_modules/typescript/lib/typescript.js:113345:7)

Node.js v18.18.0
🙂 Expected behavior

i assume that the compiler error is correct due to some kind of breaking change going from 4.9.5 to 5.0.0. so probably the example needs to be updated so that the program successfully runs and produces:

export function factorial(n): number {
    if (n <= 1) {
        return 1;
    }
    return n * factorial(n - 1);
}
Additional information about the issue

Note also that there are some deprecated APIs in the example:

DeprecationWarning: 'createParameterDeclaration' has been deprecated since v4.8.0. Decorators have been combined with modifiers. Callers should switch to an overload that does not accept a 'decorators' parameter.
DeprecationWarning: 'createFunctionDeclaration' has been deprecated since v4.8.0. Decorators are no longer supported for this function. Callers should switch to an overload that does not accept a 'decorators' parameter.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。