forked from mdn/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert remove-redundant-flags script into linter and fixer (mdn#21512)
- Loading branch information
1 parent
b150785
commit 1546e73
Showing
7 changed files
with
524 additions
and
649 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.