Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.

Tidy up ingredient handlers #511

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { linkListHandler } = require("./link-list-checker.js");

/**
* Handler for the `data.constructor_properties` ingredient.
*/
const handleDataConstructorProperties = linkListHandler(
"Constructor_properties"
);

module.exports = handleDataConstructorProperties;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const select = require("hast-util-select");

const { checkLinkList } = require("./link-list-checker.js");
const utils = require("./utils.js");
const checkLinkList = require("./link-list-checker.js");

const noConstructor = "This object cannot be instantiated directly.";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { linkListHandler } = require("./link-list-checker.js");

/**
* Handler for the `data.instance_methods` ingredient.
*/
const handleDataInstanceMethods = linkListHandler("Instance_methods");

module.exports = handleDataInstanceMethods;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { linkListHandler } = require("./link-list-checker.js");

/**
* Handler for the `data.instance_properties` ingredient.
*/
const handleDataInstanceProperties = linkListHandler("Instance_properties");

module.exports = handleDataInstanceProperties;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { linkListHandler } = require("./link-list-checker.js");

/**
* Handler for the `data.static_methods` ingredient.
*/
const handleDataStaticMethods = linkListHandler("Static_methods");

module.exports = handleDataStaticMethods;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { linkListHandler } = require("./link-list-checker.js");

/**
* Handler for the `data.static_properties` ingredient.
*/
const handleDataStaticProperties = linkListHandler("Static_properties");

module.exports = handleDataStaticProperties;
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
const { select } = require("hast-util-select");

const classMembers = require("./data-class-members");
const handleDataBrowserCompatibility = require("./data-browser-compatibility");
const handleDataConstituentProperties = require("./data-constituent-properties");
const handleDataConstructor = require("./data-constructor");
const handleDataExamples = require("./data-examples");
const handleDataFormalDefinition = require("./data-formal-definition");
const handleDataFormalSyntax = require("./data-formal-syntax");
const handleDataInteractiveExample = require("./data-interactive-example");
const handleDataPermittedProperties = require("./data-permitted-properties");
const handleDataSpecifications = require("./data-specifications");
const handleProseShortDescription = require("./prose-short-description");
const { optionalSectionHandler, requiredSectionHandler } = require("./utils");

/**
* Functions to check for recipe ingredients in Kuma page sources.
Expand All @@ -29,65 +17,30 @@ const handleProseShortDescription = require("./prose-short-description");
*
*/
const ingredientHandlers = {
"data.browser_compatibility": handleDataBrowserCompatibility,
"data.constituent_properties": handleDataConstituentProperties,
"data.constructor_properties?": classMembers.handleDataConstructorProperties,
"data.constructor": handleDataConstructor,
"data.examples": handleDataExamples,
"data.formal_definition": handleDataFormalDefinition,
"data.formal_syntax": handleDataFormalSyntax,
"data.instance_methods?": classMembers.handleDataInstanceMethods,
"data.instance_properties?": classMembers.handleDataInstanceProperties,
"data.interactive_example?": handleDataInteractiveExample,
"data.permitted_properties?": handleDataPermittedProperties,
"data.specifications": handleDataSpecifications,
"data.static_methods?": classMembers.handleDataStaticMethods,
"data.static_properties?": classMembers.handleDataStaticProperties,
"prose.accessibility_concerns?": optionalTopLevelHeading(
"data.browser_compatibility": require("./data-browser-compatibility"),
"data.constituent_properties": require("./data-constituent-properties"),
"data.constructor_properties?": require("./data-constructor-properties"),
"data.constructor": require("./data-constructor"),
"data.examples": require("./data-examples"),
"data.formal_definition": require("./data-formal-definition"),
"data.formal_syntax": require("./data-formal-syntax"),
"data.instance_methods?": require("./data-instance-methods"),
"data.instance_properties?": require("./data-instance-properties"),
"data.interactive_example?": require("./data-interactive-example"),
"data.permitted_properties?": require("./data-permitted-properties"),
"data.specifications": require("./data-specifications"),
"data.static_methods?": require("./data-static-methods"),
"data.static_properties?": require("./data-static-properties"),
"prose.accessibility_concerns?": optionalSectionHandler(
"Accessibility_concerns"
),
"prose.description?": optionalTopLevelHeading("Description"),
"prose.error_type": requireTopLevelHeading("Error_type"),
"prose.message": requireTopLevelHeading("Message"),
"prose.see_also": requireTopLevelHeading("See_also"),
"prose.short_description": handleProseShortDescription,
"prose.syntax": requireTopLevelHeading("Syntax"),
"prose.what_went_wrong": requireTopLevelHeading("What_went_wrong"),
"prose.description?": optionalSectionHandler("Description"),
"prose.error_type": requiredSectionHandler("Error_type"),
"prose.message": requiredSectionHandler("Message"),
"prose.see_also": requiredSectionHandler("See_also"),
"prose.short_description": require("./prose-short-description"),
"prose.syntax": requiredSectionHandler("Syntax"),
"prose.what_went_wrong": requiredSectionHandler("What_went_wrong"),
};

/**
* A convenience function that returns ingredient handlers for checking
* the existence of an optional H2 in a hast tree.
*
* @param {String} id - an id of an H2 to look for in the hast tree
* @returns {Function} a function
*/
function optionalTopLevelHeading(id) {
return (tree) => {
const heading = select(`h2#${id}`, tree);
if (heading !== null) {
return heading;
}
return null;
};
}

/**
* A convenience function that returns ingredient handlers for checking
* the existence of a certain H2 in a hast tree.
*
* @param {String} id - an id of an H2 to look for in the hast tree
* @returns {Function} a function
*/
function requireTopLevelHeading(id) {
return (tree, logger) => {
const heading = select(`h2#${id}`, tree);
if (heading === null) {
logger.expected(tree, `h2#${id}`, "expected-heading");
return null;
}
return heading;
};
}

module.exports = ingredientHandlers;
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const select = require("hast-util-select");

const utils = require("./utils.js");
const { sectionHandler } = require("./utils.js");

/**
* Returns true only if the given node contains a single child,
Expand Down Expand Up @@ -111,4 +112,14 @@ function checkLinkList(section, logger) {
return true;
}

module.exports = checkLinkList;
/**
* Create a handler for a section that contains a well-formed definition list.
*
* @param {String} id
* @returns {Function} a handler function
*/
function linkListHandler(id) {
return sectionHandler(id, checkLinkList, true);
}

module.exports = { checkLinkList, linkListHandler };
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,36 @@ function sectionHandler(id, handleSectionFn, optional = false) {
};
}

/**
* A convenience function that returns ingredient handlers for checking
* the existence of an optional H2 in a hast tree.
*
* @param {String} id - an id of an H2 to look for in the hast tree
* @returns {Function} a function
*/
function optionalSectionHandler(id) {
return sectionHandler(id, () => true, true);
}

/**
* A convenience function that returns ingredient handlers for checking
* the existence of a certain H2 in a hast tree.
*
* @param {String} id - an id of an H2 to look for in the hast tree
* @returns {Function} a function
*/
function requiredSectionHandler(id) {
return sectionHandler(id, () => true);
}

module.exports = {
findUnexpectedNode,
isMacro,
isNewlineOnlyTextNode,
isWhiteSpaceTextNode,
Logger,
optionalSectionHandler,
requiredSectionHandler,
sectionHandler,
sliceBetween,
sliceSection,
Expand Down