Skip to content

Fix: Improve detection of static url strings in require-meta-docs-url rule #162

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
65 changes: 37 additions & 28 deletions lib/rules/require-meta-docs-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

const path = require('path');
const util = require('../utils');
const { getStaticValue } = require('eslint-utils');

// -----------------------------------------------------------------------------
// Rule Definition
Expand All @@ -31,6 +32,11 @@ module.exports = {
},
additionalProperties: false,
}],
messages: {
mismatch: '`meta.docs.url` property must be `{{expectedUrl}}`.',
missing: 'Rules should export a `meta.docs.url` property.',
wrongType: '`meta.docs.url` property must be a string.',
},
},

/**
Expand All @@ -48,24 +54,22 @@ module.exports = {
: options.pattern.replace(/{{\s*name\s*}}/g, ruleName);

/**
* Check whether a given node is the expected URL.
* @param {Node} node The node of property value to check.
* Check whether a given URL is the expected URL.
* @param {String} url The URL to check.
* @returns {boolean} `true` if the node is the expected URL.
*/
function isExpectedUrl (node) {
function isExpectedUrl (url) {
return Boolean(
node &&
node.type === 'Literal' &&
typeof node.value === 'string' &&
typeof url === 'string' &&
(
expectedUrl === undefined ||
node.value === expectedUrl
url === expectedUrl
)
);
}

return {
Program (node) {
Program () {
const info = util.getRuleInfo(sourceCode);
if (info === null) {
return;
Expand All @@ -81,40 +85,45 @@ module.exports = {
docsPropNode.value.properties &&
docsPropNode.value.properties.find(p => p.type === 'Property' && util.getKeyName(p) === 'url');

if (isExpectedUrl(urlPropNode && urlPropNode.value)) {
const staticValue = urlPropNode ? getStaticValue(urlPropNode.value, context.getScope()) : undefined;
if (urlPropNode && !staticValue) {
// Ignore non-static values since we can't determine what they look like.
return;
}

if (isExpectedUrl(staticValue && staticValue.value)) {
return;
}

context.report({
loc:
(urlPropNode && urlPropNode.value.loc) ||
(docsPropNode && docsPropNode.value.loc) ||
(metaNode && metaNode.loc) ||
node.loc.start,

message:
!urlPropNode ? 'Rules should export a `meta.docs.url` property.' :
node: (urlPropNode && urlPropNode.value) || (docsPropNode && docsPropNode.value) || metaNode || info.create,

messageId:
!urlPropNode ? 'missing' :
// eslint-disable-next-line unicorn/no-nested-ternary
!expectedUrl ? '`meta.docs.url` property must be a string.' :
/* otherwise */ '`meta.docs.url` property must be `{{expectedUrl}}`.',
!expectedUrl ? 'wrongType' :
/* otherwise */ 'mismatch',

data: {
expectedUrl,
},

fix (fixer) {
if (expectedUrl) {
const urlString = JSON.stringify(expectedUrl);
if (urlPropNode) {
if (!expectedUrl) {
return null;
}

const urlString = JSON.stringify(expectedUrl);
if (urlPropNode) {
if (urlPropNode.value.type === 'Literal' || (urlPropNode.value.type === 'Identifier' && urlPropNode.value.name === 'undefined')) {
return fixer.replaceText(urlPropNode.value, urlString);
}
if (docsPropNode && docsPropNode.value.type === 'ObjectExpression') {
return util.insertProperty(fixer, docsPropNode.value, `url: ${urlString}`, sourceCode);
}
if (!docsPropNode && metaNode && metaNode.type === 'ObjectExpression') {
return util.insertProperty(fixer, metaNode, `docs: {\nurl: ${urlString}\n}`, sourceCode);
}
} else if (docsPropNode && docsPropNode.value.type === 'ObjectExpression') {
return util.insertProperty(fixer, docsPropNode.value, `url: ${urlString}`, sourceCode);
} else if (!docsPropNode && metaNode && metaNode.type === 'ObjectExpression') {
return util.insertProperty(fixer, metaNode, `docs: {\nurl: ${urlString}\n}`, sourceCode);
}

return null;
},
});
Expand Down
Loading