Skip to content

Commit

Permalink
Allowlist web-feature tags (#25614)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elchi3 authored Jan 9, 2025
1 parent f986486 commit dc1205a
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 40 deletions.
3 changes: 0 additions & 3 deletions api/Request.json
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,6 @@
"description": "`init.keepalive` parameter",
"mdn_url": "https://developer.mozilla.org/docs/Web/API/Request/keepalive",
"spec_url": "https://fetch.spec.whatwg.org/#dom-requestinit-keepalive",
"tags": [
"web-features:fetch-keepalive"
],
"support": {
"chrome": {
"version_added": "66"
Expand Down
4 changes: 0 additions & 4 deletions api/WebSocketError.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"WebSocketError": {
"__compat": {
"tags": [
"web-features:web-socket-stream",
"web-features:websockets"
],
"support": {
Expand Down Expand Up @@ -40,7 +39,6 @@
"__compat": {
"description": "`WebSocketError()` constructor",
"tags": [
"web-features:web-socket-stream",
"web-features:websockets"
],
"support": {
Expand Down Expand Up @@ -77,7 +75,6 @@
"closeCode": {
"__compat": {
"tags": [
"web-features:web-socket-stream",
"web-features:websockets"
],
"support": {
Expand Down Expand Up @@ -114,7 +111,6 @@
"reason": {
"__compat": {
"tags": [
"web-features:web-socket-stream",
"web-features:websockets"
],
"support": {
Expand Down
18 changes: 0 additions & 18 deletions api/WebSocketStream.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
"WebSocketStream": {
"__compat": {
"mdn_url": "https://developer.mozilla.org/docs/Web/API/WebSocketStream",
"tags": [
"web-features:web-socket-stream"
],
"support": {
"chrome": {
"version_added": "124"
Expand Down Expand Up @@ -40,9 +37,6 @@
"__compat": {
"description": "`WebSocketStream()` constructor",
"mdn_url": "https://developer.mozilla.org/docs/Web/API/WebSocketStream/WebSocketStream",
"tags": [
"web-features:web-socket-stream"
],
"support": {
"chrome": {
"version_added": "124"
Expand Down Expand Up @@ -77,9 +71,6 @@
"close": {
"__compat": {
"mdn_url": "https://developer.mozilla.org/docs/Web/API/WebSocketStream/close",
"tags": [
"web-features:web-socket-stream"
],
"support": {
"chrome": {
"version_added": "124"
Expand Down Expand Up @@ -114,9 +105,6 @@
"closed": {
"__compat": {
"mdn_url": "https://developer.mozilla.org/docs/Web/API/WebSocketStream/closed",
"tags": [
"web-features:web-socket-stream"
],
"support": {
"chrome": {
"version_added": "124"
Expand Down Expand Up @@ -151,9 +139,6 @@
"opened": {
"__compat": {
"mdn_url": "https://developer.mozilla.org/docs/Web/API/WebSocketStream/opened",
"tags": [
"web-features:web-socket-stream"
],
"support": {
"chrome": {
"version_added": "124"
Expand Down Expand Up @@ -188,9 +173,6 @@
"url": {
"__compat": {
"mdn_url": "https://developer.mozilla.org/docs/Web/API/WebSocketStream/url",
"tags": [
"web-features:web-socket-stream"
],
"support": {
"chrome": {
"version_added": "124"
Expand Down
3 changes: 0 additions & 3 deletions html/elements/script.json
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,6 @@
"integrity": {
"__compat": {
"spec_url": "https://html.spec.whatwg.org/multipage/webappapis.html#normalizing-a-module-integrity-map",
"tags": [
"web-features:import-map-integrity"
],
"support": {
"chrome": {
"version_added": "127"
Expand Down
12 changes: 8 additions & 4 deletions lint/linter/test-tags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('test.check', () => {

it('should not log error when tags are valid', () => {
const data: CompatStatement = {
tags: ['web-features:tag1'],
tags: ['web-features:javascript'],
support: {},
};

Expand Down Expand Up @@ -61,15 +61,19 @@ describe('test.check', () => {
assert.ok(logger.messages[0].message.includes('Invalid tag found:'));
});

it('should log error when tag name uses characters other than lowercase alphanumeric and hyphen', () => {
it('should log an error when an invalid web-feature ID is used', () => {
const data: CompatStatement = {
tags: ['namespace1:tag$'],
tags: ['web-features:foo'],
support: {},
};

test.check(logger, { data });

assert.equal(logger.messages.length, 1);
assert.ok(logger.messages[0].message.includes('Invalid tag found:'));
assert.ok(
logger.messages[0].message.includes(
'Non-registered web-features ID found:',
),
);
});
});
28 changes: 20 additions & 8 deletions lint/linter/test-tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
* See LICENSE file for more information. */

import chalk from 'chalk-template';
import { features } from 'web-features';

import { Linter, Logger, LinterData } from '../utils.js';
import { CompatStatement } from '../../types/types.js';

const allowedNamespaces = ['web-features'];
const validFeatureIDs = Object.keys(features);

/**
* Process the data for spec URL errors
Expand All @@ -20,19 +22,29 @@ const processData = (data: CompatStatement, logger: Logger): void => {

for (const tag of data.tags) {
if (
!allowedNamespaces.some(
(namespace) =>
tag.startsWith(namespace + ':') &&
tag.split(':')[1].match(/^[a-z0-9-]*$/),
)
!allowedNamespaces.some((namespace) => tag.startsWith(namespace + ':'))
) {
logger.error(
chalk`Invalid tag found: {bold ${tag}}. Check if:
- tag tag has a namespace
- the tag uses one of the allowed namespaces: {bold ${allowedNamespaces}}
- the tag name (after the namespace) uses only lowercase alphanumeric characters (a-z and 0-9) plus the - character (hyphen or minus sign)`,
- the tag has a namespace
- the tag uses one of the allowed namespaces: {bold ${allowedNamespaces}}`,
);
}

if (tag.startsWith('web-features')) {
const featureID = tag.split(':').pop() as string;

if (
!tag.includes(':snapshot:') && // ignore web-feature snapshots for now
!validFeatureIDs.includes(featureID)
) {
logger.error(
chalk`Non-registered web-features ID found: {bold ${featureID}}.
- New web-feature IDs need to be present in https://github.com/web-platform-dx/web-features first.
- Check for typos or remove tag. Tagging will be taken care of by web-features maintainers.`,
);
}
}
}
};

Expand Down
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"tempy": "^3.1.0",
"tsx": "^4.19.2",
"typescript": "~5.7.2",
"web-features": "^2.15.0",
"web-specs": "^3.0.0",
"yargs": "~17.7.0"
},
Expand Down

0 comments on commit dc1205a

Please sign in to comment.