Skip to content

Commit

Permalink
fix eslint rule to add multiline when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
rluvaton committed Jul 4, 2023
1 parent 00b718b commit 1a91514
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions tools/eslint-rules/set-proto-to-null-in-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
},
fixable: 'code',
},
create: function(context) {
create: function (context) {
return {
ObjectExpression(node) {
// Not adding __proto__ to module.exports as it will break a lot of libraries
Expand Down Expand Up @@ -44,12 +44,23 @@ module.exports = {
context.report({
node,
message: 'Every object must have __proto__: null',
fix: function(fixer) {
fix: function (fixer) {
// Generate the fix suggestion to add __proto__: null
const sourceCode = context.getSourceCode();
const firstProperty = properties[0];
const firstPropertyToken = sourceCode.getFirstToken(firstProperty);
const fixText = '__proto__: null, ';

let fixText = `__proto__: null`;

if (properties.length > 1) {
fixText += ',';

if (properties[0].loc.start.line !== properties[1].loc.start.line) {
fixText += '\n';
} else {
fixText += ' ';
}
}

// Insert the fix suggestion before the first property
return fixer.insertTextBefore(firstPropertyToken, fixText);
Expand All @@ -62,7 +73,7 @@ module.exports = {
context.report({
node,
message: 'Every empty object must have __proto__: null',
fix: function(fixer) {
fix: function (fixer) {
// Generate the fix suggestion to create the object with __proto__: null
const fixText = '{ __proto__: null }';

Expand Down

0 comments on commit 1a91514

Please sign in to comment.