Skip to content

Enforce use of double quotation marks for descriptions in API History #78

@piotrpdev

Description

@piotrpdev

Change bin/lint-markdown-api-history.ts to throw error on descriptions that are not surrounded by double quotation marks. Special characters can break YAML parsing. This is currently allowed and only throws a warning if a description starts or ends in a special character, since that case is almost always followed by a YAML parsing error.

Change:

// Special chars in YAML strings may break the parser if not surrounded by quotes,
// including just causing the parser to read a value as null instead of throwing an error
// <https://stackoverflow.com/questions/19109912/yaml-do-i-need-quotes-for-strings-in-yaml>
if (checkStrings) {
const possibleStrings = codeBlock.value.matchAll(possibleStringRegex);
for (const [matchedLine, matchedGroup] of possibleStrings) {
const trimmedMatchedGroup = matchedGroup.trim();
const isMatchedGroupInsideQuotes =
(trimmedMatchedGroup.startsWith('"') && trimmedMatchedGroup.endsWith('"')) ||
(trimmedMatchedGroup.startsWith("'") && trimmedMatchedGroup.endsWith("'"));
// Most special characters won't cause a problem if they're inside quotes
if (isMatchedGroupInsideQuotes) continue;
// I've only seen errors occur when the first or last character is a special character - @piotrpdev
const isFirstCharNonAlphaNumeric =
trimmedMatchedGroup[0].match(nonAlphaNumericDotRegex) !== null;
const isLastCharNonAlphaNumeric =
trimmedMatchedGroup.at(-1)?.match(nonAlphaNumericDotRegex) !== null;
if (isFirstCharNonAlphaNumeric || isLastCharNonAlphaNumeric) {
console.warn(
'Warning occurred while parsing Markdown document:\n\n' +
`'${filepath}'\n\n` +
'Possible string value starts/ends with a non-alphanumeric character.\n\n' +
'This might cause issues when parsing the YAML (might not throw an error)\n\n' +
'Matched group:\n\n' +
`${matchedGroup}\n\n` +
'Matched line:\n\n' +
`${matchedLine}\n\n` +
'API history block:\n\n' +
`${possibleHistoryBlock.value}\n`,
);
// Not throwing an error because it might be a false positive or desired behavior
warningCounter++;
}
}
}

Accordingly, change the corresponding tests:

` description: ${stringWarnChar}Made \`trafficLightPosition\` work for \`customButtonOnHover\`.\n` +

and fixtures in /tests/fixtures/api-history-*.md.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions