Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | const chalk = require('chalk'); const { analyzeFuzzy, removeFromSet } = require('./search'); const metadata = require('../metadata/metadata.js'); /** * Validate Attribute * @param {Object} obj The object from the first Promise that has valuable information about the custom prop * @todo Skip scope validation based on stylelint option * @returns {Object} Validation object or definition to skip if not found */ const validateAttribute = (obj) => { if (obj.validate.scope.isGlobal || obj.validate.scope.isShared) return { skip: true }; const results = analyzeFuzzy(obj).filter((item) => item.word); // If key has already been stored, we know its valid if (obj.keys.attribute) { return { valid: true, received: obj.groups[obj.keys.attribute] }; } else if (obj.fuzzyKeys.size > 0 && results.length > 0) { let valid; results.map((item) => { if ( (obj.keys.property && obj.keys.state === item.key + 1) || (obj.keys.property && obj.keys.psuedoState === item.key + 1) ) { obj.keys.attribute = obj.keys.property + 1; obj.fuzzyKeys = removeFromSet(obj.fuzzyKeys, obj.keys.attribute); const receivedValue = obj.groups[obj.keys.attribute]; // Check for compound attributes const nextValue = obj.groups[obj.keys.attribute + 1]; const compoundValue = nextValue ? `${receivedValue}-${nextValue}` : receivedValue; const isValid = metadata.component.valid.attribute.includes(compoundValue) || metadata.component.valid.attribute.includes(receivedValue); valid = { valid: isValid, expected: metadata.component.valid.attribute, received: compoundValue, }; } else if ( !obj.keys.attribute && results.length === 3 && obj.keys.property && item.key === obj.keys.property + 1 ) { obj.keys.attribute = obj.keys.property + 1; obj.fuzzyKeys = removeFromSet(obj.fuzzyKeys, obj.keys.attribute); const receivedValue = obj.groups[obj.keys.attribute]; // Check for compound attributes const nextValue = obj.groups[obj.keys.attribute + 1]; const compoundValue = nextValue ? `${receivedValue}-${nextValue}` : receivedValue; const isValid = metadata.component.valid.attribute.includes(compoundValue) || metadata.component.valid.attribute.includes(receivedValue); valid = { valid: isValid, expected: metadata.component.valid.attribute, received: compoundValue, }; } else if (!obj.keys.attribute && item.lastKey && item.key === obj.keys.property + 1) { const receivedValue = obj.groups[item.key]; // Check for compound attributes const nextValue = obj.groups[item.key + 1]; const compoundValue = nextValue ? `${receivedValue}-${nextValue}` : receivedValue; const isValid = metadata.component.valid.attribute.includes(compoundValue) || metadata.component.valid.attribute.includes(receivedValue); valid = { valid: isValid, received: compoundValue, expected: metadata.component.valid.attribute, }; if (!isValid) { obj.customMessage.push( `We received "${chalk.bold( compoundValue, )}" and have low confidence in determining if this is an attribute, state, or psuedoState. Possible values are ${chalk.bold( metadata.component.valid.attribute, )},${chalk.bold(metadata.component.valid.state)},${chalk.bold( metadata.component.valid.psuedoState, )}.`, ); } } else if ( !obj.keys.attribute && !item.lastKey && item.key === obj.keys.property + 1 && results.length <= 2 ) { const receivedValue = obj.groups[item.key]; // Check for compound attributes const nextValue = obj.groups[item.key + 1]; const compoundValue = nextValue ? `${receivedValue}-${nextValue}` : receivedValue; const isValid = metadata.component.valid.attribute.includes(compoundValue) || metadata.component.valid.attribute.includes(receivedValue); valid = { valid: isValid, received: compoundValue, expected: metadata.component.valid.attribute, }; if (!isValid) { obj.customMessage.push( `We received "${chalk.bold( compoundValue, )}" and have medium confidence that this is an attribute but might also be a state, or psuedoState. Possible values are ${chalk.bold( metadata.component.valid.attribute, )},${chalk.bold(metadata.component.valid.state)},${chalk.bold( metadata.component.valid.psuedoState, )}.`, ); } } else if (obj.keys.property !== obj.keys.category + 1) { valid = { valid: false, received: obj.groups[item.key] }; obj.customMessage.push( `We received "${chalk.bold( obj.groups[item.key], )}" but we do not allow words in between "${chalk.bold( obj.groups[obj.keys.category], )}", a category, and "${chalk.bold(obj.groups[obj.keys.property])}", a property.`, ); } }); return valid; } else { return { skip: true }; } }; module.exports = validateAttribute; |