| 1 | /* This file is a part of @mdn/browser-compat-data |
| 2 | * See LICENSE file for more information. */ |
| 3 | |
| 4 | import { styleText } from 'node:util'; |
| 5 | |
| 6 | import walkscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`walk.js`/walk(). from '../../utils/walk.js'scip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`walk.js`/; |
| 7 | import { inventoryscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/inventory. } from '../../utils/mdn-content-inventory.js'scip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/; |
| 8 | |
| 9 | /** @import {Linter, LinterData} from '../types.js' */ |
| 10 | /** @import {Logger} from '../utils.js' */ |
| 11 | /** @import {CompatData, CompatStatement} from '../../types/types.js' */ |
| 12 | |
| 13 | /** |
| 14 | * @typedef {object} MDNURLError |
| 15 | * @property {string} path |
| 16 | * @property {string} ruleName |
| 17 | * @property {string} actual |
| 18 | * @property {string} expected |
| 19 | */ |
| 20 | |
| 21 | /** @type {Map<string, string>} path → mdn_url, persisted across calls */ |
| 22 | export const urlsByPathscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/linter/`test-mdn-urls.js`/urlsByPath. = new Mapscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Map#(); |
| 23 | |
| 24 | /** |
| 25 | * Process the data for MDN URL issues |
| 26 | * @param {CompatStatement} data The data to test |
| 27 | * @param {string} path The path of the feature |
| 28 | * @returns {MDNURLError[]} The issues caught in the file |
| 29 | */ |
| 30 | export const processDatascip-typescript npm @mdn/browser-compat-data 7.3.14 lint/linter/`test-mdn-urls.js`/processData. = (data, path) => { |
| 31 | /** @type {MDNURLError[]} */ |
| 32 | const issues = []; |
| 33 | if (data.mdn_url) { |
| 34 | const mdnURL = new URLscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/URL#(data.mdn_url); |
| 35 | const redirectURL = '/en-US' + mdnURL.pathnamescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/URL#pathname.; |
| 36 | const slug = mdnURL.pathnamescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/URL#pathname..replacescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/String#replace().('/docs/', ''); |
| 37 | const hash = mdnURL.hashscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/URL#hash.; |
| 38 | |
| 39 | if (redirectURL in inventoryscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/inventory..redirectsscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/redirects0:) { |
| 40 | // Replace redirects with the new URL. |
| 41 | issues.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().({ |
| 42 | ruleName: 'mdn_url_redirect', |
| 43 | path, |
| 44 | actual: data.mdn_url, |
| 45 | expected: |
| 46 | mdnURL.originscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/URL#origin. + |
| 47 | inventoryscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/inventory..redirectsscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/redirects0:[redirectURL]?.replace('/en-US', ''), |
| 48 | }); |
| 49 | } else if ( |
| 50 | // Check if casing is wrong. |
| 51 | // slugs.values().some(v => v === slug) when https://tc39.es/proposal-iterator-helpers is available |
| 52 | !Arrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#.fromscip-typescript npm typescript 5.9.3 lib/`lib.es2015.core.d.ts`/ArrayConstructor#from().(inventoryscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/inventory..slugsscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/slugs0:.valuesscip-typescript npm typescript 5.9.3 lib/`lib.es2015.iterable.d.ts`/Map#values().()).includesscip-typescript npm typescript 5.9.3 lib/`lib.es2016.array.include.d.ts`/Array#includes().(slug) && |
| 53 | Arrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#.fromscip-typescript npm typescript 5.9.3 lib/`lib.es2015.core.d.ts`/ArrayConstructor#from().(inventoryscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/inventory..slugsscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/slugs0:.keysscip-typescript npm typescript 5.9.3 lib/`lib.es2015.iterable.d.ts`/Map#keys().()).includesscip-typescript npm typescript 5.9.3 lib/`lib.es2016.array.include.d.ts`/Array#includes().(slug.toLowerCasescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/String#toLowerCase().()) |
| 54 | ) { |
| 55 | issues.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().({ |
| 56 | ruleName: 'mdn_url_casing', |
| 57 | path, |
| 58 | actual: data.mdn_url, |
| 59 | expected: `https://developer.mozilla.org/docs/${inventoryscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/inventory..slugsscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/slugs0:.getscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Map#get().(slug.toLowerCasescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/String#toLowerCase().())}${hash}`, |
| 60 | }); |
| 61 | } else if (!Arrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#.fromscip-typescript npm typescript 5.9.3 lib/`lib.es2015.core.d.ts`/ArrayConstructor#from().(inventoryscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/inventory..slugsscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/slugs0:.valuesscip-typescript npm typescript 5.9.3 lib/`lib.es2015.iterable.d.ts`/Map#values().()).includesscip-typescript npm typescript 5.9.3 lib/`lib.es2016.array.include.d.ts`/Array#includes().(slug)) { |
| 62 | // Delete non-existing MDN pages. |
| 63 | issues.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().({ |
| 64 | ruleName: 'mdn_url_404', |
| 65 | path, |
| 66 | actual: data.mdn_url, |
| 67 | expected: '', |
| 68 | }); |
| 69 | } else if (inventoryscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/inventory..slugByPathscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/slugByPath0:.hasscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Map#has().(path) && !hash) { |
| 70 | // Overwrite url, unless it has a fragment. |
| 71 | const expected = `https://developer.mozilla.org/docs/${inventoryscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/inventory..slugByPathscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/slugByPath0:.getscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Map#get().(path)}`; |
| 72 | if (expected != data.mdn_url) { |
| 73 | issues.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().({ |
| 74 | ruleName: 'mdn_url_other_page', |
| 75 | path, |
| 76 | actual: data.mdn_url, |
| 77 | expected: `https://developer.mozilla.org/docs/${inventoryscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/inventory..slugByPathscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/slugByPath0:.getscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Map#get().(path)}`, |
| 78 | }); |
| 79 | } |
| 80 | } else if (hash !== hash.toLowerCasescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/String#toLowerCase().()) { |
| 81 | // Enforce lower-case fragment. |
| 82 | issues.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().({ |
| 83 | ruleName: 'mdn_url_casing_hash', |
| 84 | path, |
| 85 | actual: data.mdn_url, |
| 86 | expected: `https://developer.mozilla.org/docs/${slug}${hash.toLowerCasescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/String#toLowerCase().()}`, |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | // Check if mdn_url duplicates an ancestor's mdn_url. |
| 91 | const parts = path.splitscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/String#split().('.'); |
| 92 | for (let i = 1; i < parts.lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length.; i++) { |
| 93 | const ancestorPath = parts.slicescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#slice().(0, i).joinscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#join().('.'); |
| 94 | if (urlsByPathscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/linter/`test-mdn-urls.js`/urlsByPath..getscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Map#get().(ancestorPath) === data.mdn_url) { |
| 95 | issues.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().({ |
| 96 | ruleName: 'mdn_url_duplicate_ancestor', |
| 97 | path, |
| 98 | actual: data.mdn_url, |
| 99 | expected: '', |
| 100 | }); |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | // Track this path's mdn_url for future ancestor checks. |
| 105 | urlsByPathscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/linter/`test-mdn-urls.js`/urlsByPath..setscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Map#set().(path, data.mdn_url); |
| 106 | } else if (inventoryscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/inventory..slugByPathscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/slugByPath0:.hasscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Map#has().(path)) { |
| 107 | issues.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().({ |
| 108 | ruleName: 'mdn_url_new_page', |
| 109 | path, |
| 110 | actual: '', |
| 111 | expected: `https://developer.mozilla.org/docs/${inventoryscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/inventory..slugByPathscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`mdn-content-inventory.js`/slugByPath0:.getscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Map#get().(path)}`, |
| 112 | }); |
| 113 | } |
| 114 | return issues; |
| 115 | }; |
| 116 | |
| 117 | /** |
| 118 | * Log issues found by processData |
| 119 | * @param {MDNURLError[]} issues The issues to log |
| 120 | * @param {Logger} logger The logger to output errors to |
| 121 | */ |
| 122 | const logIssuesscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/linter/`test-mdn-urls.js`/logIssues. = (issues, logger) => { |
| 123 | for (const issue of issues) { |
| 124 | if (issue.ruleName === 'mdn_url_duplicate_ancestor') { |
| 125 | logger.errorscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`utils.js`/Logger#error().( |
| 126 | styleText( |
| 127 | 'red', |
| 128 | `${styleText('bold', issue.path)} has mdn_url duplicated from ancestor: ${styleText('italic', issue.actual)}`, |
| 129 | ), |
| 130 | { fixablescip-typescript npm @mdn/browser-compat-data 7.3.14 lint/linter/`test-mdn-urls.js`/fixable0:: true }, |
| 131 | ); |
| 132 | } else if (issue.expected === '') { |
| 133 | logger.warningscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`utils.js`/Logger#warning().( |
| 134 | styleText( |
| 135 | 'red', |
| 136 | `Current mdn_url is a 404: |
| 137 | ${styleText('bold', issue.actual)}`, |
| 138 | ), |
| 139 | { fixablescip-typescript npm @mdn/browser-compat-data 7.3.14 lint/linter/`test-mdn-urls.js`/fixable1:: true }, |
| 140 | ); |
| 141 | } else if (issue.actual === '') { |
| 142 | logger.warningscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`utils.js`/Logger#warning().( |
| 143 | styleText( |
| 144 | 'red', |
| 145 | `New mdn_url to add: |
| 146 | ${styleText('bold', issue.expected)}`, |
| 147 | ), |
| 148 | { fixablescip-typescript npm @mdn/browser-compat-data 7.3.14 lint/linter/`test-mdn-urls.js`/fixable2:: true }, |
| 149 | ); |
| 150 | } else { |
| 151 | logger.warningscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`utils.js`/Logger#warning().( |
| 152 | styleText( |
| 153 | 'red', |
| 154 | `Issues with mdn_url found: |
| 155 | Actual: ${issue.actual} |
| 156 | Expected: ${issue.expected}`, |
| 157 | ), |
| 158 | { fixablescip-typescript npm @mdn/browser-compat-data 7.3.14 lint/linter/`test-mdn-urls.js`/fixable3:: true }, |
| 159 | ); |
| 160 | } |
| 161 | } |
| 162 | }; |
| 163 | |
| 164 | /** @type {Linter} */ |
| 165 | export default { |
| 166 | namescip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`types.d.ts`/Linter#name.: 'MDN URLs', |
| 167 | descriptionscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`types.d.ts`/Linter#description.: 'Ensure the mdn_url values point to existing MDN Web Docs pages', |
| 168 | scopescip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`types.d.ts`/Linter#scope.: 'tree', |
| 169 | /** |
| 170 | * Test the data |
| 171 | * @param {Logger} logger The logger to output errors to |
| 172 | * @param {LinterData} root The data to test |
| 173 | */ |
| 174 | checkscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`types.d.ts`/Linter#check.: (logger, { datascip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`types.d.ts`/LinterData#data. }) => { |
| 175 | for (const feature of walkscip-typescript npm @mdn/browser-compat-data 7.3.14 utils/`walk.js`/walk().(undefined, /** @type {CompatData} */ (data))) { |
| 176 | const issues = processDatascip-typescript npm @mdn/browser-compat-data 7.3.14 lint/linter/`test-mdn-urls.js`/processData.(feature.compat, feature.path); |
| 177 | logIssuesscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/linter/`test-mdn-urls.js`/logIssues.(issues, logger); |
| 178 | } |
| 179 | }, |
| 180 | }; |