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 all 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 fixStatus from './status.js';

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

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

continue;
Expand Down
38 changes: 38 additions & 0 deletions scripts/fix/status.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. */

import { readFileSync, writeFileSync } from 'node:fs';

import { IS_WINDOWS } from '../../test/utils.js';

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

/**
* @param {Promise<void>} filename
*/
const fixStatus = (filename) => {
let actual = readFileSync(filename, 'utf-8').trim();
let expected = JSON.stringify(
JSON.parse(actual, fixStatusContradiction),
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 fixStatus;
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,
testStatus,
testStyle,
testVersions,
} from './linter/index.js';
Expand Down Expand Up @@ -95,6 +96,7 @@ const checkFiles = (...files) => {
testConsistency(fileData);
testDescriptions(fileData);
testPrefix(fileData, filePath);
testStatus(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 testStatus from './test-status.js';
import testStyle from './test-style.js';
import testVersions from './test-versions.js';

Expand All @@ -21,6 +22,7 @@ export {
testNotes,
testPrefix,
testSchema,
testStatus,
testStyle,
testVersions,
};
46 changes: 46 additions & 0 deletions test/linter/test-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* 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(
'.',
)}}`,
chalk`Run {bold npm run fix} to fix this issue automatically`,
);
}
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) {
const logger = new Logger('Status');

checkStatus(data, logger);

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

export default testStatusContradiction;