Skip to content

Commit

Permalink
more renames
Browse files Browse the repository at this point in the history
File --> InputFile
  • Loading branch information
chris48s committed Aug 19, 2024
1 parent 071255b commit deb6622
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 28 deletions.
4 changes: 2 additions & 2 deletions docs/docs/plugins/writing-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ There are two patterns used by v8r plugin hooks.

### Register Hooks

- `registerFileParsers`
- `registerInputFileParsers`
- `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.

### Early Return Hooks

- `parseFile`
- `parseInputFile`
- `getSingleResultLogMessage`
- `getAllResultsLogMessage`

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.registerFileParsers());
documentFormats = documentFormats.concat(plugin.registerInputFileParsers());
}
return documentFormats;
}
Expand Down
4 changes: 2 additions & 2 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { Document } from "./plugins.js";

function parseFile(plugins, contents, filename, parser) {
for (const plugin of plugins) {
const result = plugin.parseFile(contents, filename, parser);
const result = plugin.parseInputFile(contents, filename, parser);
if (result != null) {
if (!(result instanceof Document)) {
throw new Error(
`Plugin ${plugin.constructor.name} returned an unexpcted type from parseFile hook. Expected Document, got ${typeof result}`,
`Plugin ${plugin.constructor.name} returned an unexpected type from parseInputFile hook. Expected Document, got ${typeof result}`,
);
}
return result.document;
Expand Down
19 changes: 10 additions & 9 deletions src/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@ class BasePlugin {
static name = "untitled plugin";

/**
* Use the `registerFileParsers` hook to tell v8r about additional file
* Use the `registerInputFileParsers` hook to tell v8r about additional file
* formats that can be parsed. Any parsers registered with this hook become
* valid values for the `parser` property in custom schemas.
*
* @returns {string[]} File parsers to register
*/
registerFileParsers() {
registerInputFileParsers() {
return [];
}

/**
* Use the `parseFile` hook to tell v8r how to parse files.
* Use the `parseInputFile` hook to tell v8r how to parse files.
*
* If `parseFile` returns anything other than undefined, that return value
* will be used and no further plugins will be invoked. If `parseFile` returns
* undefined, v8r will move on to the next plugin in the stack.
* If `parseInputFile` returns anything other than undefined, that return
* value will be used and no further plugins will be invoked. If
* `parseInputFile` returns undefined, v8r will move on to the next plugin in
* the stack.
*
* @param {string} contents - The unparsed file content.
* @param {string} fileLocation - The file path. Filenames are resolved and
Expand All @@ -40,12 +41,12 @@ class BasePlugin {
* will be prefixed with `./` (or `.\` on Windows) even if this was not
* present in the input filename or pattern.
* @param {string | undefined} parser - If the user has specified a parser to
* use for this file in a custom schema, this will be passed to `parseFile`
* in the `parser` param.
* use for this file in a custom schema, this will be passed to
* `parseInputFile` in the `parser` param.
* @returns {Document | undefined} Parsed file contents
*/
// eslint-disable-next-line no-unused-vars
parseFile(contents, fileLocation, parser) {
parseInputFile(contents, fileLocation, parser) {
return undefined;
}

Expand Down
8 changes: 4 additions & 4 deletions src/plugins.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("loadAllPlugins", function () {
{
name: "Error",
message:
"Error loading plugin v8r-plugin-test-invalid-params: registerFileParsers must take exactly 0 arguments",
"Error loading plugin v8r-plugin-test-invalid-params: registerInputFileParsers must take exactly 0 arguments",
},
);
});
Expand All @@ -85,8 +85,8 @@ describe("resolveUserPlugins", function () {
});
});

describe("parseFile", function () {
it("throws when parseFile returns unexpected type", async function () {
describe("parseInputFile", function () {
it("throws when parseInputFile returns unexpected type", async function () {
const plugins = await loadAllPlugins([
"../testfiles/plugins/bad-parse-method.js",
]);
Expand All @@ -95,7 +95,7 @@ describe("parseFile", function () {
{
name: "Error",
message:
"Plugin v8r-plugin-test-bad-parse-method returned an unexpcted type from parseFile hook. Expected Document, got object",
"Plugin v8r-plugin-test-bad-parse-method returned an unexpected type from parseInputFile hook. Expected Document, got object",
},
);
});
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/parser-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { BasePlugin, Document } from "../plugins.js";
class JsonParser extends BasePlugin {
static name = "v8r-plugin-json-parser";

registerFileParsers() {
registerInputFileParsers() {
return ["json"];
}

parseFile(contents, fileLocation, parser) {
parseInputFile(contents, fileLocation, parser) {
if (parser === "json") {
return new Document(JSON.parse(contents));
} else if (parser == null) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/parser-json5.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { BasePlugin, Document } from "../plugins.js";
class Json5Parser extends BasePlugin {
static name = "v8r-plugin-json5-parser";

registerFileParsers() {
registerInputFileParsers() {
return ["json5"];
}

parseFile(contents, fileLocation, parser) {
parseInputFile(contents, fileLocation, parser) {
if (parser === "json5") {
return new Document(JSON5.parse(contents));
} else if (parser == null) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/parser-toml.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { BasePlugin, Document } from "../plugins.js";
class TomlParser extends BasePlugin {
static name = "v8r-plugin-toml-parser";

registerFileParsers() {
registerInputFileParsers() {
return ["toml"];
}

parseFile(contents, fileLocation, parser) {
parseInputFile(contents, fileLocation, parser) {
if (parser === "toml") {
return new Document(parse(contents));
} else if (parser == null) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/parser-yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { BasePlugin, Document } from "../plugins.js";
class YamlParser extends BasePlugin {
static name = "v8r-plugin-yaml-parser";

registerFileParsers() {
registerInputFileParsers() {
return ["yaml"];
}

parseFile(contents, fileLocation, parser) {
parseInputFile(contents, fileLocation, parser) {
if (parser === "yaml") {
return new Document(yaml.load(contents));
} else if (parser == null) {
Expand Down
2 changes: 1 addition & 1 deletion testfiles/plugins/bad-parse-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default class ValidTestPlugin extends BasePlugin {
static name = "v8r-plugin-test-bad-parse-method";

// eslint-disable-next-line no-unused-vars
parseFile(contents, fileLocation, parser) {
parseInputFile(contents, fileLocation, parser) {
// this method returns something other than a Document object,
// which should cause a failure
return { foo: "bar" };
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
registerFileParsers(foo) {
registerInputFileParsers(foo) {
return [];
}
}

0 comments on commit deb6622

Please sign in to comment.