[TS/JS] New gen TS code gen (#6302)

* TS/ES6 modules spike iteration 1

* Initial modularized dasherized output

* Remove obsoleted parts and namespace wrapping

* Use _flatbuffers_ prefix

* First part of imports logic

* Second part of imports logic

* Fix TS/JS code removal mixup

* Alias imported symbols if same name from different namespaces and some fixes

* Use star import for bare imports

* Fix messed up string concat

* var to const and remove not needed semi

* Remove some cases of ns prefixing

* Add missing space

* Cleanups

* Completed initial import tracking logic

* Compilable output

* Adjust TypeScriptTest and dependents to work

* Use local flatbuffers package for tests

* Refactor away use of any

* Remove obsolete imported_fileset and reexport_map

* Still need any and fix JavaScriptTest.sh

* Fix test runs out of the box

* Temp add generated files

* TypeScriptTest replaces JavaScriptTest and cleanups

* Also remove reference to JavaScriptTest in TestAll.sh

* Remove old generated ts/js files

* Remove use of --js in generate_code scripts

* idl_gen_js_ts to idl_gen_ts and removal of js gen

* Remove obsoleted options

* Fix obsolete ts test detection

* Tweak ts compilation be as strict as possible

* Remove jsdoc type annotation generation

* Generated test ts files

* Fix search and replace messup

* Regenerated ts test output

* Use CharToLower

* Use normal for loop

* Rework namespacedir

* Revert "Rework namespacedir"

This reverts commit 6f4eb0104ceeb86011bb076ebca901138c48e068.

* Revert "Use normal for loop"

This reverts commit 676b2135bfaa1853dfbb06c92b5c16a0d81bb13a.

* Revert "Use CharToLower"

This reverts commit 2d08648d0d72d0af201fad80d54cdc76412b35e9.

* Again do rework but correct

* Avoid runtime cast

* Fix test runs

* Also add npm install to get tsc

* Bump node test versions

* for range to std for loop

* Clang format

* Missed one clang format

* Move accessor to later

* Attempt to make windows version of TypeScriptTest

* Want to see the output

* Try to get newer node at appveyor

* Style changes
This commit is contained in:
Björn Harrtell
2021-01-19 21:51:13 +01:00
committed by GitHub
parent 75c859e98f
commit 760c657551
56 changed files with 3870 additions and 4035 deletions

View File

@@ -586,7 +586,7 @@ export class Builder {
*
* @returns offset of obj
*/
createObjectOffset(obj: string | IGeneratedObject): Offset {
createObjectOffset(obj: string | any): Offset {
if(obj === null) {
return 0
}
@@ -603,7 +603,7 @@ export class Builder {
*
* @returns list of offsets of each non null object
*/
createObjectOffsetList(list: string[]): Offset[] {
createObjectOffsetList(list: string[] | any[]): Offset[] {
const ret: number[] = [];
for(let i = 0; i < list.length; ++i) {
@@ -620,7 +620,7 @@ export class Builder {
return ret;
}
createStructOffsetList(list: string[], startFunc: (builder: Builder, length: number) => void): Offset {
createStructOffsetList(list: string[] | any[], startFunc: (builder: Builder, length: number) => void): Offset {
startFunc(this, list.length);
this.createObjectOffsetList(list);
return this.endVector();

View File

@@ -312,8 +312,8 @@ export class ByteBuffer {
/**
* A helper function for generating list for obj api
*/
createScalarList(listAccessor: (i: number) => unknown, listLength: number) : unknown[] {
const ret: unknown[] = [];
createScalarList(listAccessor: (i: number) => unknown, listLength: number): any[] {
const ret: any[] = [];
for(let i = 0; i < listLength; ++i) {
if(listAccessor(i) !== null) {
ret.push(listAccessor(i));
@@ -323,25 +323,18 @@ export class ByteBuffer {
return ret;
}
/**
* This function is here only to get around typescript type system
*/
createStringList(listAccessor: (i: number) => unknown, listLength: number): unknown[] {
return this.createScalarList(listAccessor, listLength);
}
/**
* A helper function for generating list for obj api
* @param listAccessor function that accepts an index and return data at that index
* @param listLength listLength
* @param res result list
*/
createObjList(listAccessor: (i: number) => IGeneratedObject, listLength: number): IGeneratedObject[] {
const ret: IGeneratedObject[] = [];
createObjList(listAccessor: (i: number) => unknown, listLength: number): any[] {
const ret: any[] = [];
for(let i = 0; i < listLength; ++i) {
const val = listAccessor(i);
if(val !== null) {
ret.push(val.unpack());
ret.push((val as IGeneratedObject).unpack());
}
}

View File

@@ -1,34 +1,13 @@
/* eslint-disable @typescript-eslint/no-namespace */
import * as constants from './constants'
import * as types from './types'
import * as utils from './utils'
export { SIZEOF_SHORT } from './constants'
export { SIZEOF_INT } from './constants'
export { FILE_IDENTIFIER_LENGTH } from './constants'
export { SIZE_PREFIX_LENGTH } from './constants'
import { Long as LongClass } from './long'
import { Encoding as EncodingEnum } from './encoding'
import { Builder as BuilderClass } from './builder'
import { ByteBuffer as ByteBufferClass } from './byte-buffer'
export { Table, Offset } from './types'
export namespace flatbuffers {
export { int32, float32, float64, isLittleEndian } from './utils'
export type Offset = types.Offset;
export type Table = types.Table;
export const SIZEOF_SHORT = constants.SIZEOF_SHORT;
export const SIZEOF_INT = constants.SIZEOF_INT;
export const FILE_IDENTIFIER_LENGTH = constants.FILE_IDENTIFIER_LENGTH;
export const SIZE_PREFIX_LENGTH = constants.SIZE_PREFIX_LENGTH;
export const Encoding = EncodingEnum;
export const int32 = utils.int32;
export const float32 = utils.float32;
export const float64 = utils.float64;
export const isLittleEndian = utils.isLittleEndian;
export const Long = LongClass;
export const Builder = BuilderClass;
export const ByteBuffer = ByteBufferClass;
}
export default flatbuffers;
export { Long, createLong } from './long'
export { Encoding } from './encoding'
export { Builder } from './builder'
export { ByteBuffer } from './byte-buffer'

View File

@@ -1,13 +1,14 @@
/* eslint-disable @typescript-eslint/no-namespace */
import { Builder } from './flexbuffers/builder'
import { toReference as toReferenceFunction } from './flexbuffers/reference';
import { toReference } from './flexbuffers/reference'
export { toReference } from './flexbuffers/reference'
export function builder(): Builder {
return new Builder();
}
export function toObject(buffer: Uint8Array): unknown {
return toReferenceFunction(buffer).toObject();
return toReference(buffer).toObject();
}
export function encode(object: unknown, size = 2048, deduplicateStrings = true, deduplicateKeys = true, deduplicateKeyVectors = true): Uint8Array {
@@ -15,16 +16,3 @@ export function encode(object: unknown, size = 2048, deduplicateStrings = true,
builder.add(object);
return builder.finish();
}
const builderFunction = builder
const toObjectFunction = toObject
const encodeFunction = encode
export namespace flexbuffers {
export const builder = builderFunction;
export const toObject = toObjectFunction;
export const encode = encodeFunction;
export const toReference = toReferenceFunction;
}
export default flexbuffers;