Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(TypeScript): Update types #3739

Merged
merged 2 commits into from Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion test/test-browsers.js
@@ -1,6 +1,10 @@
'use strict';
const path = require('path');

/**
* @typedef {import('../types').Identifier} Identifier
*/

/** @type {Record<string, string[]>} */
const browsers = {
desktop: [
Expand Down Expand Up @@ -38,7 +42,7 @@ const browsers = {
};

/**
* @param {*} data
* @param {Identifier} data
* @param {string[]} displayBrowsers
* @param {string[]} requiredBrowsers
* @param {string} category
Expand Down Expand Up @@ -74,6 +78,7 @@ function processData(data, displayBrowsers, requiredBrowsers, category, logger,
function testBrowsers(filename) {
const relativePath = path.relative(path.resolve(__dirname, '..'), filename);
const category = relativePath.includes(path.sep) && relativePath.split(path.sep)[0];
/** @type {Identifier} */
const data = require(filename);

if (!category || category === "test") {
Expand Down
19 changes: 19 additions & 0 deletions test/test-regexes.js
@@ -1,13 +1,31 @@
const assert = require('assert');

/**
* @typedef {import('../types').Identifier} Identifier
*
* @typedef {object} TestCase
* @property {string[]} features
* @property {string[]} matches
* @property {string[]} misses
*/

/** @type {Identifier} */
const bcd = require('..');

/**
* @param {string} dottedFeature
*/
function lookup(dottedFeature) {
const x = dottedFeature.split('.');
const feature = x.reduce((prev, current) => prev[current], bcd);
return feature;
}

/**
* @param {Identifier} feature
* @param {string[]} matches
* @param {string[]} misses
*/
function testToken(feature, matches, misses) {
const str = feature.__compat.matches.regex_token || feature.__compat.matches.regex_value;
const regexp = new RegExp(str);
Expand All @@ -16,6 +34,7 @@ function testToken(feature, matches, misses) {
misses.forEach(miss => assert.ok(!regexp.test(miss), `${regexp} erroneously matched ${miss}`));
}

/** @type {TestCase[]} */
const tests = [
{
features: [
Expand Down
4 changes: 4 additions & 0 deletions test/test-schema.js
Expand Up @@ -5,6 +5,10 @@ const path = require('path');

const ajv = new Ajv({ jsonPointers: true, allErrors: true });

/**
* @param {string} dataFilename
* @param {string} [schemaFilename]
*/
function testSchema(dataFilename, schemaFilename = './../schemas/compat-data.schema.json') {
const schema = require(schemaFilename);
const data = require(dataFilename);
Expand Down
8 changes: 8 additions & 0 deletions test/test-style.js
Expand Up @@ -44,6 +44,11 @@ function escapeInvisibles(str) {
return finalString;
}

/**
* @param {string} actual
* @param {string} expected
* @return {string}
*/
function jsonDiff(actual, expected) {
var actualLines = actual.split(/\n/);
var expectedLines = expected.split(/\n/);
Expand All @@ -59,6 +64,9 @@ function jsonDiff(actual, expected) {
}
}

/**
* @param {string} filename
*/
function testStyle(filename) {
let hasErrors = false;
let actual = fs.readFileSync(filename, 'utf-8').trim();
Expand Down
28 changes: 24 additions & 4 deletions test/test-versions.js
@@ -1,13 +1,24 @@
'use strict';
const path = require('path');
const browsers = require('..').browsers;
const compareVersions = require('compare-versions');
/**
* @typedef {import('../types').Identifier} Identifier
* @typedef {import('../types').SimpleSupportStatement} SimpleSupportStatement
* @typedef {import('../types').SupportBlock} SupportBlock
* @typedef {import('../types').VersionValue} VersionValue
*/
const browsers = require('..').browsers;

/** @type {Object<string, string[]>} */
const validBrowserVersions = {};
for (const browser of Object.keys(browsers)) {
validBrowserVersions[browser] = Object.keys(browsers[browser].releases);
}

/**
* @param {string} browserIdentifier
* @param {VersionValue} version
*/
function isValidVersion(browserIdentifier, version) {
if (typeof version === "string") {
return validBrowserVersions[browserIdentifier].includes(version);
Expand All @@ -16,15 +27,21 @@ function isValidVersion(browserIdentifier, version) {
}
}

/**
* @param {string} dataFilename
*/
function testVersions(dataFilename) {
const data = require(dataFilename);
let hasErrors = false;

/**
* @param {SupportBlock} supportData
*/
function checkVersions(supportData) {
const browsersToCheck = Object.keys(supportData);
for (const browser of browsersToCheck) {
if (validBrowserVersions[browser]) {

/** @type {SimpleSupportStatement[]} */
const supportStatements = [];
if (Array.isArray(supportData[browser])) {
Array.prototype.push.apply(supportStatements, supportData[browser]);
Expand Down Expand Up @@ -58,10 +75,13 @@ function testVersions(dataFilename) {
}
}

/**
* @param {Identifier} data
*/
function findSupport(data) {
for (const prop in data) {
if (prop === 'support') {
checkVersions(data[prop]);
if (prop === '__compat' && data[prop].support) {
checkVersions(data[prop].support);
Copy link
Contributor Author

@ExE-Boss ExE-Boss Mar 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This operation was previously type unsafe, and could lead to a potential crash if there was a feature in BCD named support.

}
const sub = data[prop];
if (typeof(sub) === "object") {
Expand Down
10 changes: 8 additions & 2 deletions types.d.ts
@@ -1,8 +1,6 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */

export as namespace bcd;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I previously misunderstood what export as namespace meant.

I now know that this means export as a UMD global, which doesn’t exist.


/**
* The names of the known browsers.
*/
Expand Down Expand Up @@ -241,6 +239,8 @@ export interface CompatStatement {
*/
mdn_url?: string;

matches?: MatchesBlock;

/**
* Each `__compat` object contains support information.
*
Expand All @@ -262,6 +262,12 @@ export interface SupportBlock
extends Partial<Record<BrowserNames, SupportStatement>>,
Partial<Record<string, SupportStatement>> {}

export interface MatchesBlock {
keywords?: string[];
regex_token?: string;
regex_value?: string;
}

/**
* The status property contains information about stability of the feature.
*/
Expand Down