[TS] Fix relative import paths of generated TypeScript code (#8880)

* Refactor logic that generates import paths in AddImport

* Add new tests to validate relative import path fix

* Generate goldens

* Generate example code

* Format TS generator file

* Revert "Format TS generator file"

This reverts commit 0f0b24aee9.

* Fix merge conflicts

---------

Co-authored-by: Björn Harrtell <bjornharrtell@users.noreply.github.com>
This commit is contained in:
Austin Chick
2026-02-17 03:22:32 -06:00
committed by GitHub
parent d71c0ab4ac
commit b8e3d215b8
15 changed files with 177 additions and 58 deletions

View File

@@ -0,0 +1,28 @@
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, resolve } from "node:path";
const here = dirname(fileURLToPath(import.meta.url));
const headerPath = resolve(here, "relative_imports/transit/three/header.ts");
const contents = readFileSync(headerPath, "utf8");
const expectedImports = [
"from '../one/info.js';",
"from '../two/identity.js';",
];
for (const expected of expectedImports) {
if (!contents.includes(expected)) {
throw new Error(`Missing relative import "${expected}" in ${headerPath}`);
}
}
const forbidden = "../transit/";
if (contents.includes(forbidden)) {
throw new Error(
`Found unexpected namespace segment in import path within ${headerPath}`
);
}
console.log("JavaScriptRelativeImportPathTest: OK");

View File

@@ -199,6 +199,20 @@ flatc(
flatc(options=["--ts"], schema="../long_namespace.fbs")
flatc(options=["--ts"], schema="../longer_namespace.fbs")
flatc(
options=[
"--ts",
"--reflect-names",
"--gen-name-strings",
"--gen-object-api",
"--ts-entry-points",
"--ts-flat-files",
],
schema="relative_imports/relative_imports.fbs",
prefix="relative_imports",
)
print("Running TypeScript Compiler...")
check_call(["tsc"])
print(
@@ -215,6 +229,7 @@ check_call(NODE_CMD + ["JavaScriptUnionVectorTest"])
check_call(NODE_CMD + ["JavaScriptFlexBuffersTest"])
check_call(NODE_CMD + ["JavaScriptComplexArraysTest"])
check_call(NODE_CMD + ["JavaScriptUnionUnderlyingTypeTest"])
check_call(NODE_CMD + ["JavaScriptRelativeImportPathTest"])
check_call(NODE_CMD + ["JavaScriptUndefinedForOptionals"])
print("Running old v1 TypeScript Tests...")

View File

@@ -2,7 +2,7 @@
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
import { Monster, MonsterT } from '../../my-game/example/monster.js';
import { Monster, MonsterT } from './monster.js';
export enum AnyAmbiguousAliases {

View File

@@ -2,9 +2,9 @@
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
import { Monster as MyGame_Example2_Monster, MonsterT as MyGame_Example2_MonsterT } from '../../my-game/example2/monster.js';
import { Monster, MonsterT } from '../../my-game/example/monster.js';
import { TestSimpleTableWithEnum, TestSimpleTableWithEnumT } from '../../my-game/example/test-simple-table-with-enum.js';
import { Monster as MyGame_Example2_Monster, MonsterT as MyGame_Example2_MonsterT } from '../example2/monster.js';
import { Monster, MonsterT } from './monster.js';
import { TestSimpleTableWithEnum, TestSimpleTableWithEnumT } from './test-simple-table-with-enum.js';
export enum AnyUniqueAliases {

View File

@@ -2,9 +2,9 @@
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
import { Monster as MyGame_Example2_Monster, MonsterT as MyGame_Example2_MonsterT } from '../../my-game/example2/monster.js';
import { Monster, MonsterT } from '../../my-game/example/monster.js';
import { TestSimpleTableWithEnum, TestSimpleTableWithEnumT } from '../../my-game/example/test-simple-table-with-enum.js';
import { Monster as MyGame_Example2_Monster, MonsterT as MyGame_Example2_MonsterT } from '../example2/monster.js';
import { Monster, MonsterT } from './monster.js';
import { TestSimpleTableWithEnum, TestSimpleTableWithEnumT } from './test-simple-table-with-enum.js';
export enum Any {

View File

@@ -4,19 +4,19 @@
import * as flatbuffers from 'flatbuffers';
import { Monster as MyGame_Example2_Monster, MonsterT as MyGame_Example2_MonsterT } from '../../my-game/example2/monster.js';
import { Ability, AbilityT } from '../../my-game/example/ability.js';
import { Any, unionToAny, unionListToAny } from '../../my-game/example/any.js';
import { AnyAmbiguousAliases, unionToAnyAmbiguousAliases, unionListToAnyAmbiguousAliases } from '../../my-game/example/any-ambiguous-aliases.js';
import { AnyUniqueAliases, unionToAnyUniqueAliases, unionListToAnyUniqueAliases } from '../../my-game/example/any-unique-aliases.js';
import { Color } from '../../my-game/example/color.js';
import { Race } from '../../my-game/example/race.js';
import { Referrable, ReferrableT } from '../../my-game/example/referrable.js';
import { Stat, StatT } from '../../my-game/example/stat.js';
import { Test, TestT } from '../../my-game/example/test.js';
import { TestSimpleTableWithEnum, TestSimpleTableWithEnumT } from '../../my-game/example/test-simple-table-with-enum.js';
import { Vec3, Vec3T } from '../../my-game/example/vec3.js';
import { InParentNamespace, InParentNamespaceT } from '../../my-game/in-parent-namespace.js';
import { Monster as MyGame_Example2_Monster, MonsterT as MyGame_Example2_MonsterT } from '../example2/monster.js';
import { Ability, AbilityT } from './ability.js';
import { Any, unionToAny, unionListToAny } from './any.js';
import { AnyAmbiguousAliases, unionToAnyAmbiguousAliases, unionListToAnyAmbiguousAliases } from './any-ambiguous-aliases.js';
import { AnyUniqueAliases, unionToAnyUniqueAliases, unionListToAnyUniqueAliases } from './any-unique-aliases.js';
import { Color } from './color.js';
import { Race } from './race.js';
import { Referrable, ReferrableT } from './referrable.js';
import { Stat, StatT } from './stat.js';
import { Test, TestT } from './test.js';
import { TestSimpleTableWithEnum, TestSimpleTableWithEnumT } from './test-simple-table-with-enum.js';
import { Vec3, Vec3T } from './vec3.js';
import { InParentNamespace, InParentNamespaceT } from '../in-parent-namespace.js';
/**

View File

@@ -4,7 +4,7 @@
import * as flatbuffers from 'flatbuffers';
import { StructOfStructs, StructOfStructsT } from '../../my-game/example/struct-of-structs.js';
import { StructOfStructs, StructOfStructsT } from './struct-of-structs.js';
export class StructOfStructsOfStructs implements flatbuffers.IUnpackableObject<StructOfStructsOfStructsT> {

View File

@@ -4,8 +4,8 @@
import * as flatbuffers from 'flatbuffers';
import { Ability, AbilityT } from '../../my-game/example/ability.js';
import { Test, TestT } from '../../my-game/example/test.js';
import { Ability, AbilityT } from './ability.js';
import { Test, TestT } from './test.js';
export class StructOfStructs implements flatbuffers.IUnpackableObject<StructOfStructsT> {

View File

@@ -4,7 +4,7 @@
import * as flatbuffers from 'flatbuffers';
import { Color } from '../../my-game/example/color.js';
import { Color } from './color.js';
export class TestSimpleTableWithEnum implements flatbuffers.IUnpackableObject<TestSimpleTableWithEnumT> {

View File

@@ -4,8 +4,8 @@
import * as flatbuffers from 'flatbuffers';
import { Color } from '../../my-game/example/color.js';
import { Test, TestT } from '../../my-game/example/test.js';
import { Color } from './color.js';
import { Test, TestT } from './test.js';
export class Vec3 implements flatbuffers.IUnpackableObject<Vec3T> {

View File

@@ -4,7 +4,7 @@
import * as flatbuffers from 'flatbuffers';
import { OptionalByte } from '../optional-scalars/optional-byte.js';
import { OptionalByte } from './optional-byte.js';
export class ScalarStuff {

View File

@@ -0,0 +1,20 @@
namespace Transit.One;
table Info {
timestamp:ulong;
}
namespace Transit.Two;
table Identity {
id:uint;
}
namespace Transit.Three;
table Header {
info:Transit.One.Info;
id:Transit.Two.Identity;
}
root_type Header;

View File

@@ -1,7 +1,10 @@
{
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020", "DOM"],
"lib": [
"ES2020",
"DOM"
],
"module": "NodeNext",
"declaration": true,
"strict": true
@@ -17,6 +20,7 @@
"arrays_test_complex/**/*.ts",
"union_underlying_type_test.ts",
"long-namespace/**/*.ts",
"longer-namespace/**/*.ts"
"longer-namespace/**/*.ts",
"relative_imports/**/*.ts"
]
}
}