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. diff --git a/bin/cli.js b/bin/cli.js index a744d88..fe9b182 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -61,7 +61,15 @@ process.on("exit", () => { let configObj; try { const configFile = await getConfigurationFile(cli.flags.config); - configObj = require(configFile); + 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}`); 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( 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); } 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); + }); + }); + }); + });