Skip to content

Commit

Permalink
Add deprecated, experimental and standard track flags to traverse (#2…
Browse files Browse the repository at this point in the history
…4707)

* Add deprecated flag to traverse

* Add experimental and standard track flags

* Fix lint
  • Loading branch information
jamesnw authored Oct 14, 2024
1 parent 9b3f9d6 commit 56a8967
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 12 deletions.
168 changes: 157 additions & 11 deletions scripts/traverse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@

import assert from 'node:assert/strict';

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

import { iterateFeatures } from './traverse.js';

describe('iterateFeatures', () => {
it('should yield correct identifiers for given object', () => {
const obj = {
let obj: any;
let options: any;
beforeEach(() => {
obj = {
feature1: {
__compat: {
support: {
chrome: { version_added: '1.0' },
firefox: { version_added: '1.5' },
},
status: {
experimental: false,
standard_track: true,
deprecated: false,
},
},
},
feature2: {
Expand All @@ -24,20 +29,161 @@ describe('iterateFeatures', () => {
chrome: { version_added: '2.0' },
firefox: { version_added: null },
},
status: {
experimental: false,
standard_track: true,
deprecated: false,
},
},
},
};
options = {
browsers: ['chrome', 'firefox'],
values: ['1.0', 'null'],
depth: 2,
tag: '',
identifier: '',
deprecated: undefined,
standard_track: undefined,
experimental: undefined,
};
});

const browsers: BrowserName[] = ['chrome', 'firefox'];
const values = ['1.0', 'null'];
const depth = 2;
const tag = '';
const identifier = '';

it('should yield correct identifiers for given object', () => {
const result = Array.from(
iterateFeatures(obj, browsers, values, depth, tag, identifier),
iterateFeatures(
obj,
options.browsers,
options.values,
options.depth,
options.tag,
options.deprecated,
options.standard_track,
options.experimental,
options.identifier,
),
);

assert.deepEqual(result, ['feature1', 'feature2']);
});

it('should filter out deprecated', () => {
options.deprecated = false;
obj.feature2.__compat.status.deprecated = true;
const result = Array.from(
iterateFeatures(
obj,
options.browsers,
options.values,
options.depth,
options.tag,
options.deprecated,
options.standard_track,
options.experimental,
options.identifier,
),
);

assert.deepEqual(result, ['feature1']);
});

it('should filter out non-deprecated', () => {
options.deprecated = true;
obj.feature2.__compat.status.deprecated = true;
const result = Array.from(
iterateFeatures(
obj,
options.browsers,
options.values,
options.depth,
options.tag,
options.deprecated,
options.standard_track,
options.experimental,
options.identifier,
),
);

assert.deepEqual(result, ['feature2']);
});

it('should filter out non-experimental', () => {
obj.feature2.__compat.status.experimental = true;
options.experimental = true;
const result = Array.from(
iterateFeatures(
obj,
options.browsers,
options.values,
options.depth,
options.tag,
options.deprecated,
options.standard_track,
options.experimental,
options.identifier,
),
);

assert.deepEqual(result, ['feature2']);
});

it('should filter out experimental', () => {
obj.feature2.__compat.status.experimental = true;
options.experimental = false;
const result = Array.from(
iterateFeatures(
obj,
options.browsers,
options.values,
options.depth,
options.tag,
options.deprecated,
options.standard_track,
options.experimental,
options.identifier,
),
);

assert.deepEqual(result, ['feature1']);
});

it('should filter out non-standard track', () => {
obj.feature1.__compat.status.standard_track = false;
options.standard_track = true;
const result = Array.from(
iterateFeatures(
obj,
options.browsers,
options.values,
options.depth,
options.tag,
options.deprecated,
options.standard_track,
options.experimental,
options.identifier,
),
);

assert.deepEqual(result, ['feature2']);
});

it('should filter out standard track', () => {
obj.feature1.__compat.status.standard_track = false;
options.standard_track = false;
const result = Array.from(
iterateFeatures(
obj,
options.browsers,
options.values,
options.depth,
options.tag,
options.deprecated,
options.standard_track,
options.experimental,
options.identifier,
),
);

assert.deepEqual(result, ['feature1']);
});
});
91 changes: 90 additions & 1 deletion scripts/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import bcd, { dataFolders } from '../index.js';
* @param values The values to test for
* @param depth The depth to traverse
* @param tag The tag to filter results with
* @param deprecated Whether to filter by deprecation status
* @param standard_track Whether to filter by standard track status
* @param experimental Whether to filter by experimental status
* @param identifier The identifier of the current object
* @yields {string} The feature identifier
*/
Expand All @@ -25,13 +28,31 @@ export function* iterateFeatures(
values: string[],
depth: number,
tag: string,
deprecated: boolean | undefined,
standard_track: boolean | undefined,
experimental: boolean | undefined,
identifier: string,
): IterableIterator<string> {
depth--;
if (depth >= 0) {
for (const i in obj) {
if (!!obj[i] && typeof obj[i] == 'object' && i !== '__compat') {
if (obj[i].__compat) {
if (typeof deprecated === 'boolean') {
if (deprecated !== obj[i].__compat.status?.deprecated) {
continue;
}
}
if (typeof standard_track === 'boolean') {
if (standard_track !== obj[i].__compat.status?.standard_track) {
continue;
}
}
if (typeof experimental === 'boolean') {
if (experimental !== obj[i].__compat.status?.experimental) {
continue;
}
}
if (tag) {
const tags = obj[i].__compat?.tags;
if ((tags && tags.includes(tag)) || (!tags && tag == 'false')) {
Expand Down Expand Up @@ -109,6 +130,9 @@ export function* iterateFeatures(
values,
depth,
tag,
deprecated,
standard_track,
experimental,
identifier + i + '.',
);
}
Expand All @@ -123,6 +147,9 @@ export function* iterateFeatures(
* @param values The version values to traverse for
* @param depth The depth to traverse
* @param tag The tag to filter results with
* @param deprecated Whether to filter by deprecation status
* @param standard_track Whether to filter by standard track status
* @param experimental Whether to filter by experimental status
* @param identifier The identifier of the current object
* @returns An array of the features
*/
Expand All @@ -132,10 +159,23 @@ const traverseFeatures = (
values: string[],
depth: number,
tag: string,
deprecated: boolean | undefined,
standard_track: boolean | undefined,
experimental: boolean | undefined,
identifier: string,
): string[] => {
const features = Array.from(
iterateFeatures(obj, browsers, values, depth, tag, identifier),
iterateFeatures(
obj,
browsers,
values,
depth,
tag,
deprecated,
standard_track,
experimental,
identifier,
),
);

return features.filter((item, pos) => features.indexOf(item) == pos);
Expand All @@ -148,6 +188,9 @@ const traverseFeatures = (
* @param values The version values to traverse for
* @param depth The depth to traverse
* @param tag The tag to filter results with
* @param deprecated Whether to filter by deprecation status
* @param standard_track Whether to filter by standard track status
* @param experimental Whether to filter by experimental status
* @returns The list of features
*/
const main = (
Expand All @@ -158,6 +201,9 @@ const main = (
values = ['null', 'true'],
depth = 100,
tag = '',
deprecated = undefined,
standard_track = undefined,
experimental = undefined,
): string[] => {
const features: string[] = [];

Expand All @@ -169,6 +215,9 @@ const main = (
values,
depth,
tag,
deprecated,
standard_track,
experimental,
folders[folder] + '.',
),
);
Expand Down Expand Up @@ -236,6 +285,27 @@ if (esMain(import.meta)) {
type: 'boolean',
default: process.stdout.isTTY,
})
.option('deprecated', {
alias: 'x',
describe:
'Filter features by deprecation status. Set to `true` to only show deprecated features or `false` to only show non-deprecated features.',
type: 'boolean',
default: undefined,
})
.option('standard_track', {
alias: 's',
describe:
'Filter features by standard_track status. Set to `true` to only show standards track features or `false` to only show non-standards track features.',
type: 'boolean',
default: undefined,
})
.option('experimental', {
alias: 'e',
describe:
'Filter features by experimental status. Set to `true` to only show experimental features or `false` to only show non-experimental features.',
type: 'boolean',
default: undefined,
})
.example(
'npm run traverse -- --browser=safari -n',
'Find all features containing non-real Safari entries',
Expand All @@ -259,6 +329,22 @@ if (esMain(import.meta)) {
.example(
'npm run traverse -- -t false',
'Find all features with no tags.',
)
.example(
'npm run traverse -- --deprecated',
'Find all features that are deprecated.',
)
.example(
'npm run traverse -- --no-deprecated',
'Omit all features that are deprecated.',
)
.example(
'npm run traverse -- --standard_track',
'Find all features that are on the standard track.',
)
.example(
'npm run traverse -- --experimental',
'Omit all features that are deprecated.',
);
},
);
Expand All @@ -271,6 +357,9 @@ if (esMain(import.meta)) {
filter,
argv.depth,
argv.tag,
argv.deprecated,
argv.standard_track,
argv.experimental,
);
console.log(features.join('\n'));
if (argv.showCount) {
Expand Down

0 comments on commit 56a8967

Please sign in to comment.