From 1065ad7141ff2a0bd3f17e73b8045a56b923607c Mon Sep 17 00:00:00 2001 From: "Daniel D. Beck" Date: Fri, 10 Jul 2020 17:02:53 +0100 Subject: [PATCH 1/5] Make link list ingredient handlers look like other ingredient factories --- .../ingredient-handlers/data-class-members.js | 33 ++++--------------- .../ingredient-handlers/data-constructor.js | 2 +- .../ingredient-handlers/link-list-checker.js | 13 +++++++- 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-class-members.js b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-class-members.js index 094a37c4..6005da75 100644 --- a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-class-members.js +++ b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-class-members.js @@ -1,50 +1,31 @@ -const checkLinkList = require("./link-list-checker.js"); -const { sectionHandler } = require("./utils"); +const { linkListHandler } = require("./link-list-checker.js"); /** * Handler for the `data.constructor_properties` ingredient. */ -const handleDataConstructorProperties = sectionHandler( - "Constructor_properties", - checkLinkList, - true +const handleDataConstructorProperties = linkListHandler( + "Constructor_properties" ); /** * Handler for the `data.static_methods` ingredient. */ -const handleDataStaticMethods = sectionHandler( - "Static_methods", - checkLinkList, - true -); +const handleDataStaticMethods = linkListHandler("Static_methods"); /** * Handler for the `data.static_properties` ingredient. */ -const handleDataStaticProperties = sectionHandler( - "Static_properties", - checkLinkList, - true -); +const handleDataStaticProperties = linkListHandler("Static_properties"); /** * Handler for the `data.instance_methods` ingredient. */ -const handleDataInstanceMethods = sectionHandler( - "Instance_methods", - checkLinkList, - true -); +const handleDataInstanceMethods = linkListHandler("Instance_methods"); /** * Handler for the `data.instance_properties` ingredient. */ -const handleDataInstanceProperties = sectionHandler( - "Instance_properties", - checkLinkList, - true -); +const handleDataInstanceProperties = linkListHandler("Instance_properties"); module.exports = { handleDataConstructorProperties, diff --git a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-constructor.js b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-constructor.js index 4201195d..e5b6064b 100644 --- a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-constructor.js +++ b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-constructor.js @@ -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."; diff --git a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/link-list-checker.js b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/link-list-checker.js index 0aae09c9..b6b2f8a6 100644 --- a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/link-list-checker.js +++ b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/link-list-checker.js @@ -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, @@ -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 }; From 8274c918493b233fa8c9c1ded98367bb6274e3cf Mon Sep 17 00:00:00 2001 From: "Daniel D. Beck" Date: Fri, 10 Jul 2020 17:11:36 +0100 Subject: [PATCH 2/5] Use one file per ingredient handler --- .../ingredient-handlers/data-class-members.js | 36 ------------------- .../data-constructor-properties.js | 10 ++++++ .../data-instance-methods.js | 8 +++++ .../data-instance-properties.js | 8 +++++ .../data-static-methods.js | 8 +++++ .../data-static-properties.js | 8 +++++ .../ingredient-handlers/index.js | 11 +++--- 7 files changed, 47 insertions(+), 42 deletions(-) delete mode 100644 scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-class-members.js create mode 100644 scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-constructor-properties.js create mode 100644 scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-instance-methods.js create mode 100644 scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-instance-properties.js create mode 100644 scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-static-methods.js create mode 100644 scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-static-properties.js diff --git a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-class-members.js b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-class-members.js deleted file mode 100644 index 6005da75..00000000 --- a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-class-members.js +++ /dev/null @@ -1,36 +0,0 @@ -const { linkListHandler } = require("./link-list-checker.js"); - -/** - * Handler for the `data.constructor_properties` ingredient. - */ -const handleDataConstructorProperties = linkListHandler( - "Constructor_properties" -); - -/** - * Handler for the `data.static_methods` ingredient. - */ -const handleDataStaticMethods = linkListHandler("Static_methods"); - -/** - * Handler for the `data.static_properties` ingredient. - */ -const handleDataStaticProperties = linkListHandler("Static_properties"); - -/** - * Handler for the `data.instance_methods` ingredient. - */ -const handleDataInstanceMethods = linkListHandler("Instance_methods"); - -/** - * Handler for the `data.instance_properties` ingredient. - */ -const handleDataInstanceProperties = linkListHandler("Instance_properties"); - -module.exports = { - handleDataConstructorProperties, - handleDataStaticMethods, - handleDataStaticProperties, - handleDataInstanceMethods, - handleDataInstanceProperties, -}; diff --git a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-constructor-properties.js b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-constructor-properties.js new file mode 100644 index 00000000..8610248d --- /dev/null +++ b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-constructor-properties.js @@ -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; diff --git a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-instance-methods.js b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-instance-methods.js new file mode 100644 index 00000000..a67a4356 --- /dev/null +++ b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-instance-methods.js @@ -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; diff --git a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-instance-properties.js b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-instance-properties.js new file mode 100644 index 00000000..7bc0d131 --- /dev/null +++ b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-instance-properties.js @@ -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; diff --git a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-static-methods.js b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-static-methods.js new file mode 100644 index 00000000..57b94837 --- /dev/null +++ b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-static-methods.js @@ -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; diff --git a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-static-properties.js b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-static-properties.js new file mode 100644 index 00000000..bb7ee4aa --- /dev/null +++ b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/data-static-properties.js @@ -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; diff --git a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/index.js b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/index.js index 21da4b90..b65385ec 100644 --- a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/index.js +++ b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/index.js @@ -1,6 +1,5 @@ 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"); @@ -31,18 +30,18 @@ const handleProseShortDescription = require("./prose-short-description"); const ingredientHandlers = { "data.browser_compatibility": handleDataBrowserCompatibility, "data.constituent_properties": handleDataConstituentProperties, - "data.constructor_properties?": classMembers.handleDataConstructorProperties, + "data.constructor_properties?": require("./data-constructor-properties"), "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.instance_methods?": require("./data-instance-methods"), + "data.instance_properties?": require("./data-instance-properties"), "data.interactive_example?": handleDataInteractiveExample, "data.permitted_properties?": handleDataPermittedProperties, "data.specifications": handleDataSpecifications, - "data.static_methods?": classMembers.handleDataStaticMethods, - "data.static_properties?": classMembers.handleDataStaticProperties, + "data.static_methods?": require("./data-static-methods"), + "data.static_properties?": require("./data-static-properties"), "prose.accessibility_concerns?": optionalTopLevelHeading( "Accessibility_concerns" ), From 857c16fab28377a882cb1708ff0ccaba5935a677 Mon Sep 17 00:00:00 2001 From: "Daniel D. Beck" Date: Fri, 10 Jul 2020 17:14:35 +0100 Subject: [PATCH 3/5] Make registering handlers less repetitous --- .../ingredient-handlers/index.js | 31 ++++++------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/index.js b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/index.js index b65385ec..c54d231e 100644 --- a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/index.js +++ b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/index.js @@ -1,16 +1,5 @@ const { select } = require("hast-util-select"); -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"); - /** * Functions to check for recipe ingredients in Kuma page sources. * @@ -28,18 +17,18 @@ const handleProseShortDescription = require("./prose-short-description"); * */ const ingredientHandlers = { - "data.browser_compatibility": handleDataBrowserCompatibility, - "data.constituent_properties": handleDataConstituentProperties, + "data.browser_compatibility": require("./data-browser-compatibility"), + "data.constituent_properties": require("./data-constituent-properties"), "data.constructor_properties?": require("./data-constructor-properties"), - "data.constructor": handleDataConstructor, - "data.examples": handleDataExamples, - "data.formal_definition": handleDataFormalDefinition, - "data.formal_syntax": handleDataFormalSyntax, + "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?": handleDataInteractiveExample, - "data.permitted_properties?": handleDataPermittedProperties, - "data.specifications": handleDataSpecifications, + "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?": optionalTopLevelHeading( @@ -49,7 +38,7 @@ const ingredientHandlers = { "prose.error_type": requireTopLevelHeading("Error_type"), "prose.message": requireTopLevelHeading("Message"), "prose.see_also": requireTopLevelHeading("See_also"), - "prose.short_description": handleProseShortDescription, + "prose.short_description": require("./prose-short-description"), "prose.syntax": requireTopLevelHeading("Syntax"), "prose.what_went_wrong": requireTopLevelHeading("What_went_wrong"), }; From a5878e44714d45d7d08b153b3f56aa490a94e106 Mon Sep 17 00:00:00 2001 From: "Daniel D. Beck" Date: Fri, 10 Jul 2020 17:18:59 +0100 Subject: [PATCH 4/5] Make section handler factories reuse the section handler wrapper --- .../ingredient-handlers/index.js | 37 ++++++------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/index.js b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/index.js index c54d231e..af6a048c 100644 --- a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/index.js +++ b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/index.js @@ -1,4 +1,4 @@ -const { select } = require("hast-util-select"); +const { sectionHandler } = require("./utils"); /** * Functions to check for recipe ingredients in Kuma page sources. @@ -31,16 +31,16 @@ const ingredientHandlers = { "data.specifications": require("./data-specifications"), "data.static_methods?": require("./data-static-methods"), "data.static_properties?": require("./data-static-properties"), - "prose.accessibility_concerns?": optionalTopLevelHeading( + "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.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": requireTopLevelHeading("Syntax"), - "prose.what_went_wrong": requireTopLevelHeading("What_went_wrong"), + "prose.syntax": requiredSectionHandler("Syntax"), + "prose.what_went_wrong": requiredSectionHandler("What_went_wrong"), }; /** @@ -50,14 +50,8 @@ const ingredientHandlers = { * @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; - }; +function optionalSectionHandler(id) { + return sectionHandler(id, () => true, true); } /** @@ -67,15 +61,8 @@ function optionalTopLevelHeading(id) { * @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; - }; +function requiredSectionHandler(id) { + return sectionHandler(id, () => true); } module.exports = ingredientHandlers; From 9f957ead50057aaedc99878df48f93508964f707 Mon Sep 17 00:00:00 2001 From: "Daniel D. Beck" Date: Fri, 10 Jul 2020 17:21:37 +0100 Subject: [PATCH 5/5] Move section handler factories to utils --- .../ingredient-handlers/index.js | 24 +------------------ .../ingredient-handlers/utils.js | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/index.js b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/index.js index af6a048c..80771a58 100644 --- a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/index.js +++ b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/index.js @@ -1,4 +1,4 @@ -const { sectionHandler } = require("./utils"); +const { optionalSectionHandler, requiredSectionHandler } = require("./utils"); /** * Functions to check for recipe ingredients in Kuma page sources. @@ -43,26 +43,4 @@ const ingredientHandlers = { "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 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 = ingredientHandlers; diff --git a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/utils.js b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/utils.js index 84d2c151..3ec5eef6 100644 --- a/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/utils.js +++ b/scripts/scraper-ng/rules/html-require-recipe-ingredients/ingredient-handlers/utils.js @@ -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,