Skip to content

Commit

Permalink
Add snapshots as YAML files to track name/spec (#660)
Browse files Browse the repository at this point in the history
This also ensures that only snapshots which are defined can be used by a
feature.

Snapshots are still not published, and this does not affect the built
JSON at all.
  • Loading branch information
foolip authored Mar 8, 2024
1 parent 0435f36 commit acfe146
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 13 deletions.
46 changes: 33 additions & 13 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,43 @@ const omittables = [
"group"
]

function scrub(data: FeatureData) {
function scrub(data: any) {
for (const key of omittables) {
delete data[key];
}
return data;
return data as FeatureData;
}

const filePaths = new fdir()
.withBasePath()
.filter((fp) => fp.endsWith('.yml'))
.crawl('feature-group-definitions')
.sync() as string[];
function* yamlEntries(root: string): Generator<[string, any]> {
const filePaths = new fdir()
.withBasePath()
.filter((fp) => fp.endsWith('.yml'))
.crawl(root)
.sync() as string[];

const features: { [key: string]: FeatureData } = {};
for (const fp of filePaths) {
// The feature identifier/key is the filename without extension.
const key = path.parse(fp).name;

const src = fs.readFileSync(fp, { encoding: 'utf-8'});
const data = YAML.parse(src);

yield [key, data];
}
}

for (const fp of filePaths) {
// The feature identifier/key is the filename without extension.
const key = path.parse(fp).name;
// Load snapshots first so that snapshot identifiers can be validated while
// loading features.
const snapshots: { [key: string]: any } = {};

const src = fs.readFileSync(fp, { encoding: 'utf-8'});
const data = YAML.parse(src) as FeatureData;
for (const [key, data] of yamlEntries('snapshots')) {
// Note that the data is unused and unvalidated for now.
snapshots[key] = data;
}

const features: { [key: string]: FeatureData } = {};

for (const [key, data] of yamlEntries('feature-group-definitions')) {
// Compute Baseline high date from low date.
if (data.status?.baseline_high_date) {
throw new Error(`baseline_high_date is computed and should not be used in source YAML. Remove it from ${key}.yml.`);
Expand All @@ -50,6 +65,11 @@ for (const fp of filePaths) {
data.status.baseline_high_date = String(highDate);
}

// Ensure that only known snapshot identifiers are used.
if (data.snapshot && !Object.hasOwn(snapshots, data.snapshot)) {
throw new Error(`snapshot ${data.snapshot} used in ${key}.yml is not a valid snapshot. Add it to snapshots/ if needed.`);
}

features[key] = scrub(data);
}

Expand Down
2 changes: 2 additions & 0 deletions snapshots/ecmascript-1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: ECMAScript
spec: https://ecma-international.org/wp-content/uploads/ECMA-262_1st_edition_june_1997.pdf
2 changes: 2 additions & 0 deletions snapshots/ecmascript-2015.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: ECMAScript 2015
spec: https://262.ecma-international.org/6.0/
2 changes: 2 additions & 0 deletions snapshots/ecmascript-2016.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: ECMAScript 2016
spec: https://262.ecma-international.org/7.0/
2 changes: 2 additions & 0 deletions snapshots/ecmascript-2017.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: ECMAScript 2017
spec: https://262.ecma-international.org/8.0/
2 changes: 2 additions & 0 deletions snapshots/ecmascript-2018.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: ECMAScript 2018
spec: https://262.ecma-international.org/9.0/
2 changes: 2 additions & 0 deletions snapshots/ecmascript-2019.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: ECMAScript 2019
spec: https://262.ecma-international.org/10.0/
2 changes: 2 additions & 0 deletions snapshots/ecmascript-2020.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: ECMAScript 2020
spec: https://262.ecma-international.org/11.0/
2 changes: 2 additions & 0 deletions snapshots/ecmascript-2021.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: ECMAScript 2021
spec: https://262.ecma-international.org/12.0/
2 changes: 2 additions & 0 deletions snapshots/ecmascript-2022.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: ECMAScript 2022
spec: https://262.ecma-international.org/13.0/
2 changes: 2 additions & 0 deletions snapshots/ecmascript-2023.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: ECMAScript 2023
spec: https://262.ecma-international.org/14.0/
2 changes: 2 additions & 0 deletions snapshots/ecmascript-5.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: ECMAScript 5.1
spec: https://262.ecma-international.org/5.1/

0 comments on commit acfe146

Please sign in to comment.