Skip to content

Commit

Permalink
Add auto-generated version_last property to support statements (mdn…
Browse files Browse the repository at this point in the history
…#21500)

* Add auto-generated `version_last` property to support statements

* pUpdate test/linter/test-versions.ts

Co-authored-by: Claas Augner <[email protected]>

* Add if clause

* Usee Array.map()

* Update compat-data-schema.md

* Add missing new line

---------

Co-authored-by: Claas Augner <[email protected]>
  • Loading branch information
queengooborg and caugner authored Dec 14, 2023
1 parent eddfa9d commit d5c39ba
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 5 deletions.
23 changes: 19 additions & 4 deletions schemas/compat-data-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,26 +315,41 @@ Default values:

Examples:

- Removed in version 10 (added in 3.5):
- Removed in version 10 (added in 4 and supported up until 9):

```json
{
"version_added": "3.5",
"version_added": "4",
"version_removed": "10"
}
```

- Removed in some version after 3.5:
- Removed in some version after 4:

```json
{
"version_added": "3.5",
"version_added": "4",
"version_removed": true
}
```

Note: many data categories no longer allow for `version_removed` to be set to `true`, as we are working to [improve the quality of the compatibility data](https://github.com/mdn/browser-compat-data/issues/3555).

#### `version_last`

> [!NOTE]
> This property is automatically generated at build time.
If `version_removed` is present, a `version_last` is automatically generated during build time, which will be set to the version number of the last browser version that supported the feature. For example, assuming the browser version only incremented in whole numbers, if a feature was added in version 20 and supported until 29, then was no longer supported in 30, `version_removed` would be `30` and `version_last` will be `29`:

```json
{
"version_added": "20",
"version_removed": "30",
"version_last": "29"
}
```

### Ranged versions (≤)

For certain browser versions, ranged versions (also called "ranged values") are allowed as it is sometimes impractical to find out in which early version of a browser a feature shipped. Ranged versions are a way to include some version data in BCD, while also stating the version number may not be accurate. These values state that the feature has been confirmed to be supported in at least a certain version of the browser, but may have been added in an earlier release. Ranged versions are indicated by the `Less Than or Equal To (U+2264)` (``) symbol before the version number.
Expand Down
13 changes: 13 additions & 0 deletions schemas/compat-data.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@
],
"tsType": "VersionValue"
},
"version_last": {
"description": "A string, indicating the last browser version that supported this feature, or the value true, indicating that the feature was removed in an unknown version. This is automatically generated.",
"anyOf": [
{
"type": "string",
"pattern": "^(≤?(\\d+)(\\.\\d+)*|preview)$"
},
{
"const": true
}
],
"tsType": "VersionValue"
},
"prefix": {
"type": "string",
"description": "A prefix to add to the sub-feature name (defaults to empty string). If applicable, leading and trailing '-' must be included."
Expand Down
56 changes: 55 additions & 1 deletion scripts/release/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import fs from 'node:fs/promises';

import esMain from 'es-main';
import stringify from 'fast-json-stable-stringify';
import { compareVersions } from 'compare-versions';

import { InternalSupportStatement } from '../../types/index.js';
import { BrowserName, CompatData } from '../../types/types.js';
import { BrowserName, CompatData, VersionValue } from '../../types/types.js';
import compileTS from '../generate-types.js';
import { walk } from '../../utils/index.js';
import { WalkOutput } from '../../utils/walk.js';
import bcd from '../../index.js';

import mirrorSupport from './mirror.js';

Expand Down Expand Up @@ -51,6 +53,57 @@ export const applyMirroring = (feature: WalkOutput): void => {
}
};

const getPreviousVersion = (
browser: BrowserName,
version: VersionValue,
): VersionValue => {
if (typeof version === 'string' && !version.startsWith('≤')) {
const browserVersions = Object.keys(bcd.browsers[browser].releases).sort(
compareVersions,
);
const currentVersionIndex = browserVersions.indexOf(version);
if (currentVersionIndex > 0) {
return browserVersions[currentVersionIndex - 1];
}
}

return version;
};

/**
* Add version_last
* @param {WalkOutput} feature The BCD to transform
* @returns {void}
*/
export const addVersionLast = (feature: WalkOutput): void => {
for (const [browser, supportData] of Object.entries(
feature.compat.support as InternalSupportStatement,
)) {
if (Array.isArray(supportData)) {
(feature.data as any).__compat.support[browser] = supportData.map((d) => {
if (d.version_removed) {
return {
...d,
version_last: getPreviousVersion(
browser as BrowserName,
d.version_removed,
),
};
}
return d;
});
} else if (typeof supportData === 'object') {
if ((supportData as any).version_removed) {
(feature.data as any).__compat.support[browser].version_last =
getPreviousVersion(
browser as BrowserName,
(supportData as any).version_removed,
);
}
}
}
};

/**
* Generate a BCD data bundle
* @returns An object containing the prepared BCD data
Expand All @@ -62,6 +115,7 @@ export const createDataBundle = async (): Promise<CompatData> => {

for (const feature of walker) {
applyMirroring(feature);
addVersionLast(feature);
}

return {
Expand Down
6 changes: 6 additions & 0 deletions test/linter/test-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ const checkVersions = (
chalk`{bold ${browser}} cannot have a {bold version_added: false} in an array of statements.`,
);
}

if ('version_last' in statement) {
logger.error(
chalk`{bold version_last} is automatically generated and should not be defined manually.`,
);
}
}
}
};
Expand Down

0 comments on commit d5c39ba

Please sign in to comment.