Skip to content

Commit

Permalink
Combine status options in traverse (#24711)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnw authored Oct 15, 2024
1 parent 56a8967 commit 68e1b14
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 76 deletions.
47 changes: 17 additions & 30 deletions scripts/traverse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ describe('iterateFeatures', () => {
depth: 2,
tag: '',
identifier: '',
deprecated: undefined,
standard_track: undefined,
experimental: undefined,
status: {
deprecated: undefined,
standard_track: undefined,
experimental: undefined,
},
};
});

Expand All @@ -57,9 +59,6 @@ describe('iterateFeatures', () => {
options.values,
options.depth,
options.tag,
options.deprecated,
options.standard_track,
options.experimental,
options.identifier,
),
);
Expand All @@ -68,7 +67,7 @@ describe('iterateFeatures', () => {
});

it('should filter out deprecated', () => {
options.deprecated = false;
options.status.deprecated = false;
obj.feature2.__compat.status.deprecated = true;
const result = Array.from(
iterateFeatures(
Expand All @@ -77,18 +76,16 @@ describe('iterateFeatures', () => {
options.values,
options.depth,
options.tag,
options.deprecated,
options.standard_track,
options.experimental,
options.identifier,
options.status,
),
);

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

it('should filter out non-deprecated', () => {
options.deprecated = true;
options.status.deprecated = true;
obj.feature2.__compat.status.deprecated = true;
const result = Array.from(
iterateFeatures(
Expand All @@ -97,10 +94,8 @@ describe('iterateFeatures', () => {
options.values,
options.depth,
options.tag,
options.deprecated,
options.standard_track,
options.experimental,
options.identifier,
options.status,
),
);

Expand All @@ -109,18 +104,16 @@ describe('iterateFeatures', () => {

it('should filter out non-experimental', () => {
obj.feature2.__compat.status.experimental = true;
options.experimental = true;
options.status.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,
options.status,
),
);

Expand All @@ -129,18 +122,16 @@ describe('iterateFeatures', () => {

it('should filter out experimental', () => {
obj.feature2.__compat.status.experimental = true;
options.experimental = false;
options.status.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,
options.status,
),
);

Expand All @@ -149,18 +140,16 @@ describe('iterateFeatures', () => {

it('should filter out non-standard track', () => {
obj.feature1.__compat.status.standard_track = false;
options.standard_track = true;
options.status.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,
options.status,
),
);

Expand All @@ -169,18 +158,16 @@ describe('iterateFeatures', () => {

it('should filter out standard track', () => {
obj.feature1.__compat.status.standard_track = false;
options.standard_track = false;
options.status.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,
options.status,
),
);

Expand Down
71 changes: 25 additions & 46 deletions scripts/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ import { BrowserName, Identifier } from '../types/types.js';
import { InternalSupportStatement } from '../types/index.js';
import bcd, { dataFolders } from '../index.js';

interface StatusFilters {
deprecated: boolean | undefined;
standard_track: boolean | undefined;
experimental: boolean | undefined;
}

/**
* Traverse all of the features within a specified object and find all features that have one of the specified values
* @param obj The compat data to traverse through
* @param browsers The browsers to test for
* @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
* @param status Whether to filter by status flags
* @yields {string} The feature identifier
*/
export function* iterateFeatures(
Expand All @@ -28,11 +32,10 @@ export function* iterateFeatures(
values: string[],
depth: number,
tag: string,
deprecated: boolean | undefined,
standard_track: boolean | undefined,
experimental: boolean | undefined,
identifier: string,
status: StatusFilters | null = null,
): IterableIterator<string> {
const { deprecated, standard_track, experimental } = status ?? {};
depth--;
if (depth >= 0) {
for (const i in obj) {
Expand Down Expand Up @@ -130,10 +133,8 @@ export function* iterateFeatures(
values,
depth,
tag,
deprecated,
standard_track,
experimental,
identifier + i + '.',
status,
);
}
}
Expand All @@ -147,10 +148,8 @@ 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
* @param status Whether to filter by status flags
* @returns An array of the features
*/
const traverseFeatures = (
Expand All @@ -159,23 +158,11 @@ const traverseFeatures = (
values: string[],
depth: number,
tag: string,
deprecated: boolean | undefined,
standard_track: boolean | undefined,
experimental: boolean | undefined,
identifier: string,
status: StatusFilters,
): string[] => {
const features = Array.from(
iterateFeatures(
obj,
browsers,
values,
depth,
tag,
deprecated,
standard_track,
experimental,
identifier,
),
iterateFeatures(obj, browsers, values, depth, tag, identifier, status),
);

return features.filter((item, pos) => features.indexOf(item) == pos);
Expand All @@ -188,9 +175,7 @@ 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
* @param status Whether to filter by status flags
* @returns The list of features
*/
const main = (
Expand All @@ -201,9 +186,7 @@ const main = (
values = ['null', 'true'],
depth = 100,
tag = '',
deprecated = undefined,
standard_track = undefined,
experimental = undefined,
status = {} as StatusFilters,
): string[] => {
const features: string[] = [];

Expand All @@ -215,10 +198,8 @@ const main = (
values,
depth,
tag,
deprecated,
standard_track,
experimental,
folders[folder] + '.',
status,
),
);
}
Expand Down Expand Up @@ -285,21 +266,21 @@ if (esMain(import.meta)) {
type: 'boolean',
default: process.stdout.isTTY,
})
.option('deprecated', {
.option('status.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', {
.option('status.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', {
.option('status.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.',
Expand Down Expand Up @@ -331,20 +312,20 @@ if (esMain(import.meta)) {
'Find all features with no tags.',
)
.example(
'npm run traverse -- --deprecated',
'npm run traverse -- --status.deprecated',
'Find all features that are deprecated.',
)
.example(
'npm run traverse -- --no-deprecated',
'npm run traverse -- --no-status.deprecated',
'Omit all features that are deprecated.',
)
.example(
'npm run traverse -- --standard_track',
'npm run traverse -- --status.standard_track',
'Find all features that are on the standard track.',
)
.example(
'npm run traverse -- --experimental',
'Omit all features that are deprecated.',
'npm run traverse -- --status.experimental',
'Find all features that are experimental.',
);
},
);
Expand All @@ -357,9 +338,7 @@ if (esMain(import.meta)) {
filter,
argv.depth,
argv.tag,
argv.deprecated,
argv.standard_track,
argv.experimental,
argv.status,
);
console.log(features.join('\n'));
if (argv.showCount) {
Expand Down

0 comments on commit 68e1b14

Please sign in to comment.