Skip to content

Commit

Permalink
Convert remove-redundant-flags script into linter and fixer (mdn#21512)
Browse files Browse the repository at this point in the history
  • Loading branch information
queengooborg authored Dec 17, 2023
1 parent b150785 commit 1546e73
Show file tree
Hide file tree
Showing 7 changed files with 524 additions and 649 deletions.
266 changes: 266 additions & 0 deletions scripts/fix/flags.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */

import assert from 'node:assert/strict';

import { SupportStatement } from '../../types/types.js';

import { removeIrrelevantFlags } from './flags.js';

const tests: { input: SupportStatement; output: SupportStatement }[] = [
{
input: [
{
version_added: '70',
},
{
version_added: '21',
version_removed: '65',
flags: [
{
type: 'preference',
name: '#service-worker-payment-apps',
value_to_set: 'Enabled',
},
],
},
],
output: {
version_added: '70',
},
},
{
input: [
{
version_added: '62',
},
{
version_added: '21',
version_removed: '80',
flags: [
{
type: 'preference',
name: '#service-worker-payment-apps',
value_to_set: 'Enabled',
},
],
},
],
output: {
version_added: '62',
},
},
{
input: [
{
version_added: '62',
},
{
version_added: '21',
flags: [
{
type: 'preference',
name: '#service-worker-payment-apps',
value_to_set: 'Enabled',
},
],
},
],
output: {
version_added: '62',
},
},
{
input: [
{
version_added: '42',
flags: [
{
type: 'preference',
name: '#enable-experimental-web-features',
value_to_set: 'Enabled',
},
],
},
{
version_added: '21',
version_removed: '45',
flags: [
{
type: 'preference',
name: '#service-worker-payment-apps',
value_to_set: 'Enabled',
},
],
},
],
output: {
version_added: '42',
flags: [
{
type: 'preference',
name: '#enable-experimental-web-features',
value_to_set: 'Enabled',
},
],
},
},
{
input: {
version_added: '42',
version_removed: '43',
flags: [
{
type: 'preference',
name: '#enable-experimental-web-features',
value_to_set: 'Enabled',
},
],
},
output: {
version_added: false,
},
},
{
input: [
{
version_added: '80',
},
{
version_added: '21',
version_removed: '80',
flags: [
{
type: 'preference',
name: '#service-worker-payment-apps',
value_to_set: 'Enabled',
},
],
},
],
output: {
version_added: '80',
},
},
{
input: [
{
version_added: '79',
flags: [
{
type: 'preference',
name: 'enable-experimental-web-platform-features',
value_to_set: 'enabled',
},
],
},
{
version_added: '12',
version_removed: '79',
},
],
output: [
{
version_added: '79',
flags: [
{
type: 'preference',
name: 'enable-experimental-web-platform-features',
value_to_set: 'enabled',
},
],
},
{
version_added: '12',
version_removed: '79',
},
],
},
{
input: [
{
version_added: '86',
},
{
version_added: '21',
flags: [
{
type: 'preference',
name: '#service-worker-payment-apps',
value_to_set: 'Enabled',
},
],
},
],
output: {
version_added: '86',
},
},
{
input: [
{
version_added: '79',
version_removed: '80',
flags: [
{
type: 'preference',
name: 'WebVR',
},
],
},
{
version_added: '15',
version_removed: '79',
},
],
output: {
version_added: '15',
version_removed: '79',
},
},
{
input: [
{
version_added: 'preview',
},
{
version_added: '105',
flags: [
{
type: 'preference',
name: 'layout.css.font-tech.enabled',
value_to_set: 'true',
},
],
},
],
output: [
{
version_added: 'preview',
},
{
version_added: '105',
flags: [
{
type: 'preference',
name: 'layout.css.font-tech.enabled',
value_to_set: 'true',
},
],
},
],
},
];

describe('fix -> flags', () => {
let i = 1;
for (const test of tests) {
it(`Test #${i}`, () => {
const result = removeIrrelevantFlags(test.input);

assert.deepStrictEqual(result, test.output);
});

i += 1;
}
});
92 changes: 92 additions & 0 deletions scripts/fix/flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */

import fs from 'node:fs';

import {
BrowserName,
SupportStatement,
SimpleSupportStatement,
} from '../../types/types.js';
import { IS_WINDOWS } from '../../test/utils.js';
import testFlags, {
isIrrelevantFlagData,
getBasicSupportStatement,
} from '../../test/linter/test-flags.js';
import walk from '../../utils/walk.js';

/**
* Removes irrelevant flags from the compatibility data
* @param supportData The compatibility statement to test
* @returns The compatibility statement with all of the flags removed
*/
export const removeIrrelevantFlags = (
supportData: SupportStatement,
): SupportStatement => {
if (typeof supportData === 'string') {
return supportData;
}

if (!Array.isArray(supportData)) {
if (isIrrelevantFlagData(supportData, undefined)) {
return { version_added: false };
}

return supportData;
}

const result: SimpleSupportStatement[] = [];
const basicSupport = getBasicSupportStatement(supportData);

for (const statement of supportData) {
if (!isIrrelevantFlagData(statement, basicSupport)) {
result.push(statement);
}
}

if (result.length == 0) {
return { version_added: false };
}
if (result.length == 1) {
return result[0];
}
return result;
};

/**
* Removes irrelevant flags from the compatibility data of a specified file
* @param filename The filename containing compatibility info
*/
const fixFlags = (filename: string): void => {
let actual = fs.readFileSync(filename, 'utf-8').trim();

const data = JSON.parse(actual);
const walker = walk(undefined, data);

for (const feature of walker) {
if (testFlags.exceptions?.includes(feature.path)) {
continue;
}

for (const [browser, supportData] of Object.entries(
feature.compat.support,
) as [BrowserName, SupportStatement][]) {
feature.data.__compat.support[browser] =
removeIrrelevantFlags(supportData);
}
}

let expected = JSON.stringify(data, null, 2);

if (IS_WINDOWS) {
// prevent false positives from git.core.autocrlf on Windows
actual = actual.replace(/\r/g, '');
expected = expected.replace(/\r/g, '');
}

if (actual !== expected) {
fs.writeFileSync(filename, expected + '\n', 'utf-8');
}
};

export default fixFlags;
2 changes: 2 additions & 0 deletions scripts/fix/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import fixBrowserOrder from './browser-order.js';
import fixFeatureOrder from './feature-order.js';
import fixPropertyOrder from './property-order.js';
import fixStatementOrder from './statement-order.js';
import fixFlags from './flags.js';
import fixLinks from './links.js';
import fixStatus from './status.js';
import fixMirror from './mirror.js';
Expand Down Expand Up @@ -45,6 +46,7 @@ const load = async (...files: string[]): Promise<void> => {
fixBrowserOrder(file);
fixFeatureOrder(file);
fixStatementOrder(file);
fixFlags(file);
fixLinks(file);
fixStatus(file);
fixMirror(file);
Expand Down
Loading

0 comments on commit 1546e73

Please sign in to comment.