Skip to content

Commit

Permalink
add 'extends' property for extending ruleset
Browse files Browse the repository at this point in the history
  • Loading branch information
willnorris committed Mar 31, 2021
1 parent a7f18cf commit 4b9610c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const fetch = require('node-fetch')
const findConfig = require('find-config')
const fs = require('fs')
const jsonfile = require('jsonfile')
const lodash = require('lodash')
const path = require('path')
const yaml = require('js-yaml')

Expand All @@ -26,11 +27,12 @@ function isAbsoluteURL(url) {
/**
* Load a ruleset config from the specified location.
*
* @param {string} configLocation A URL, file location, or directory containing a repolinter config file
* @param {string} [configLocation] A URL, file location, or directory containing a repolinter config file
* @param {array} [processed] list of config files already processed, used to prevent loops
* @returns {Object} The loaded repolinter json config
* @throws Will throw an error if unable to parse config or if config is invalid
*/
async function loadConfig(configLocation) {
async function loadConfig(configLocation, processed = []) {
if (configLocation == null) {
throw new Error('must specify config location')
}
Expand Down Expand Up @@ -74,6 +76,28 @@ async function loadConfig(configLocation) {
}
}

// merge extended rulesets
if (ruleset.extends) {
processed.push(configLocation)
if (processed.length > 20) {
// safeguard against infinite loops. expose as flag one day if needed
throw new Error('exceeded maximum 20 ruleset extensions')
}

let parent
if (isAbsoluteURL(ruleset.extends)) {
parent = ruleset.extends
} else if (isAbsoluteURL(configLocation)) {
parent = new URL(ruleset.extends, configLocation)
} else {
parent = path.resolve(path.dirname(configLocation), ruleset.extends)
}
if (!processed.includes(parent)) {
const parentRuleset = await loadConfig(parent, processed)
ruleset = lodash.merge({}, parentRuleset, ruleset)
}
}

return ruleset
}

Expand Down
4 changes: 4 additions & 0 deletions rulesets/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
"properties": {
"$schema": { "type": "string" },
"version": { "const": 2 },
"extends": {
"type": "string",
"title": "URL or path of ruleset file this ruleset extends"
},
"axioms": {
"type": "object",
"title": "The axioms schema",
Expand Down

0 comments on commit 4b9610c

Please sign in to comment.