Skip to content

Commit

Permalink
Use strict mode for the linter
Browse files Browse the repository at this point in the history
  Also fixes the issue where an error in a single file would cause
all subsequent files to be printed at the end.
  • Loading branch information
ExE-Boss committed Jun 11, 2018
1 parent 98ffd44 commit c5c3045
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 54 deletions.
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
var fs = require('fs'),
path = require('path'),
extend = require('extend');
'use strict';
const fs = require('fs');
const path = require('path');
const extend = require('extend');

function load() {
// Recursively load one or more directories passed as arguments.
var dir, result = {};
let dir, result = {};

function processFilename(fn) {
let fp = path.join(dir, fn);
const fp = path.join(dir, fn);
let extra;

// If the given filename is a directory, recursively load it.
Expand Down
47 changes: 29 additions & 18 deletions test/lint.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
var fs = require('fs');
var path = require('path');
var {testStyle} = require('./test-style');
var {testSchema} = require('./test-schema');
var {testVersions} = require('./test-versions');
var hasErrors, hasStyleErrors, hasSchemaErrors, hasVersionErrors = false;
var filesWithErrors = {};
'use strict';
const fs = require('fs');
const path = require('path');
const {testStyle} = require('./test-style');
const {testSchema} = require('./test-schema');
const {testVersions} = require('./test-versions');
/** @type {Map<string,string>} */
const filesWithErrors = new Map();

let hasErrors = false;

/**
* @param {string[]} files
*/
function load(...files) {
for (let file of files) {
if (file.indexOf(__dirname) !== 0) {
file = path.resolve(__dirname, '..', file);
}

if (!fs.existsSync(file)) {
continue; // Ignore non-existent files
}

if (fs.statSync(file).isFile()) {
if (path.extname(file) === '.json') {
let hasStyleErrors, hasSchemaErrors, hasVersionErrors = false;
console.log(file.replace(path.resolve(__dirname, '..') + path.sep, ''));
if (file.indexOf('browsers' + path.sep) !== -1) {
hasSchemaErrors = testSchema(file, './../schemas/browsers.schema.json');
Expand All @@ -24,15 +35,15 @@ function load(...files) {
}
if (hasStyleErrors || hasSchemaErrors || hasVersionErrors) {
hasErrors = true;
fileName = file.replace(path.resolve(__dirname, '..') + path.sep, '');
filesWithErrors[fileName] = file;
const fileName = file.replace(path.resolve(__dirname, '..') + path.sep, '');
filesWithErrors.set(fileName, file);
}
}

continue;
}

let subFiles = fs.readdirSync(file).map((subfile) => {
const subFiles = fs.readdirSync(file).map((subfile) => {
return path.join(file, subfile);
});

Expand All @@ -41,7 +52,7 @@ function load(...files) {
}

if (process.argv[2]) {
load(process.argv[2])
load(process.argv[2]);
} else {
load(
'api',
Expand All @@ -59,13 +70,13 @@ if (process.argv[2]) {
}

if (hasErrors) {
console.log("");
console.log(`Problems in ${Object.keys(filesWithErrors).length} files:`);
for (let file in filesWithErrors) {
console.log(file);
testSchema(filesWithErrors[file]);
testStyle(filesWithErrors[file]);
testVersions(filesWithErrors[file]);
console.log('');
console.warn(`Problems in ${filesWithErrors.size} file${filesWithErrors.size > 1 ? 's' : ''}:`);
for (const [fileName, file] of filesWithErrors) {
console.log(fileName);
testSchema(file);
testStyle(file);
testVersions(file);
}
process.exit(1);
}
5 changes: 3 additions & 2 deletions test/test-schema.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Ajv = require('ajv');
var ajv = new Ajv({ allErrors: true });
'use strict';
const Ajv = require('ajv');
const ajv = new Ajv({ allErrors: true });

function testSchema(dataFilename, schemaFilename = './../schemas/compat-data.schema.json') {
var valid = ajv.validate(
Expand Down
28 changes: 14 additions & 14 deletions test/test-style.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var fs = require('fs');
var path = require('path');
var hasErrors = false;
'use strict';
const fs = require('fs');
let hasErrors = false;

function jsonDiff(actual, expected) {
var actualLines = actual.split(/\n/);
Expand All @@ -18,13 +18,13 @@ function jsonDiff(actual, expected) {
}

function testStyle(filename) {
var actual = fs.readFileSync(filename, 'utf-8').trim();
var expected = JSON.stringify(JSON.parse(actual), null, 2);
let actual = fs.readFileSync(filename, 'utf-8').trim();
let expected = JSON.stringify(JSON.parse(actual), null, 2);

var platform = require("os").platform;
if (platform() === "win32") { // prevent false positives from git.core.autocrlf on Windows
actual = actual.replace(/\r/g, "");
expected = expected.replace(/\r/g, "");
const {platform} = require('os');
if (platform() === 'win32') { // prevent false positives from git.core.autocrlf on Windows
actual = actual.replace(/\r/g, '');
expected = expected.replace(/\r/g, '');
}

if (actual === expected) {
Expand All @@ -35,23 +35,23 @@ function testStyle(filename) {
console.error('\x1b[31m Style – Error on line ' + jsonDiff(actual, expected));
}

let bugzillaMatch = actual.match(String.raw`https?://bugzilla\.mozilla\.org/show_bug\.cgi\?id=(\d+)`);
const bugzillaMatch = actual.match(String.raw`https?://bugzilla\.mozilla\.org/show_bug\.cgi\?id=(\d+)`);
if (bugzillaMatch) {
// use https://bugzil.la/1000000 instead
hasErrors = true;
console.error('\x1b[33m Style – Use shortenable URL (%s → https://bugzil.la/%s).\x1b[0m', bugzillaMatch[0],
bugzillaMatch[1]);
}

let crbugMatch = actual.match(String.raw`https?://bugs\.chromium\.org/p/chromium/issues/detail\?id=(\d+)`);
const crbugMatch = actual.match(String.raw`https?://bugs\.chromium\.org/p/chromium/issues/detail\?id=(\d+)`);
if (crbugMatch) {
// use https://crbug.com/100000 instead
hasErrors = true;
console.error('\x1b[33m Style – Use shortenable URL (%s → https://crbug.com/%s).\x1b[0m', crbugMatch[0],
crbugMatch[1]);
}

let mdnUrlMatch = actual.match(String.raw`https?://developer.mozilla.org/(\w\w-\w\w)/(.*?)(?=["'\s])`)
const mdnUrlMatch = actual.match(String.raw`https?://developer.mozilla.org/(\w\w-\w\w)/(.*?)(?=["'\s])`);
if (mdnUrlMatch) {
hasErrors = true;
console.error(
Expand All @@ -60,9 +60,9 @@ function testStyle(filename) {
mdnUrlMatch[2]);
}

if (actual.includes("href=\\\"")) {
if (actual.includes('href=\\"')) {
hasErrors = true;
console.error('\x1b[33m Style – Found \\\" but expected \' for <a href>.\x1b[0m');
console.error('\x1b[33m Style – Found \\" but expected \' for <a href>.\x1b[0m');
}

return hasErrors;
Expand Down
31 changes: 16 additions & 15 deletions test/test-versions.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
var browsers = require('..').browsers;
'use strict';
const {browsers} = require('..');

var validBrowserVersions = {};
for (let browser of Object.keys(browsers)) {
const validBrowserVersions = {};
for (const browser of Object.keys(browsers)) {
validBrowserVersions[browser] = Object.keys(browsers[browser].releases);
}

function isValidVersion(browserIdentifier, version) {
if (typeof version === "string") {
if (typeof version === 'string') {
return validBrowserVersions[browserIdentifier].includes(version);
} else {
return true;
}
}

function testVersions(dataFilename) {
var hasErrors = false;
var data = require(dataFilename);
const data = require(dataFilename);
let hasErrors = false;

function checkVersions(supportData) {
var browsersToCheck = Object.keys(supportData);
for (let browser of browsersToCheck) {
const browsersToCheck = Object.keys(supportData);
for (const browser of browsersToCheck) {
if (validBrowserVersions[browser]) {

let supportStatements = [];
const supportStatements = [];
if (Array.isArray(supportData[browser])) {
Array.prototype.push.apply(supportStatements, supportData[browser]);
} else {
supportStatements.push(supportData[browser]);
}

for (let statement of supportStatements) {
for (const statement of supportStatements) {
if (!isValidVersion(browser, statement.version_added)) {
console.error('\x1b[31m version_added: "' + statement.version_added + '" is not a valid version number for ' + browser);
console.error(' Valid ' + browser + ' versions are: ' + validBrowserVersions[browser].join(', '));
Expand All @@ -40,8 +41,8 @@ function testVersions(dataFilename) {
console.error(' Valid ' + browser + ' versions are: ' + validBrowserVersions[browser].join(', '));
hasErrors = true;
}
if ("version_removed" in statement && "version_added" in statement) {
if (typeof statement.version_added !== "string" && statement.version_added !== true) {
if ('version_removed' in statement && 'version_added' in statement) {
if (typeof statement.version_added !== 'string' && statement.version_added !== true) {
console.error('\x1b[31m version_added: "' + statement.version_added + '" is not a valid version number when version_removed is present');
console.error(' Valid', browser, 'versions are:', validBrowserVersions[browser].length > 0 ? 'true, ' + validBrowserVersions[browser].join(', ') : 'true');
hasErrors = true;
Expand All @@ -53,12 +54,12 @@ function testVersions(dataFilename) {
}

function findSupport(data) {
for (var prop in data) {
for (const prop in data) {
if (prop === 'support') {
checkVersions(data[prop]);
}
var sub = data[prop];
if (typeof(sub) === "object") {
const sub = data[prop];
if (typeof(sub) === 'object') {
findSupport(sub);
}
}
Expand Down

0 comments on commit c5c3045

Please sign in to comment.