123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- "use strict";
- const builtInRules = require("../rules");
- function explodeArray(xs) {
- return xs.reduce((accumulator, x) => {
- accumulator.push([x]);
- return accumulator;
- }, []);
- }
- function combineArrays(arr1, arr2) {
- const res = [];
- if (arr1.length === 0) {
- return explodeArray(arr2);
- }
- if (arr2.length === 0) {
- return explodeArray(arr1);
- }
- arr1.forEach(x1 => {
- arr2.forEach(x2 => {
- res.push([].concat(x1, x2));
- });
- });
- return res;
- }
- function groupByProperty(objects) {
- const groupedObj = objects.reduce((accumulator, obj) => {
- const prop = Object.keys(obj)[0];
- accumulator[prop] = accumulator[prop] ? accumulator[prop].concat(obj) : [obj];
- return accumulator;
- }, {});
- return Object.keys(groupedObj).map(prop => groupedObj[prop]);
- }
- function combinePropertyObjects(objArr1, objArr2) {
- const res = [];
- if (objArr1.length === 0) {
- return objArr2;
- }
- if (objArr2.length === 0) {
- return objArr1;
- }
- objArr1.forEach(obj1 => {
- objArr2.forEach(obj2 => {
- const combinedObj = {};
- const obj1Props = Object.keys(obj1);
- const obj2Props = Object.keys(obj2);
- obj1Props.forEach(prop1 => {
- combinedObj[prop1] = obj1[prop1];
- });
- obj2Props.forEach(prop2 => {
- combinedObj[prop2] = obj2[prop2];
- });
- res.push(combinedObj);
- });
- });
- return res;
- }
- class RuleConfigSet {
-
-
- constructor(configs) {
-
- this.ruleConfigs = configs || [];
- }
-
- addErrorSeverity() {
- const severity = 2;
- this.ruleConfigs = this.ruleConfigs.map(config => {
- config.unshift(severity);
- return config;
- });
-
- this.ruleConfigs.unshift(severity);
- }
-
- addEnums(enums) {
- this.ruleConfigs = this.ruleConfigs.concat(combineArrays(this.ruleConfigs, enums));
- }
-
- addObject(obj) {
- const objectConfigSet = {
- objectConfigs: [],
- add(property, values) {
- for (let idx = 0; idx < values.length; idx++) {
- const optionObj = {};
- optionObj[property] = values[idx];
- this.objectConfigs.push(optionObj);
- }
- },
- combine() {
- this.objectConfigs = groupByProperty(this.objectConfigs).reduce((accumulator, objArr) => combinePropertyObjects(accumulator, objArr), []);
- }
- };
-
- Object.keys(obj.properties).forEach(prop => {
- if (obj.properties[prop].enum) {
- objectConfigSet.add(prop, obj.properties[prop].enum);
- }
- if (obj.properties[prop].type && obj.properties[prop].type === "boolean") {
- objectConfigSet.add(prop, [true, false]);
- }
- });
- objectConfigSet.combine();
- if (objectConfigSet.objectConfigs.length > 0) {
- this.ruleConfigs = this.ruleConfigs.concat(combineArrays(this.ruleConfigs, objectConfigSet.objectConfigs));
- return true;
- }
- return false;
- }
- }
- function generateConfigsFromSchema(schema) {
- const configSet = new RuleConfigSet();
- if (Array.isArray(schema)) {
- for (const opt of schema) {
- if (opt.enum) {
- configSet.addEnums(opt.enum);
- } else if (opt.type && opt.type === "object") {
- if (!configSet.addObject(opt)) {
- break;
- }
-
- } else {
-
- break;
- }
- }
- }
- configSet.addErrorSeverity();
- return configSet.ruleConfigs;
- }
- function createCoreRuleConfigs(noDeprecated = false) {
- return Array.from(builtInRules).reduce((accumulator, [id, rule]) => {
- const schema = (typeof rule === "function") ? rule.schema : rule.meta.schema;
- const isDeprecated = (typeof rule === "function") ? rule.deprecated : rule.meta.deprecated;
- if (noDeprecated && isDeprecated) {
- return accumulator;
- }
- accumulator[id] = generateConfigsFromSchema(schema);
- return accumulator;
- }, {});
- }
- module.exports = {
- generateConfigsFromSchema,
- createCoreRuleConfigs
- };
|