| 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 bcd from '../../index.js'scip-typescript npm @mdn/browser-compat-data 7.3.14 `index.js`/; |
| 7 | const { browsers } = bcd; |
| 8 | |
| 9 | /** @import {Linter, LinterData} from '../types.js' */ |
| 10 | /** @import {Logger} from '../utils.js' */ |
| 11 | /** @import {CompatStatement} from '../../types/types.js' */ |
| 12 | |
| 13 | /** |
| 14 | * Check the data for any disallowed browsers or if it's missing required browsers |
| 15 | * @param {CompatStatement} data The data to test |
| 16 | * @param {string} category The category the data belongs to. |
| 17 | * @param {Logger} logger The logger to output errors to. |
| 18 | * @returns {void} |
| 19 | */ |
| 20 | const processDatascip-typescript npm @mdn/browser-compat-data 7.3.14 lint/linter/`test-browsers-presence.js`/processData. = (data, category, logger) => { |
| 21 | if (data.support) { |
| 22 | const support = data.support; |
| 23 | const definedBrowsers = Objectscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Object#.keysscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/ObjectConstructor#keys().(support); |
| 24 | |
| 25 | const displayBrowsers = /** @type {(keyof typeof browsers)[]} */ ( |
| 26 | Objectscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Object#.keysscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/ObjectConstructor#keys().(browsers) |
| 27 | ).filterscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#filter().( |
| 28 | (b) => |
| 29 | [ |
| 30 | 'desktop', |
| 31 | 'mobile', |
| 32 | 'xr', |
| 33 | ...(['api', 'javascript', 'webassembly'].includesscip-typescript npm typescript 5.9.3 lib/`lib.es2016.array.include.d.ts`/Array#includes().(category) |
| 34 | ? ['server'] |
| 35 | : []), |
| 36 | ].includesscip-typescript npm typescript 5.9.3 lib/`lib.es2016.array.include.d.ts`/Array#includes().(browsers[b].type) && |
| 37 | (category !== 'webextensions' || browsers[b].accepts_webextensions), |
| 38 | ); |
| 39 | const requiredBrowsers = /** @type {(keyof typeof browsers)[]} */ ( |
| 40 | Objectscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Object#.keysscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/ObjectConstructor#keys().(browsers) |
| 41 | ).filterscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#filter().( |
| 42 | (b) => |
| 43 | !['ie'].includesscip-typescript npm typescript 5.9.3 lib/`lib.es2016.array.include.d.ts`/Array#includes().(b) && |
| 44 | ['desktop', 'mobile'].includesscip-typescript npm typescript 5.9.3 lib/`lib.es2016.array.include.d.ts`/Array#includes().(browsers[b].type) && |
| 45 | (category !== 'webextensions' || browsers[b].accepts_webextensions), |
| 46 | ); |
| 47 | |
| 48 | const undefEntries = definedBrowsers.filterscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#filter().( |
| 49 | (value) => !(value in browsers), |
| 50 | ); |
| 51 | if (undefEntries.lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length. > 0) { |
| 52 | logger.errorscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`utils.js`/Logger#error().( |
| 53 | `Has the following browsers, which are not defined in BCD: ${styleText('bold', undefEntries.joinscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#join().(', '))}`, |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | const invalidEntries = /** @type {(keyof typeof support)[]} */ ( |
| 58 | Objectscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Object#.keysscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/ObjectConstructor#keys().(support) |
| 59 | ).filterscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#filter().((value) => !displayBrowsers.includesscip-typescript npm typescript 5.9.3 lib/`lib.es2016.array.include.d.ts`/Array#includes().(value)); |
| 60 | if (invalidEntries.lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length. > 0) { |
| 61 | logger.errorscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`utils.js`/Logger#error().( |
| 62 | `Has the following browsers, which are invalid for ${styleText('bold', category)} compat data: ${styleText('bold', invalidEntries.joinscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#join().(', '))}`, |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | const missingEntries = requiredBrowsers.filterscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#filter().( |
| 67 | (value) => !(value in support), |
| 68 | ); |
| 69 | if (missingEntries.lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length. > 0) { |
| 70 | logger.errorscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`utils.js`/Logger#error().( |
| 71 | `Missing the following browsers, which are required for ${styleText('bold', category)} compat data: ${styleText('bold', missingEntries.joinscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#join().(', '))}`, |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | if ( |
| 76 | 'ie' in support && |
| 77 | JSONscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#.stringifyscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#stringify().(support.ie) === '{"version_added":false}' |
| 78 | ) { |
| 79 | logger.errorscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`utils.js`/Logger#error().( |
| 80 | `Unnecessary ${styleText('bold', 'ie')} statement: ${styleText('bold', '"ie": {"version_added": false}')} is implicit and can be removed`, |
| 81 | { fixablescip-typescript npm @mdn/browser-compat-data 7.3.14 lint/linter/`test-browsers-presence.js`/fixable0:: true }, |
| 82 | ); |
| 83 | } |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | /** @type {Linter} */ |
| 88 | export default { |
| 89 | namescip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`types.d.ts`/Linter#name.: 'Browser Presence', |
| 90 | descriptionscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`types.d.ts`/Linter#description.: |
| 91 | 'Test the presence of browser data within compatibility statements', |
| 92 | scopescip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`types.d.ts`/Linter#scope.: 'feature', |
| 93 | /** |
| 94 | * Test the data |
| 95 | * @param {Logger} logger The logger to output errors to |
| 96 | * @param {LinterData} root The data to test |
| 97 | */ |
| 98 | 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., pathscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`types.d.ts`/LinterData#path.: { categoryscip-typescript npm @mdn/browser-compat-data 7.3.14 lint/`types.d.ts`/LinterPath#category. } }) => { |
| 99 | processDatascip-typescript npm @mdn/browser-compat-data 7.3.14 lint/linter/`test-browsers-presence.js`/processData.(/** @type {CompatStatement} */ (data), category, logger); |
| 100 | }, |
| 101 | }; |