microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101k stars 12.48k forks source link

TypeScript 5.1: ts.createLiteral is not a function #53077

Closed johnnyreilly closed 1 year ago

johnnyreilly commented 1 year ago

Bug Report

πŸ”Ž Search Terms

ts.createLiteral is not a function

πŸ•— Version & Regression Information

I noticed that the typescript@next test packs for ts-loader were failing with the esoteric error ts.createLiteral is not a function. I'm baffled as to why - so I thought I'd ask!

https://github.com/TypeStrong/ts-loader/actions/runs/4321988253/jobs/7543877954

image

πŸ’» Code

// We can quickly address your report if:
//  - The code sample is short. Nearly all TypeScript bugs can be demonstrated in 20-30 lines of code!
//  - It doesn't use external libraries. These are often issues with the type definitions rather than TypeScript bugs.
//  - The incorrectness of the behavior is readily apparent from reading the sample.
// Reports are slower to investigate if:
//  - We have to pare too much extraneous code.
//  - We have to clone a large repo and validate that the problem isn't elsewhere.
//  - The sample is confusing or doesn't clearly demonstrate what's wrong.

https://github.com/TypeStrong/ts-loader/blob/9aa5b521a7647fdbd575bb76daa94183e3286604/test/execution-tests/loaderOptions/uppercaseStringLiteralTransformer.js#L10

"use strict";
exports.__esModule = true;
var ts = require("typescript");
var transformer = function (context) {
    var visitor = function (node) {
        if (node.kind === ts.SyntaxKind.StringLiteral) {
            var text = node.text;
            if (text.match(/^Hello from submodule/) !== null) {
                if (text !== text.toUpperCase()) {
                    return ts.createLiteral(text.toUpperCase());
                }
            }
        }
        return ts.visitEachChild(node, visitor, context);
    };
    return function (node) { return ts.visitNode(node, visitor); };
};
exports["default"] = transformer;

The tests make use of this API - ts-loader itself does not.

πŸ™ Actual behavior

Tests that usually pass now fail

πŸ™‚ Expected behavior

Tests that usually pass still pass

RyanCavanaugh commented 1 year ago

This function has been marked deprecated since 4.1 (possibly earlier?)

https://github.com/microsoft/TypeScript/pull/52314/files#diff-c1d25c0f0add39e7c2e113b07a6214aacfd9538c89e0f8d9d055301c8d47eae6L1029

    /** @deprecated Use `factory.createStringLiteral`, `factory.createStringLiteralFromNode`, `factory.createNumericLiteral`, `factory.createBigIntLiteral`, `factory.createTrue`, `factory.createFalse`, or the factory supplied by your transformation context instead. */
    const createLiteral: {
johnnyreilly commented 1 year ago

Wowser! Thanks