From a22ad12d6a3af7c67f6ce29104ce1b91f7c60142 Mon Sep 17 00:00:00 2001 From: Eric Cornelissen Date: Sat, 2 Oct 2021 21:15:13 +0200 Subject: [PATCH 1/5] test: run SVGLint API without config Add a test for each of the SVGLint API functions to verify they work if no config is provided - with the expected behaviour being the same as that of providing the config `{}` explicitly. --- test/api.spec.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/api.spec.js b/test/api.spec.js index c3a2e87..eda0c1b 100644 --- a/test/api.spec.js +++ b/test/api.spec.js @@ -9,6 +9,14 @@ process.on("unhandledRejection", error => { const svg = ""; describe(".lintSource()", function() { + it("should succeed without config", function(done) { + const result = SVGLint.lintSource(svg); + result.on("done", () => { + expect(result.state).toBe(result.STATES.success); + done(); + }); + }); + it("should succeed with empty config", function(done) { const result = SVGLint.lintSource(svg, {}); result.on("done", () => { @@ -67,4 +75,14 @@ describe(".lintFile()", function() { }); }); }); + + it("should succeed without config", function() { + return SVGLint.lintFile(path.join(__dirname, "./svgs/empty.svg")) + .then(linting => { + linting.on("done", () => { + expect(linting.state).toBe(linting.STATES.success); + }); + }); + }); + }); From 1e7c25b9564fae6a839b59c3ebac1145eb5a50ef Mon Sep 17 00:00:00 2001 From: Eric Cornelissen Date: Sat, 2 Oct 2021 21:17:56 +0200 Subject: [PATCH 2/5] feat: update API to make config optional Update the API so that the linting configuration is optional. Currently, the config falls back to an empty object. --- src/svglint.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/svglint.js b/src/svglint.js index a958b24..9475d77 100644 --- a/src/svglint.js +++ b/src/svglint.js @@ -129,10 +129,10 @@ module.exports = { * The function returns before the Linting is finished. * You should listen to Linting.on("done") to wait for the result. * @param {String} source The SVG to lint - * @param {Config} config The config to lint by + * @param {Config} [config={}] The config to lint by * @return {Linting} The Linting that represents the result */ - lintSource(source, config) { + lintSource(source, config={}) { const ast = parse.parseSource(source); return lint(null, ast, config); }, @@ -142,10 +142,10 @@ module.exports = { * The returned Promise resolves before the Linting is finished. * You should listen to Linting.on("done") to wait for the result. * @param {String} file The file path to lint - * @param {Config} config The config to lint by + * @param {Config} [config={}] The config to lint by * @returns {Promise} Resolves to the Linting that represents the result */ - async lintFile(file, config) { + async lintFile(file, config={}) { const ast = await parse.parseFile(file); return lint(file, ast, config); } From cd58c350881576a588aece7a25aa936c7ae90c2b Mon Sep 17 00:00:00 2001 From: Eric Cornelissen Date: Sat, 2 Oct 2021 21:20:39 +0200 Subject: [PATCH 3/5] feat: don't require config file with CLI Update the SVGLint CLI to not require a configuration file. The `getConfigurationFile` function was updated to output a special value (`false`) if no configuration file is found. When the CLI, using this function, detects there's no config file it will not try to read (`require`) that file. --- bin/cli.js | 4 +++- src/cli/config.js | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index a744d88..7bda4d5 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -61,7 +61,9 @@ process.on("exit", () => { let configObj; try { const configFile = await getConfigurationFile(cli.flags.config); - configObj = require(configFile); + if (configFile) { + configObj = require(configFile); + } } catch (e) { logger.error(`Failed to parse config: ${e.stack}`); process.exit(1); diff --git a/src/cli/config.js b/src/cli/config.js index 4c4b298..b144ff3 100644 --- a/src/cli/config.js +++ b/src/cli/config.js @@ -3,7 +3,6 @@ const fs = require("fs"); /** * Gets the configuration file to use - * Throws if file isn't found * @param {String} filename The filename to look for * @param {String} folder The folder to look in * @returns {Promise} The path to the configuration file, or false @@ -20,7 +19,7 @@ function getConfigurationFile(filename=".svglintrc.js", folder=process.cwd()) { } else { const parent = path.resolve(folder, ".."); if (parent === folder) { - return rej(new Error(`Config file not found at '${resolved}'`)); + return res(false); } // if not, get next folder getConfigurationFile( From 52a492a0197094c98fd96536bc2b2e93bd6d39aa Mon Sep 17 00:00:00 2001 From: Eric Cornelissen Date: Sat, 2 Oct 2021 21:49:45 +0200 Subject: [PATCH 4/5] chore(docs): add note about the default configuration --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 452796f..49a7e3f 100644 --- a/README.md +++ b/README.md @@ -73,3 +73,5 @@ For specifics on how the config for each rule should be formatted, see [their sp If you are using the JS API, this configuration object is passed as the second parameter. +If no configuration is found or provided, a default configuration object is used. +This default configuration may be changed such that previously valid SVGs become invalid in minor releases and patches. From 50a4e91d425d6b4b3634ccaf4b4460e257870760 Mon Sep 17 00:00:00 2001 From: Eric Cornelissen Date: Sat, 2 Oct 2021 22:00:36 +0200 Subject: [PATCH 5/5] feat: exit code 1 when specified config file is missing Update the CLI such that *if* a configuration file path is specified, i.e. the `--config` option is used, then the configuration file must exist. If it does not exist, the CLI will exist with a non-zero exit code, after logging an error. If `--config` is not used the CLI behaviour is unchanged. --- bin/cli.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bin/cli.js b/bin/cli.js index 7bda4d5..fe9b182 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -63,6 +63,12 @@ process.on("exit", () => { const configFile = await getConfigurationFile(cli.flags.config); if (configFile) { configObj = require(configFile); + } else { + logger.debug("No configuration file found") + if (cli.flags.config) { + logger.error("Configuration file not found"); + process.exit(1); + } } } catch (e) { logger.error(`Failed to parse config: ${e.stack}`);