Skip to content

Commit

Permalink
feat(TypeScript): Update types
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Apr 9, 2019
1 parent 2401a76 commit 38364fa
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
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
23 changes: 19 additions & 4 deletions test/test-versions.js
@@ -1,13 +1,19 @@
'use strict';
const path = require('path');
const browsers = require('..').browsers;
const compareVersions = require('compare-versions');
/** @typedef {import('../types')} bcd */
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 {bcd.VersionValue} version
*/
function isValidVersion(browserIdentifier, version) {
if (typeof version === "string") {
return validBrowserVersions[browserIdentifier].includes(version);
Expand All @@ -16,15 +22,21 @@ function isValidVersion(browserIdentifier, version) {
}
}

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

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

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

/**
* @param {bcd.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);
}
const sub = data[prop];
if (typeof(sub) === "object") {
Expand Down
8 changes: 8 additions & 0 deletions types.d.ts
Expand Up @@ -241,6 +241,8 @@ export interface CompatStatement {
*/
mdn_url?: string;

matches?: MatchesBlock;

/**
* Each `__compat` object contains support information.
*
Expand All @@ -262,6 +264,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

0 comments on commit 38364fa

Please sign in to comment.