nodejs-testing icon indicating copy to clipboard operation
nodejs-testing copied to clipboard

Typescript "as const" may break tree parsing

Open yleflour opened this issue 1 year ago • 4 comments

I encountered a weird bug:

import assert from "node:assert/strict";
import { describe, it } from "node:test";

describe("A", () => {
  describe("A.1", () => {
    it("works", async () => {
      assert.equal(1, 1);
    });

    it("breaks the tree", async () => {
      const plop = `Hello ${{ type: "extra" } as const} World`;
      assert.equal(1, 1);
    });
  });

  describe("A.2", () => {
    it("doesn't show", async () => {
      assert.equal(1, 1);
    });
  });
});

Produces the following tree: image

The issue comes from ${{ type: "extra" } as const} and the following rewrites work:

  • Using${{ type: "extra" as const }}
  • Declaring const inlineObj = { type: "extra" } as const and ${inlineObj}

yleflour avatar Aug 14 '24 22:08 yleflour

That shouldn't affect things because this repo parses JS only, not the TypeScript sources. Are you able to toss your example setup in a repo so I can see your compiler settings and such?

connor4312 avatar Aug 15 '24 00:08 connor4312

Just got a minimal sample repo set up here: https://github.com/yleflour/nodejs-testing-demo I'll check out the built JS. if I have time

yleflour avatar Aug 21 '24 20:08 yleflour

FYI Adding an "outDir": "./dist" and setting "emitDeclarationOnly": false in "tsconfig.json" fixes the issue as the test runner uses the .js files from the "./dist" folder Only seem to break with on the fly transpilation

yleflour avatar Aug 21 '24 20:08 yleflour

This is because we try to still parse the source naively with acorn in the transpilation case. With acorn-loose it's often good enough but it seems like we get tripped up by this case.

While TS is technically not the only language that could transpile and get turned into JS, it's by far the most common one, so let's just ship a transpiler to deal with it properly.

connor4312 avatar Oct 01 '24 04:10 connor4312

Fixed in https://github.com/connor4312/nodejs-testing/commit/08b5a2f4b6e64b9c1fe3dbd60628774cdde57e40, we will now use VS Code's TS language features to do the parsing

connor4312 avatar Dec 08 '24 01:12 connor4312