Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate descriptions linter functions into one #4872

Merged
merged 27 commits into from
May 13, 2022
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a2aabd0
Consolidate descriptions linter functions into one
queengooborg Sep 23, 2019
57a01f9
Merge branch 'master' into linter/descriptions-update
queengooborg Oct 24, 2019
78dfb05
Merge branch 'master' into linter/descriptions-update
queengooborg Oct 25, 2019
b77507a
Create checkError function
queengooborg Oct 25, 2019
66a69f6
Standardize output
queengooborg Oct 25, 2019
f65e45c
Merge branch 'master' into linter/descriptions-update
queengooborg Dec 1, 2019
9fffc56
Merge branch 'master' into linter/descriptions-update
queengooborg Dec 9, 2019
d2cad50
Run Prettier on file
queengooborg Dec 9, 2019
22f78f8
Use arrow functions
queengooborg Feb 2, 2020
0c702f2
Remove redundant return
queengooborg Feb 2, 2020
c5576e0
Update JSDoc comments
queengooborg Feb 2, 2020
793d4d2
Update JSDocs
queengooborg Feb 5, 2020
b568e5c
Add copyright notice
queengooborg Feb 5, 2020
6d1a689
Create processApiData and move API check to processData
queengooborg Apr 6, 2020
023f419
Merge branch 'master' into linter/descriptions-update
queengooborg May 23, 2020
700b8ce
Merge branch 'master' into linter/descriptions-update
queengooborg May 31, 2020
dbae9d5
Fix bad merge
queengooborg May 31, 2020
59219fc
Merge branch 'master' into linter/descriptions-update
queengooborg Oct 8, 2020
f2d7957
Fix Prettier issues
queengooborg Oct 8, 2020
7ce97b4
Merge branch 'master' into linter/descriptions-update
queengooborg Feb 13, 2021
c99182f
Merge branch 'main' into linter/descriptions-update
queengooborg May 4, 2022
5d4063c
Revert out-of-scope changes
queengooborg May 4, 2022
4529b8d
Merge branch 'main' into linter/descriptions-update
queengooborg May 6, 2022
983feaa
Update test/linter/test-descriptions.js
queengooborg May 12, 2022
9acc96d
Update method/variable names
queengooborg May 12, 2022
0c2b9c6
Update test/linter/test-descriptions.js
queengooborg May 13, 2022
a193d97
Merge branch 'main' into linter/descriptions-update
queengooborg May 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 79 additions & 94 deletions test/linter/test-descriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,123 +11,108 @@ const { Logger } = require('../utils.js');
*/

/**
* @param {Identifier} apiData
* @param {String} apiName
* @param {Logger} logger
* Check for errors in the description of a specified statement's description and return whether there's an error and log as such
*
* @param {string} ruleName The name of the error
* @param {string} name The name of the API method
* @param {Identifier} feature - The feature's compat data.
* @param {string} expected Expected description
* @param {Logger} logger The logger to output errors to
* @returns {void}
*/
function hasValidConstrutorDescription(apiData, apiName, logger) {
const constructor = apiData[apiName];
if (
constructor &&
constructor.__compat.description !== `<code>${apiName}()</code> constructor`
) {
logger.error(chalk`Incorrect constructor description for {bold ${apiName}()}
{yellow Actual: {bold "${constructor.__compat.description || ''}"}}
{green Expected: {bold "<code>${apiName}()</code> constructor"}}`);
function checkDescription(ruleName, name, method, expected, logger) {
const actual = method.__compat.description || '';
if (actual != expected) {
logger.error(chalk`{red → Incorrect ${ruleName} description for {bold ${name}}
Actual: {yellow "${actual}"}
Expected: {green "${expected}"}}`);
}
}

/**
* @param {Identifier} apiData
* @param {String} apiName
* @param {Logger} logger
* Process API data and check for any incorrect descriptions in said data, logging any errors
*
* @param {Identifier} apiData The data to test
* @param {string} apiName The name of the API
* @param {Logger} logger The logger to output errors to
* @returns {void}
*/
function hasCorrectDOMEventsDescription(apiData, apiName, logger) {
for (const methodName in apiData) {
if (methodName.endsWith('_event')) {
const event = apiData[methodName];
const eventName = methodName.replace('_event', '');
if (event.__compat.description !== `<code>${eventName}</code> event`) {
logger.error(chalk`Incorrect event description for {bold ${apiName}#${methodName}}
{yellow Actual: {bold "${event.__compat.description || ''}"}}
{green Expected: {bold "<code>${eventName}</code> event"}}`);
}
function processApiData(apiData, apiName, logger) {
for (const featureName in apiData) {
const feature = apiData[featureName];
if (featureName == apiName) {
checkDescription(
'constructor',
`${apiName}()`,
feature,
`<code>${apiName}()</code> constructor`,
logger,
);
} else if (featureName.endsWith('_event')) {
checkDescription(
'event',
`${apiName}.${featureName}`,
feature,
`<code>${featureName.replace('_event', '')}</code> event`,
logger,
);
} else if (featureName.endsWith('_permission')) {
checkDescription(
'permission',
`${apiName}.${featureName}`,
feature,
`<code>${featureName.replace('_permission', '')}</code> permission`,
logger,
);
} else if (featureName == 'secure_context_required') {
checkDescription(
'secure context required',
`${apiName}()`,
feature,
'Secure context required',
logger,
);
} else if (featureName == 'worker_support') {
checkDescription(
'worker',
`${apiName}()`,
feature,
'Available in workers',
logger,
);
}
}
}

/**
* @param {Identifier} apiData
* @param {String} apiName
* @param {Logger} logger
* Process data and check for any incorrect descriptions in said data, logging any errors
*
* @param {Identifier} data The data to test
* @param {Logger} logger The logger to output errors to
* @returns {void}
*/
function hasCorrectSecureContextRequiredDescription(apiData, apiName, logger) {
const secureContext = apiData.secure_context_required;
if (
secureContext &&
secureContext.__compat.description !== `Secure context required`
) {
logger.error(chalk`Incorrect secure context required description for {bold ${apiName}()}
{yellow Actual: {bold "${secureContext.__compat.description || ''}"}}
{green Expected: {bold "Secure context required"}}`);
}
}

/**
* @param {Identifier} apiData
* @param {String} apiName
* @param {Logger} logger
*/
function hasCorrectWebWorkersDescription(apiData, apiName, logger) {
const workerSupport = apiData.worker_support;
if (
workerSupport &&
workerSupport.__compat.description !== `Available in workers`
) {
logger.error(chalk`Incorrect worker support description for {bold ${apiName}()}
{yellow Actual: {bold "${workerSupport.__compat.description || ''}"}}
{green Expected: {bold "Available in workers"}}`);
}
}

/**
* @param {Identifier} apiData
* @param {String} apiName
* @param {Logger} logger
*/
function hasCorrectPermissionDescription(apiData, apiName, logger) {
const expectedDescrition = `<code>${apiName.replace(
'_permission',
'',
)}</code> permission`;
if (
apiName &&
apiName.match('_permission$') &&
apiData &&
apiData.__compat &&
apiData.__compat.description !== expectedDescrition
) {
logger.error(chalk`Incorrect permission description for {bold ${apiName}}
{yellow Actual: {bold "${apiData.__compat.description || ''}"}}
{green Expected: {bold "${expectedDescrition}"}}`);
function processData(data, logger) {
if (data.api) {
for (const apiName in data.api) {
const apiData = data.api[apiName];
processApiData(apiData, apiName, logger);
}
}
}

/**
* @param {string} filename
* Test all of the descriptions through the data in a given filename. This test only functions with files with API data; all other files are silently ignored
*
* @param {string} filename The file to test
* @returns {boolean} Whether the file has errors
*/
function testDescriptions(filename) {
/** @type {Identifier} */
const data = require(filename);

const logger = new Logger('Descriptions');

if (data.api) {
for (const apiName in data.api) {
const apiData = data.api[apiName];
hasValidConstrutorDescription(apiData, apiName, logger);
hasCorrectDOMEventsDescription(apiData, apiName, logger);
hasCorrectSecureContextRequiredDescription(apiData, apiName, logger);
hasCorrectWebWorkersDescription(apiData, apiName, logger);
}
}

if (data.api && data.api.Permissions) {
for (const permissionKey in data.api.Permissions) {
const apiData = data.api.Permissions[permissionKey];
hasCorrectPermissionDescription(apiData, permissionKey, logger);
}
}
processData(data, logger);

logger.emit();
return logger.hasErrors();
Expand Down