-
Notifications
You must be signed in to change notification settings - Fork 1
Development Evolution
Emily Rodriguez edited this page Dec 28, 2022
·
10 revisions
updates.ts
Option 1 (not implemented)
function getExistingDescribeFromControl(control: Control): string {
// Algorithm:
// Locate the index of the last occurrence of the meta-information 'tag'
// if: we have a tag do
// Place each line of the control code into an array
// loop: over the array starting at the end of the line the last meta-information 'tag' was found
// remove any empty before describe block content is found
// add found content to describe block variable, append EOL
// end
// end
// Assumptions:
// 1 - The meta-information 'tag' precedes the describe block
// Potential Problems:
// 1 - The word 'tag' could be part of the describe block
if (control.code) {
let existingDescribeBlock = ''
const lastTag = control.code.lastIndexOf('tag')
if (lastTag > 0) {
const tagEOL = control.code.indexOf('\n',lastTag)
const lastEnd = control.code.lastIndexOf('end')
let processLine = false
control.code.substring(tagEOL,lastEnd).split('\n').forEach((line) => {
// Ignore any blank lines at the beginning of the describe block
if (line !== '' || processLine) {
existingDescribeBlock += line + '\n'
processLine = true
}
})
}
return existingDescribeBlock.trimEnd();
} else {
return ''
}
}
The original code
function getExistingDescribeFromControl(control: Control): string {
if (control.code) {
let existingDescribeBlock = ''
let currentQuoteEscape = ''
const percentBlockRegexp = /%[qQrRiIwWxs]?(?<lDelimiter>[([{<])/;
let inPercentBlock = false;
let inQuoteBlock = false
const inMetadataValueOverride = false
let indentedMetadataOverride = false
let inDescribeBlock = false;
let mostSpacesSeen = 0;
let lDelimiter = '(';
let rDelimiter = ')';
control.code.split('\n').forEach((line) => {
const wordArray = line.trim().split(' ')
const spaces = line.substring(0, line.indexOf(wordArray[0])).length
if (spaces - mostSpacesSeen > 10) {
indentedMetadataOverride = true
} else {
mostSpacesSeen = spaces;
indentedMetadataOverride = false
}
if ((!inPercentBlock && !inQuoteBlock && !inMetadataValueOverride && !indentedMetadataOverride) || inDescribeBlock) {
if (inDescribeBlock && wordArray.length === 1 && wordArray.includes('')) {
existingDescribeBlock += '\n'
}
// Get the number of spaces at the beginning of the current line
else if (spaces >= 2) {
const firstWord = wordArray[0]
if (knownInSpecKeywords.indexOf(firstWord.toLowerCase()) === -1 || (knownInSpecKeywords.indexOf(firstWord.toLowerCase()) !== -1 && spaces > 2) || inDescribeBlock) {
inDescribeBlock = true;
existingDescribeBlock += line + '\n'
}
}
}
wordArray.forEach((word, index) => {
const percentBlockMatch = percentBlockRegexp.exec(word);
if(percentBlockMatch && inPercentBlock === false) {
inPercentBlock = true;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
lDelimiter = percentBlockMatch.groups!.lDelimiter || '(';
switch(lDelimiter) {
case '{': {
rDelimiter = '}';
break;
}
case '[': {
rDelimiter = ']';
break;
}
case '<': {
rDelimiter = '>';
break;
}
default: {
break;
}
}
}
const charArray = word.split('')
charArray.forEach((char, index) => {
if (inPercentBlock) {
if (char === rDelimiter && charArray[index - 1] !== '\\' && !inQuoteBlock) {
inPercentBlock = false;
}
}
if (char === '"' && charArray[index - 1] !== '\\') {
if (!currentQuoteEscape || !inQuoteBlock) {
currentQuoteEscape = '"'
}
if (currentQuoteEscape === '"') {
inQuoteBlock = !inQuoteBlock
}
} else if (char === "'" && charArray[index - 1] !== '\\') {
if (!currentQuoteEscape || !inQuoteBlock) {
currentQuoteEscape = "'"
}
if (currentQuoteEscape === "'") {
inQuoteBlock = !inQuoteBlock
}
}
})
})
})
// Take off the extra newline at the end
return existingDescribeBlock.slice(0, -1)
} else {
return ''
}
}
diffMarkdown.ts
Removed unused function: ```ts function getUpdatedCheckForId(id: string, profile: Profile) { const foundControl = profile.controls.find((control) => control.id === id); return _.get(foundControl?.descs, 'check') || 'Missing check'; } ```Typescript 🧾 objects 🎛️ for InSpec 🔎 profiles 📄