graphile-export
Advanced tools
Comparing version 0.0.2-beta.10 to 0.0.2-beta.11
# graphile-export | ||
## 0.0.2-beta.11 | ||
### Patch Changes | ||
- [#1958](https://github.com/graphile/crystal/pull/1958) | ||
[`8315e8d01`](https://github.com/graphile/crystal/commit/8315e8d01c118cebc4ebbc53a2f264b958b252ad) | ||
Thanks [@benjie](https://github.com/benjie)! - EXPORTABLE now accepts a third | ||
argument, `nameHint`, which is used to hint what variable name to use for the | ||
given value. Used this in `graphile-export` along with some fixes and | ||
optimizations to improve the exports further. | ||
- [#1946](https://github.com/graphile/crystal/pull/1946) | ||
[`9d53dde72`](https://github.com/graphile/crystal/commit/9d53dde726b7304962e921b88a159649e49156e5) | ||
Thanks [@benjie](https://github.com/benjie)! - Exporting a schema now performs | ||
ESLint 'no-use-before-define' check to catch even more invalid export | ||
conditions. Fix `registerNodeIdCodec` calls caught by this. | ||
- Updated dependencies | ||
[[`9f85c614d`](https://github.com/graphile/crystal/commit/9f85c614d48dc745c5fed15333dbb75af7fddc88), | ||
[`6c6be29f1`](https://github.com/graphile/crystal/commit/6c6be29f12b24782c926b2bc62ed2ede09ac05de), | ||
[`8315e8d01`](https://github.com/graphile/crystal/commit/8315e8d01c118cebc4ebbc53a2f264b958b252ad)]: | ||
- grafast@0.1.1-beta.6 | ||
## 0.0.2-beta.10 | ||
@@ -4,0 +26,0 @@ |
@@ -72,3 +72,6 @@ "use strict"; | ||
function getNameForThing(thing, locationHint, baseNameHint) { | ||
if (typeof thing === "function") { | ||
if (thing.$exporter$name) { | ||
return thing.$exporter$name; | ||
} | ||
else if (typeof thing === "function") { | ||
if (baseNameHint) { | ||
@@ -85,3 +88,6 @@ return baseNameHint; | ||
const thingConstructor = thing.constructor; | ||
const thingConstructorNameRaw = thingConstructor?.name ?? thingConstructor?.displayName ?? null; | ||
const thingConstructorNameRaw = thingConstructor?.$exporter$name ?? | ||
thingConstructor?.name ?? | ||
thingConstructor?.displayName ?? | ||
null; | ||
const thingConstructorName = ["Array", "Object", "Set", "Map"].includes(thingConstructorNameRaw) | ||
@@ -1095,6 +1101,55 @@ ? null | ||
exports.exportSchemaAsString = exportSchemaAsString; | ||
async function exportSchema(schema, toPath, options = {}) { | ||
const { code } = await exportSchemaAsString(schema, options); | ||
const HEADER = `/* eslint-disable graphile-export/export-instances, graphile-export/export-methods, graphile-export/exhaustive-deps */\n`; | ||
const toFormat = HEADER + code; | ||
async function loadESLint() { | ||
try { | ||
return await import("eslint"); | ||
} | ||
catch (e) { | ||
return null; | ||
} | ||
} | ||
async function lint(code, rawFilePath) { | ||
const eslintModule = await loadESLint(); | ||
if (eslintModule == null) { | ||
console.warn(`graphile-export could not find 'eslint' so disabling additional checks`); | ||
return; | ||
} | ||
const { ESLint } = eslintModule; | ||
const eslint = new ESLint({ | ||
useEslintrc: false, | ||
reportUnusedDisableDirectives: "off", | ||
allowInlineConfig: false, | ||
overrideConfig: { | ||
reportUnusedDisableDirectives: false, | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: "module", | ||
}, | ||
rules: { | ||
"no-use-before-define": [ | ||
"error", | ||
{ | ||
functions: false, | ||
classes: false, | ||
// We often have cyclic dependencies between types, this is handled via callbacks, so we don't care about that. | ||
variables: false, | ||
allowNamedExports: false, | ||
}, | ||
], | ||
}, | ||
}, | ||
}); | ||
const filePath = typeof rawFilePath === "string" ? rawFilePath : rawFilePath.pathname; | ||
const results = await eslint.lintText(code, { filePath }); | ||
if (results.length !== 1) { | ||
throw new Error(`Expected ESLint results to have exactly one entry`); | ||
} | ||
const [result] = results; | ||
if (result.errorCount > 0) { | ||
console.log(`ESLint found problems in the export; this likely indicates some issue with \`EXPORTABLE\` calls`); | ||
const formatter = await eslint.loadFormatter("stylish"); | ||
const output = formatter.format(results); | ||
console.log(output); | ||
} | ||
} | ||
async function format(toFormat, toPath, options) { | ||
if (options.prettier) { | ||
@@ -1107,9 +1162,17 @@ const prettier = await import("prettier"); | ||
}); | ||
await (0, promises_1.writeFile)(toPath, formatted); | ||
return formatted; | ||
} | ||
else { | ||
await (0, promises_1.writeFile)(toPath, toFormat); | ||
return toFormat; | ||
} | ||
} | ||
const HEADER = `/* eslint-disable graphile-export/export-instances, graphile-export/export-methods, graphile-export/exhaustive-deps */\n`; | ||
async function exportSchema(schema, toPath, options = {}) { | ||
const { code } = await exportSchemaAsString(schema, options); | ||
const toFormat = HEADER + code; | ||
const formatted = await format(toFormat, toPath, options); | ||
await (0, promises_1.writeFile)(toPath, formatted); | ||
await lint(formatted, toPath); | ||
} | ||
exports.exportSchema = exportSchema; | ||
//# sourceMappingURL=exportSchema.js.map |
@@ -1,2 +0,2 @@ | ||
export declare function EXPORTABLE<T, TScope extends any[]>(factory: (...args: TScope) => T, args: [...TScope]): T; | ||
export declare function EXPORTABLE<T, TScope extends any[]>(factory: (...args: TScope) => T, args: [...TScope], nameHint?: string): T; | ||
//# sourceMappingURL=helpers.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.EXPORTABLE = void 0; | ||
function EXPORTABLE(factory, args) { | ||
function EXPORTABLE(factory, args, nameHint) { | ||
const fn = factory(...args); | ||
@@ -11,2 +11,3 @@ if (((typeof fn === "object" && fn !== null) || typeof fn === "function") && | ||
$exporter$factory: { value: factory }, | ||
$exporter$name: { writable: true, value: nameHint }, | ||
}); | ||
@@ -13,0 +14,0 @@ } |
import * as t from "@babel/types"; | ||
export declare const optimize: (ast: t.Node, runs?: number) => t.Node; | ||
export declare const optimize: (inAst: t.File, runs?: number) => t.File; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -6,2 +6,4 @@ "use strict"; | ||
// import generate from "@babel/generator"; | ||
const generator_1 = tslib_1.__importDefault(require("@babel/generator")); | ||
const parser_1 = require("@babel/parser"); | ||
const traverse_1 = tslib_1.__importStar(require("@babel/traverse")); | ||
@@ -35,3 +37,6 @@ const t = tslib_1.__importStar(require("@babel/types")); | ||
} | ||
const optimize = (ast, runs = 1) => { | ||
const optimize = (inAst, runs = 1) => { | ||
let ast = inAst; | ||
// Reset the full AST | ||
ast = (0, parser_1.parse)((0, generator_1.default)(ast).code, { sourceType: "module" }); | ||
(0, traverse_1.default)(ast, { | ||
@@ -191,3 +196,5 @@ VariableDeclaration: { | ||
}); | ||
ast = (0, parser_1.parse)((0, generator_1.default)(ast).code, { sourceType: "module" }); | ||
// convert `plan: function plan() {...}` to `plan() { ... }` | ||
// convert `fn(...["a", "b"])` to `fn("a", "b")` | ||
(0, traverse_1.default)(ast, { | ||
@@ -224,2 +231,15 @@ ObjectProperty(path) { | ||
}, | ||
CallExpression(path) { | ||
path.node.arguments; | ||
const argsPath = path.get("arguments"); | ||
if (argsPath.length === 1) { | ||
const argPath = argsPath[0]; | ||
if (t.isSpreadElement(argPath.node)) { | ||
const spreadPath = argPath; | ||
if (t.isArrayExpression(spreadPath.node.argument)) { | ||
argPath.replaceWithMultiple(spreadPath.node.argument.elements.filter(isNotNullish)); | ||
} | ||
} | ||
} | ||
}, | ||
}); | ||
@@ -232,2 +252,5 @@ if (runs < 2) { | ||
exports.optimize = optimize; | ||
function isNotNullish(o) { | ||
return o != null; | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "graphile-export", | ||
"version": "0.0.2-beta.10", | ||
"version": "0.0.2-beta.11", | ||
"description": "Export in-memory generated GraphQL schemas to JS files when built with our helpers.", | ||
@@ -56,6 +56,10 @@ "type": "commonjs", | ||
"peerDependencies": { | ||
"grafast": "^0.1.1-beta.5", | ||
"eslint": "^8.48.0", | ||
"grafast": "^0.1.1-beta.6", | ||
"pg-sql2": "^5.0.0-beta.5" | ||
}, | ||
"peerDependenciesMeta": { | ||
"eslint": { | ||
"optional": true | ||
}, | ||
"pg-sql2": { | ||
@@ -62,0 +66,0 @@ "optional": true |
@@ -49,3 +49,3 @@ # graphile-export | ||
`$exporter$factory` and `$exporter$args` that represent the first and second | ||
arguments to the `EXPORTABLE(factory, args)` function respectively. | ||
arguments to the `EXPORTABLE(factory, args, nameHint)` function respectively. | ||
@@ -161,2 +161,3 @@ The function still works as before: | ||
args: [...TScope], | ||
nameHint?: string, | ||
): T { | ||
@@ -171,2 +172,3 @@ const fn: T = factory(...args); | ||
$exporter$factory: { value: factory }, | ||
$exporter$name: { writable: true, value: nameHint }, | ||
}); | ||
@@ -181,3 +183,3 @@ } | ||
```js | ||
export function EXPORTABLE(factory, args) { | ||
export function EXPORTABLE(factory, args, nameHint) { | ||
const fn = factory(...args); | ||
@@ -191,2 +193,3 @@ if ( | ||
$exporter$factory: { value: factory }, | ||
$exporter$name: { writable: true, value: nameHint }, | ||
}); | ||
@@ -193,0 +196,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
155402
1704
204
11