Skip to content

Commit

Permalink
rename: registerDocumentFormats --> registerDocumentParsers, format -…
Browse files Browse the repository at this point in the history
…-> parser
  • Loading branch information
chris48s committed Aug 12, 2024
1 parent 2590b5b commit e72e5d1
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion docs/docs/plugins/writing-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/parser.js
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
14 changes: 7 additions & 7 deletions src/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}

Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugins.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
);
});
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/parser-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") ||
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/parser-json5.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/parser-toml.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/parser-yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion testfiles/plugins/invalid-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
}

0 comments on commit e72e5d1

Please sign in to comment.