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 support for meta.url to enable links in terminal #5845

Merged
merged 5 commits into from Jan 22, 2022
Merged

Conversation

ybiquitous
Copy link
Member

@ybiquitous ybiquitous commented Jan 18, 2022

Which issue, if any, is this issue related to?

Closes #5842
Related to #5835 (comment)

Is there anything in the PR that needs further explanation?

This pull request adds the meta.url property to all built-in rules. For automation, I use a temporary script as below:

Run:

$ cd stylelint
$ node add-meta-to-stylelint-rules.js
...
$ npm run format

Script:

// add-meta-to-stylelint-rules.js
const fs = require('fs');
const globby = require('globby');

const files = globby.sync('lib/rules/*/index.js');

for (const file of files) {
	const content = fs.readFileSync(file).toString();

	const rule = file.replace(/lib\/rules\/([^/]+)\/index.js/, '$1');

	let newContent = content.replace(
		'module.exports = rule;',
		'rule.meta = meta;\nmodule.exports = rule;',
	);

	newContent = newContent.replace(
		/(const messages = ruleMessages\(.+?\);)/s,
		`$1

const meta = {
	url: 'https://stylelint.io/user-guide/rules/list/${rule}',
};`,
	);

	fs.writeFileSync(file, newContent);

	console.log(`${file} (${rule}) updated`); // eslint-disable-line no-console
}

@ybiquitous ybiquitous marked this pull request as ready for review January 18, 2022 15:39
Copy link
Member

@jeddy3 jeddy3 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks.

(Nice script to automate it!)

@jeddy3 jeddy3 mentioned this pull request Jan 20, 2022
6 tasks
@jeddy3
Copy link
Member

jeddy3 commented Jan 21, 2022

@ybiquitous When you have a mo, can you resolve the conflicts and then we'll merge this and #5845.

@ybiquitous
Copy link
Member Author

@jeddy3 Conflicts fixed!

Copy link
Member

@jeddy3 jeddy3 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@jeddy3 jeddy3 merged commit ee78325 into main Jan 22, 2022
@jeddy3 jeddy3 deleted the issue-5842 branch January 22, 2022 11:11
@ybiquitous
Copy link
Member Author

ybiquitous commented Jan 22, 2022

@jeddy3 Don't we need to add this change to CHANGELOG.md?

(EDIT: Please ignore this comment if no problems. I don't want to block any release progress 😃 )

@Reda011
Copy link

Reda011 commented Aug 6, 2022

Hello, does Plugins support meta.url ?

I developed as Document(https://stylelint.io/developer-guide/plugins) said, but it doesn't work. Such as:

eg: I changed something from 'stylelint-z-index-value-constraint'(https://github.com/kristerkari/stylelint-z-index-value-constraint)

`
const stylelint = require("stylelint");

const ruleName = "plugin/z-index-value-constraint";

const messages = stylelint.utils.ruleMessages(ruleName, {
largerThanMax: expected =>
Expected z-index to have maximum value of ${expected}.,
smallerThanMin: expected =>
Expected z-index to have minimum value of ${expected}.
});

const meta = {
url: 'https://www.google.com/', // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Here it is !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
};

function isNumber(value) {
return typeof value === "number";
}

function isNegative(value) {
return value < 0;
}

const _isNaN =
Number.isNaN ||
function (value) {
return value !== value;
};

function possibleValueTest(value) {
return isNumber(value) && !isNegative(value);
}

module.exports = stylelint.createPlugin(
ruleName,
function (options, secondary) {
return function (cssRoot, result) {
const validOptions = stylelint.utils.validateOptions(
result,
ruleName,
{
actual: options,
possible: {
min: possibleValueTest,
max: possibleValueTest
}
},
{
actual: secondary,
possible: {
ignoreValues: [isNumber]
},
optional: true
}
);

  if (!validOptions) {
    return;
  }

  cssRoot.walkDecls("z-index", function (decl) {
    const value = Number(decl.value);

    if (
      _isNaN(value) ||
      (secondary &&
        Array.isArray(secondary.ignoreValues) &&
        secondary.ignoreValues.indexOf(value) > -1)
    ) {
      return;
    }

    if (options.max && Math.abs(value) > options.max) {
      stylelint.utils.report({
        ruleName,
        result,
        node: decl,
        message: messages.largerThanMax(
          isNegative(value) ? options.max * -1 : options.max
        )
      });
    }

    if (options.min && Math.abs(value) < options.min) {
      stylelint.utils.report({
        ruleName,
        result,
        node: decl,
        message: messages.smallerThanMin(
          isNegative(value) ? options.min * -1 : options.min
        )
      });
    }
  });
};

}
);

module.exports.ruleName = ruleName;
module.exports.messages = messages;
module.exports.meta = meta;

`

@ybiquitous
Copy link
Member Author

@Reda011 Thanks for the comment. Maybe, it's a bug. I'll fix it soon, but I appreciate it if you would open an issue.

@Reda011
Copy link

Reda011 commented Aug 7, 2022

@ybiquitous Ok, I have opened an issue:#6245

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Add support for meta.url to enable links in terminal
3 participants