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

Add linter for experimental+deprecated statuses #6813

Merged
merged 21 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/fix/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import esMain from 'es-main';
import fixBrowserOrder from './browser-order.js';
import fixFeatureOrder from './feature-order.js';
import fixLinks from './links.js';
import fixStatusContradiction from './status-contradiction.js';

const dirname = fileURLToPath(new URL('.', import.meta.url));

Expand All @@ -34,6 +35,7 @@ function load(...files) {
fixBrowserOrder(file);
fixFeatureOrder(file);
fixLinks(file);
fixStatusContradiction(file);
}

continue;
Expand Down
38 changes: 38 additions & 0 deletions scripts/fix/status-contradiction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */

'use strict';

import { readFileSync, writeFileSync } from 'fs';
import { platform } from 'os';

/** Determines if the OS is Windows */
const IS_WINDOWS = platform() === 'win32';

const fixStatus = (key, value) => {
const status = value?.__compat?.status;
if (status && status.experimental && status.deprecated) {
status.experimental = false;
}
return value;
};

/**
* @param {Promise<void>} filename
*/
const fixStatusContradiction = (filename) => {
let actual = readFileSync(filename, 'utf-8').trim();
let expected = JSON.stringify(JSON.parse(actual, fixStatus), null, 2);

if (IS_WINDOWS) {
// prevent false positives from git.core.autocrlf on Windows
actual = actual.replace(/\r/g, '');
expected = expected.replace(/\r/g, '');
}

if (actual !== expected) {
writeFileSync(filename, expected + '\n', 'utf-8');
}
};

export default fixStatusContradiction;
2 changes: 2 additions & 0 deletions test/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
testNotes,
testPrefix,
testSchema,
testStatusContradiction,
testStyle,
testVersions,
} from './linter/index.js';
Expand Down Expand Up @@ -97,6 +98,7 @@ const checkFiles = (...files) => {
testConsistency(fileData);
testDescriptions(fileData);
testPrefix(fileData, filePath);
testStatusContradiction(fileData);
testStyle(rawFileData);
testVersions(fileData);
testNotes(fileData);
Expand Down
2 changes: 2 additions & 0 deletions test/linter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import testLinks from './test-links.js';
import testNotes from './test-notes.js';
import testPrefix from './test-prefix.js';
import testSchema from './test-schema.js';
import testStatusContradiction from './test-status-contradiction.js';
import testStyle from './test-style.js';
import testVersions from './test-versions.js';

Expand All @@ -21,6 +22,7 @@ export {
testNotes,
testPrefix,
testSchema,
testStatusContradiction,
testStyle,
testVersions,
};
45 changes: 45 additions & 0 deletions test/linter/test-status-contradiction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */

import chalk from 'chalk-template';
import { Logger } from '../utils.js';

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

/**
* @param {Identifier} data
* @param {Logger} logger
*/
function checkStatus(data, logger, path = []) {
const status = data.__compat?.status;
if (status && status.experimental && status.deprecated) {
logger.error(
chalk`{red Unexpected simultaneous experimental and deprecated status in ${path.join(
'.',
)}}`,
saschanaz marked this conversation as resolved.
Show resolved Hide resolved
);
}
for (const member in data) {
if (member === '__compat') {
continue;
}
checkStatus(data[member], logger, [...path, member]);
}
}

/**
* @param {Identifier} data The contents of the file to test
* @returns {boolean} If the file contains errors
*/
function testStatusContradiction(data) {
saschanaz marked this conversation as resolved.
Show resolved Hide resolved
const logger = new Logger('Flag consistency');
saschanaz marked this conversation as resolved.
Show resolved Hide resolved

checkStatus(data, logger);

logger.emit();
return logger.hasErrors();
}

export default testStatusContradiction;