Skip to content

Commit

Permalink
Adapt @gi.ts/lib codebase to ts-for-gir coding conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
ewlsh committed Nov 4, 2023
1 parent 74a1e97 commit 68b651e
Show file tree
Hide file tree
Showing 47 changed files with 8,859 additions and 8,704 deletions.
26 changes: 26 additions & 0 deletions packages/lib/src/newlib/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -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
}
}
9 changes: 9 additions & 0 deletions packages/lib/src/newlib/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"semi": true,
"trailingComma": "none",
"arrowParens": "avoid",
"singleQuote": false,
"printWidth": 120,
"tabWidth": 4
}

6 changes: 3 additions & 3 deletions packages/lib/src/newlib/formatters/default.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Formatter } from "./formatter.js";

export class DefaultFormatter extends Formatter {
format(source: string): string {
return source;
}
format(source: string): string {
return source;
}
}
2 changes: 1 addition & 1 deletion packages/lib/src/newlib/formatters/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export abstract class Formatter {
abstract format(source: string): string;
abstract format(source: string): string;
}
6 changes: 3 additions & 3 deletions packages/lib/src/newlib/formatters/json.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Formatter } from "./formatter.js";

export class JSONFormatter extends Formatter {
format(source: string): string {
return JSON.stringify(JSON.parse(source), null, 4);
}
format(source: string): string {
return JSON.stringify(JSON.parse(source), null, 4);
}
}
116 changes: 58 additions & 58 deletions packages/lib/src/newlib/generators/dts-inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,79 +9,79 @@ import { override as overrideGio } from "./dts/gio.js";
import { DtsGenerator } from "./dts.js";

export class DtsInlineGenerator extends DtsGenerator {
constructor(namespace: IntrospectedNamespace, options: GenerationOptions) {
super(namespace, options);
}
constructor(namespace: IntrospectedNamespace, options: GenerationOptions) {
super(namespace, options);
}

async generateNamespace(node: IntrospectedNamespace): Promise<string | null> {
const { namespace, options } = this;
generateNamespace(node: IntrospectedNamespace): Promise<string | null> {
const { namespace, options } = this;

if (options.verbose) {
console.debug(`Resolving the types of ${namespace.name}...`);
}
if (options.verbose) {
console.debug(`Resolving the types of ${namespace.name}...`);
}

let suffix = "";
let suffix = "";

if (!options.noAdvancedVariants && node.name === "GLib") {
suffix = `\n${overrideGLib(node)}\n`;
} else if (node.name === "GObject") {
suffix = `\n${overrideGObject(node)}\n`;
} else if (node.name === "Gio") {
suffix = `\n${overrideGio(node)}\n`;
}
if (!options.noAdvancedVariants && node.name === "GLib") {
suffix = `\n${overrideGLib(node)}\n`;
} else if (node.name === "GObject") {
suffix = `\n${overrideGObject(node)}\n`;
} else if (node.name === "Gio") {
suffix = `\n${overrideGio(node)}\n`;
}

try {
const { name } = node;
try {
const { name } = node;

const header = `
const header = `
/**
* ${name} ${node.version}
*
* Generated from ${node.package_version.join(".")}
*/
`;
const base = `
const base = `
`;

if (options.promisify) {
promisifyNamespaceFunctions(node);
}

const content = Array.from(node.members.values())
.map(m => {
return `${(Array.isArray(m) ? m : [m])
.map(m => (m.emit ? (m as GirBase).asString(this) : ""))
.join(`\n`)}`;
})
.join(`\n`);

// Resolve imports after we stringify everything else, sometimes we have to ad-hoc add an import.
const imports = Array.from(node.getImports())
.map(
([i, version]) =>
`import * as ${i} from "${options.importPrefix}${i.toLowerCase()}${
options.versionedImports ? version.toLowerCase().split(".")[0] : ""
}";`
)
.join(`${`\n`}`);

const output = [header, imports, base, content, suffix].join(`\n\n`);

if (options.verbose) {
console.debug(`Printing ${namespace.name}...`);
}

return output;
} catch (err) {
console.error(`Failed to generate namespace: ${node.name}`);
console.error(err);

return null;
if (options.promisify) {
promisifyNamespaceFunctions(node);
}

const content = Array.from(node.members.values())
.map(m => {
return `${(Array.isArray(m) ? m : [m])
.map(m => (m.emit ? (m as GirBase).asString(this) : ""))
.join("\n")}`;
})
.join("\n");

// Resolve imports after we stringify everything else, sometimes we have to ad-hoc add an import.
const imports = Array.from(node.getImports())
.map(
([i, version]) =>
`import * as ${i} from "${options.importPrefix}${i.toLowerCase()}${
options.versionedImports ? version.toLowerCase().split(".")[0] : ""
}";`
)
.join(`${"\n"}`);

const output = [header, imports, base, content, suffix].join("\n\n");

if (options.verbose) {
console.debug(`Printing ${namespace.name}...`);
}

return Promise.resolve(output);
} catch (err) {
console.error(`Failed to generate namespace: ${node.name}`);
console.error(err);

return Promise.resolve(null);
}
}
}

async stringifyNamespace(node: IntrospectedNamespace): Promise<string | null> {
return this.generateNamespace(node);
}
async stringifyNamespace(node: IntrospectedNamespace): Promise<string | null> {
return this.generateNamespace(node);
}
}
Loading

0 comments on commit 68b651e

Please sign in to comment.