Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslint-rule-composer

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-rule-composer - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

CHANGELOG.md

91

lib/rule-composer.js

@@ -54,10 +54,18 @@ 'use strict';

* @param {MessageDescriptor} descriptor The report message descriptor.
* @returns {string} The interpolated message for the descriptor
* @param {Object} messageIds Message IDs from rule metadata
* @returns {{message: string, data: Object}} The interpolated message and data for the descriptor
*/
function normalizeMessagePlaceholders(descriptor) {
function normalizeMessagePlaceholders(descriptor, messageIds) {
const message = typeof descriptor.messageId === 'string' ? messageIds[descriptor.messageId] : descriptor.message;
if (!descriptor.data) {
return descriptor.message;
return {
message,
data: typeof descriptor.messageId === 'string' ? {} : null,
};
}
return descriptor.message.replace(/\{\{\s*([^{}]+?)\s*\}\}/g, (fullMatch, term) => {
const normalizedData = Object.create(null);
const interpolatedMessage = message.replace(/\{\{\s*([^{}]+?)\s*\}\}/g, (fullMatch, term) => {
if (term in descriptor.data) {
normalizedData[term] = descriptor.data[term];
return descriptor.data[term];

@@ -68,14 +76,40 @@ }

});
}
function normalizeReport() {
const descriptor = normalizeMultiArgReportCall.apply(null, arguments);
return {
node: descriptor.node,
message: normalizeMessagePlaceholders(descriptor),
loc: normalizeReportLoc(descriptor),
fix: descriptor.fix,
message: interpolatedMessage,
data: Object.freeze(normalizedData),
};
}
function getRuleMeta(rule) {
return typeof rule === 'object' && rule.meta && typeof rule.meta === 'object'
? rule.meta
: {};
}
function getMessageIds(rule) {
const meta = getRuleMeta(rule);
return meta.messages && typeof rule.meta.messages === 'object'
? meta.messages
: {};
}
function getReportNormalizer(rule) {
const messageIds = getMessageIds(rule);
return function normalizeReport() {
const descriptor = normalizeMultiArgReportCall.apply(null, arguments);
const interpolatedMessageAndData = normalizeMessagePlaceholders(descriptor, messageIds);
return {
node: descriptor.node,
message: interpolatedMessageAndData.message,
messageId: typeof descriptor.messageId === 'string' ? descriptor.messageId : null,
data: typeof descriptor.messageId === 'string' ? interpolatedMessageAndData.data : null,
loc: normalizeReportLoc(descriptor),
fix: descriptor.fix,
};
};
}
function getRuleCreateFunc(rule) {

@@ -85,2 +119,12 @@ return typeof rule === 'function' ? rule : rule.create;

function removeMessageIfMessageIdPresent(reportDescriptor) {
const newDescriptor = Object.assign({}, reportDescriptor);
if (typeof reportDescriptor.messageId === 'string' && typeof reportDescriptor.message === 'string') {
delete newDescriptor.message;
}
return newDescriptor;
}
module.exports = Object.freeze({

@@ -99,5 +143,5 @@ filterReports(rule, predicate) {

value() {
const reportDescriptor = normalizeReport.apply(null, arguments);
const reportDescriptor = getReportNormalizer(rule).apply(null, arguments);
if (predicate(reportDescriptor, { sourceCode })) {
context.report(reportDescriptor);
context.report(removeMessageIfMessageIdPresent(reportDescriptor));
}

@@ -112,3 +156,3 @@ },

schema: rule.schema,
meta: rule.meta,
meta: getRuleMeta(rule),
});

@@ -128,3 +172,10 @@ },

value() {
context.report(iteratee(normalizeReport.apply(null, arguments), { sourceCode }));
context.report(
removeMessageIfMessageIdPresent(
iteratee(
getReportNormalizer(rule).apply(null, arguments),
{ sourceCode }
)
)
);
},

@@ -138,3 +189,3 @@ },

schema: rule.schema,
meta: rule.meta,
meta: getRuleMeta(rule),
});

@@ -168,5 +219,11 @@ },

},
meta: { fixable: 'code' },
meta: Object.freeze({
messages: Object.assign.apply(
null,
[Object.create(null)].concat(rules.map(getMessageIds))
),
fixable: 'code',
}),
});
},
});

6

package.json
{
"name": "eslint-rule-composer",
"version": "0.1.0",
"version": "0.1.1",
"description": "A utility for composing ESLint rules from other ESLint rules",

@@ -11,3 +11,4 @@ "main": "lib/rule-composer.js",

"lint": "eslint lib/ tests/",
"test": "npm run lint && mocha tests/**/*.js"
"test": "npm run lint && mocha tests/**/*.js",
"generate-release": "node-release-script"
},

@@ -28,2 +29,3 @@ "repository": {

"devDependencies": {
"@not-an-aardvark/node-release-script": "^0.1.0",
"chai": "^4.1.2",

@@ -30,0 +32,0 @@ "eslint": "^4.7.1",

@@ -49,3 +49,3 @@ # eslint-rule-composer

### `ruleComposer.filterReports(rule, predicate) and ruleComposer.mapReports(rule, predicate)`
### `ruleComposer.filterReports(rule, predicate)` and `ruleComposer.mapReports(rule, predicate)`

@@ -65,2 +65,4 @@ Both of these functions accept two arguments: `rule` (an ESLint rule object) and `predicate` (a function)

message: string,
messageId: string | null,
data: Object | null,
loc: {

@@ -74,2 +76,5 @@ start: { line: number, column: number },

Note that the `messageId` and `data` properties will only be present if the original rule reported a problem using [Message IDs](https://eslint.org/docs/developer-guide/working-with-rules#messageids), otherwise they will be null.
When returning a descriptor with `mapReports`, the `messageId` property on the returned descriptor will be used to generate the new message. To modify a report message directly for a rule that uses message IDs, ensure that the `predicate` function returns an object without a `messageId` property.
* `metadata` is an object containing information about the source text that was linted. This has a `sourceCode` property, which is a [`SourceCode`](https://eslint.org/docs/developer-guide/working-with-rules#contextgetsourcecode) instance corresponding to the linted text.

@@ -76,0 +81,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc