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

Mark deprecated rules as deprecated in their metadata #911

Merged
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
57 changes: 32 additions & 25 deletions index.js
@@ -1,12 +1,6 @@
'use strict';

var deprecatedRules = {
'no-comment-textnodes': require('./lib/rules/no-comment-textnodes'),
'require-extension': require('./lib/rules/require-extension'),
'wrap-multilines': require('./lib/rules/wrap-multilines')
};

var rules = {
var allRules = {
'jsx-uses-react': require('./lib/rules/jsx-uses-react'),
'no-multi-comp': require('./lib/rules/no-multi-comp'),
'prop-types': require('./lib/rules/prop-types'),
Expand Down Expand Up @@ -58,32 +52,45 @@ var rules = {
'no-danger-with-children': require('./lib/rules/no-danger-with-children'),
'style-prop-object': require('./lib/rules/style-prop-object'),
'no-unused-prop-types': require('./lib/rules/no-unused-prop-types'),
'no-children-prop': require('./lib/rules/no-children-prop')
'no-children-prop': require('./lib/rules/no-children-prop'),
'no-comment-textnodes': require('./lib/rules/no-comment-textnodes'),
'require-extension': require('./lib/rules/require-extension'),
'wrap-multilines': require('./lib/rules/wrap-multilines')
};

var ruleNames = Object.keys(rules);
var allRules = {};
for (var i = 0; i < ruleNames.length; i++) {
allRules['react/' + ruleNames[i]] = 2;
}

var exportedRules = {};
for (var key in rules) {
if (!rules.hasOwnProperty(key)) {
continue;
function filterRules(rules, predicate) {
var result = {};
for (var key in rules) {
if (rules.hasOwnProperty(key) && predicate(rules[key])) {
result[key] = rules[key];
}
}
exportedRules[key] = rules[key];
return result;
}
for (var deprecatedKey in deprecatedRules) {
if (!deprecatedRules.hasOwnProperty(deprecatedKey)) {
continue;

function configureAsError(rules) {
var result = {};
for (var key in rules) {
if (!rules.hasOwnProperty(key)) {
continue;
}
result['react/' + key] = 2;
}
exportedRules[deprecatedKey] = deprecatedRules[deprecatedKey];
return result;
}

var activeRules = filterRules(allRules, function(rule) {
return !rule.meta.deprecated;
});
var activeRulesConfig = configureAsError(activeRules);

var deprecatedRules = filterRules(allRules, function(rule) {
return rule.meta.deprecated;
Copy link
Member

Choose a reason for hiding this comment

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

this seems like the react/ prefix won't actually be there for the deprecated rules?

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 don't believe it needs to be. It wasn't before - deprecatedRules was defined directly as:

var deprecatedRules = {
  'no-comment-textnodes': require('./lib/rules/no-comment-textnodes'),
  'require-extension': require('./lib/rules/require-extension'),
  'wrap-multilines': require('./lib/rules/wrap-multilines')
};

The react/ prefix is only needed for activeRulesConfig, because those are used to define the all configuration, whereas rules and deprecatedRules both use the un-prefixed names.

Copy link
Member

Choose a reason for hiding this comment

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

That seems odd to me but I believe you that the behavior won't be changed by this PR - so while I think all the exported rule name should have the prefix, that clearly is out of scope of this PR. Thanks for clarifying!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here's the relevant code from ESLint (from https://github.com/eslint/eslint/blob/master/lib/rules.js#L54-L63):

function importPlugin(plugin, pluginName) {
    if (plugin.rules) {
        Object.keys(plugin.rules).forEach(function(ruleId) {
            const qualifiedRuleId = `${pluginName}/${ruleId}`,
                rule = plugin.rules[ruleId];

            define(qualifiedRuleId, rule);
        });
    }
}

You can see that it takes the responsibility of prefixing the plugin name.

Copy link
Member

Choose a reason for hiding this comment

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

hm, that's a good point. thanks

});

module.exports = {
deprecatedRules: deprecatedRules,
rules: exportedRules,
rules: allRules,
configs: {
recommended: {
parserOptions: {
Expand Down Expand Up @@ -114,7 +121,7 @@ module.exports = {
jsx: true
}
},
rules: allRules
rules: activeRulesConfig
}
}
};
1 change: 1 addition & 0 deletions lib/rules/no-comment-textnodes.js
Expand Up @@ -15,6 +15,7 @@ var isWarnedForDeprecation = false;

module.exports = {
meta: {
deprecated: true,
docs: {
description: 'Comments inside children section of tag should be placed inside braces',
category: 'Possible Errors',
Expand Down
1 change: 1 addition & 0 deletions lib/rules/require-extension.js
Expand Up @@ -24,6 +24,7 @@ var PKG_REGEX = /^[^\.]((?!\/).)*$/;

module.exports = {
meta: {
deprecated: true,
docs: {
description: 'Restrict file extensions that may be required',
category: 'Stylistic Issues',
Expand Down
1 change: 1 addition & 0 deletions lib/rules/wrap-multilines.js
Expand Up @@ -15,6 +15,7 @@ var isWarnedForDeprecation = false;

module.exports = {
meta: {
deprecated: true,
docs: {
description: 'Prevent missing parentheses around multilines JSX',
category: 'Stylistic Issues',
Expand Down
14 changes: 14 additions & 0 deletions tests/index.js
Expand Up @@ -23,6 +23,20 @@ describe('all rule files should be exported by the plugin', function() {
});
});

describe('deprecated rules', function() {
it('marks all deprecated rules as deprecated', function() {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should just use the deprecated property to dynamically build up the deprecatedRules list?

Copy link
Member

Choose a reason for hiding this comment

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

lol i see this in your OP now. yeah i think it might be worth it - getAllRules().filter(x => x.deprecated) seems like it should be a viable approach

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't have a getAllRules() function here, but I'll take another run at this and see what I can come up with. I agree that the duplicated knowledge of which rules are deprecated is not ideal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ljharb OK, here's what I came up with: 33aa4d5. Let me know what you think.

ruleFiles.forEach(function(ruleName) {
var inDeprecatedRules = Boolean(plugin.deprecatedRules[ruleName]);
var isDeprecated = plugin.rules[ruleName].meta.deprecated;
if (inDeprecatedRules) {
assert(isDeprecated, ruleName + ' metadata should mark it as deprecated');
} else {
assert(!isDeprecated, ruleName + ' metadata should not mark it as deprecated');
}
});
});
});

describe('configurations', function() {
it('should export a \'recommended\' configuration', function() {
assert(plugin.configs.recommended);
Expand Down