From e72e5d102f2ee157d05c908f8e13d8c8a55040ee Mon Sep 17 00:00:00 2001 From: chris48s Date: Mon, 12 Aug 2024 08:04:54 +0100 Subject: [PATCH] rename: registerDocumentFormats --> registerDocumentParsers, format --> parser --- docs/docs/plugins/writing-plugins.md | 2 +- src/bootstrap.js | 2 +- src/parser.js | 8 ++++---- src/plugins.js | 14 +++++++------- src/plugins.spec.js | 2 +- src/plugins/parser-json.js | 8 ++++---- src/plugins/parser-json5.js | 8 ++++---- src/plugins/parser-toml.js | 8 ++++---- src/plugins/parser-yaml.js | 8 ++++---- testfiles/plugins/invalid-params.js | 2 +- 10 files changed, 31 insertions(+), 31 deletions(-) diff --git a/docs/docs/plugins/writing-plugins.md b/docs/docs/plugins/writing-plugins.md index 98d2810..63a8654 100644 --- a/docs/docs/plugins/writing-plugins.md +++ b/docs/docs/plugins/writing-plugins.md @@ -22,7 +22,7 @@ There are two patterns used by v8r plugin hooks. ### Register Hooks -- `registerDocumentFormats` +- `registerDocumentParsers` - `registerOutputFormats` These hooks return an array of strings. Any values returned by these hooks are added to the list of formats v8r can work with. diff --git a/src/bootstrap.js b/src/bootstrap.js index 4317b2c..760f3c7 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -158,7 +158,7 @@ function parseArgs(argv, config, documentFormats, outputFormats) { function getDocumentFormats(loadedPlugins) { let documentFormats = []; for (const plugin of loadedPlugins) { - documentFormats = documentFormats.concat(plugin.registerDocumentFormats()); + documentFormats = documentFormats.concat(plugin.registerDocumentParsers()); } return documentFormats; } diff --git a/src/parser.js b/src/parser.js index 949142e..0b2a9c9 100644 --- a/src/parser.js +++ b/src/parser.js @@ -1,16 +1,16 @@ import path from "path"; import yaml from "js-yaml"; -function parseDocument(plugins, contents, filename, format) { +function parseDocument(plugins, contents, filename, parser) { for (const plugin of plugins) { - const result = plugin.parseDocument(contents, filename, format); + const result = plugin.parseDocument(contents, filename, parser); if (result != null) { return result; } } - const errorMessage = format - ? `Unsupported format ${format}` + const errorMessage = parser + ? `Unsupported format ${parser}` : `Unsupported format ${path.extname(filename).slice(1)}`; throw new Error(errorMessage); } diff --git a/src/plugins.js b/src/plugins.js index f461db0..bfb0a38 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -16,13 +16,13 @@ class BasePlugin { static name = "untitled plugin"; /** - * Use the `registerDocumentFormats` hook to tell v8r about additional - * document formats that can be parsed. Any formats registered with this hook + * Use the `registerDocumentParsers` hook to tell v8r about additional + * document formats that can be parsed. Any parsers registered with this hook * become valid values for the `parser` property in custom schemas. * - * @returns {string[]} Document formats to register + * @returns {string[]} Document parsers to register */ - registerDocumentFormats() { + registerDocumentParsers() { return []; } @@ -35,13 +35,13 @@ class BasePlugin { * * @param {string} contents - The unparsed document file content. * @param {string} filename - The document filename. - * @param {string | undefined} format - If this filename matched a document + * @param {string | undefined} parser - If this filename matched a document * parser the user has specified in a custom schema, this will be passed to - * `parseDocument` in the `format` param. + * `parseDocument` in the `parser` param. * @returns {object | undefined} Parsed file contents */ // eslint-disable-next-line no-unused-vars - parseDocument(contents, filename, format) { + parseDocument(contents, filename, parser) { return undefined; } diff --git a/src/plugins.spec.js b/src/plugins.spec.js index 7dbe2b0..d7a0a8e 100644 --- a/src/plugins.spec.js +++ b/src/plugins.spec.js @@ -62,7 +62,7 @@ describe("loadAllPlugins", function () { { name: "Error", message: - "Error loading plugin v8r-plugin-test-invalid-params: registerDocumentFormats must take exactly 1 arguments", + "Error loading plugin v8r-plugin-test-invalid-params: registerDocumentParsers must take exactly 1 arguments", }, ); }); diff --git a/src/plugins/parser-json.js b/src/plugins/parser-json.js index f49d63b..5fdea96 100644 --- a/src/plugins/parser-json.js +++ b/src/plugins/parser-json.js @@ -3,14 +3,14 @@ import { BasePlugin } from "../plugins.js"; class JsonParser extends BasePlugin { static name = "v8r-plugin-json-parser"; - registerDocumentFormats() { + registerDocumentParsers() { return ["json"]; } - parseDocument(contents, filename, format) { - if (format === "json") { + parseDocument(contents, filename, parser) { + if (parser === "json") { return JSON.parse(contents); - } else if (format == null) { + } else if (parser == null) { if ( filename.endsWith(".json") || filename.endsWith(".geojson") || diff --git a/src/plugins/parser-json5.js b/src/plugins/parser-json5.js index 7c963b3..3649301 100644 --- a/src/plugins/parser-json5.js +++ b/src/plugins/parser-json5.js @@ -4,14 +4,14 @@ import { BasePlugin } from "../plugins.js"; class Json5Parser extends BasePlugin { static name = "v8r-plugin-json5-parser"; - registerDocumentFormats() { + registerDocumentParsers() { return ["json5"]; } - parseDocument(contents, filename, format) { - if (format === "json5") { + parseDocument(contents, filename, parser) { + if (parser === "json5") { return JSON5.parse(contents); - } else if (format == null) { + } else if (parser == null) { if (filename.endsWith(".json5") || filename.endsWith(".jsonc")) { return JSON5.parse(contents); } diff --git a/src/plugins/parser-toml.js b/src/plugins/parser-toml.js index b09f89e..f7c61e6 100644 --- a/src/plugins/parser-toml.js +++ b/src/plugins/parser-toml.js @@ -4,14 +4,14 @@ import { BasePlugin } from "../plugins.js"; class TomlParser extends BasePlugin { static name = "v8r-plugin-toml-parser"; - registerDocumentFormats() { + registerDocumentParsers() { return ["toml"]; } - parseDocument(contents, filename, format) { - if (format === "toml") { + parseDocument(contents, filename, parser) { + if (parser === "toml") { return parse(contents); - } else if (format == null) { + } else if (parser == null) { if (filename.endsWith(".toml")) { return parse(contents); } diff --git a/src/plugins/parser-yaml.js b/src/plugins/parser-yaml.js index f758916..e666a52 100644 --- a/src/plugins/parser-yaml.js +++ b/src/plugins/parser-yaml.js @@ -4,14 +4,14 @@ import { BasePlugin } from "../plugins.js"; class YamlParser extends BasePlugin { static name = "v8r-plugin-yaml-parser"; - registerDocumentFormats() { + registerDocumentParsers() { return ["yaml"]; } - parseDocument(contents, filename, format) { - if (format === "yaml") { + parseDocument(contents, filename, parser) { + if (parser === "yaml") { return yaml.load(contents); - } else if (format == null) { + } else if (parser == null) { if (filename.endsWith(".yaml") || filename.endsWith(".yml")) { return yaml.load(contents); } diff --git a/testfiles/plugins/invalid-params.js b/testfiles/plugins/invalid-params.js index 20c78d7..cde99cd 100644 --- a/testfiles/plugins/invalid-params.js +++ b/testfiles/plugins/invalid-params.js @@ -4,7 +4,7 @@ export default class InvalidParamsTestPlugin extends BasePlugin { static name = "v8r-plugin-test-invalid-params"; // eslint-disable-next-line no-unused-vars - registerDocumentFormats(foo) { + registerDocumentParsers(foo) { return []; } }