(node)
| 10 | create: function(context) { |
| 11 | return { |
| 12 | ObjectExpression(node) { |
| 13 | // Not adding __proto__ to module.exports as it will break a lot of libraries |
| 14 | if (isModuleExportsObject(node) || isModifiedExports(node)) { |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | const properties = node.properties; |
| 19 | let hasProto = false; |
| 20 | |
| 21 | for (const property of properties) { |
| 22 | |
| 23 | if (!property.key) { |
| 24 | continue; |
| 25 | } |
| 26 | |
| 27 | if (property.key.type === 'Identifier' && property.key.name === '__proto__') { |
| 28 | hasProto = true; |
| 29 | break; |
| 30 | } |
| 31 | |
| 32 | if (property.key.type === 'Literal' && property.key.value === '__proto__') { |
| 33 | hasProto = true; |
| 34 | break; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if (hasProto) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | if (properties.length > 0) { |
| 43 | // If the object has properties but no __proto__ property |
| 44 | context.report({ |
| 45 | node, |
| 46 | message: 'Every object must have __proto__: null', |
| 47 | fix: function(fixer) { |
| 48 | // Generate the fix suggestion to add __proto__: null |
| 49 | const sourceCode = context.sourceCode; |
| 50 | const firstProperty = properties[0]; |
| 51 | const firstPropertyToken = sourceCode.getFirstToken(firstProperty); |
| 52 | |
| 53 | |
| 54 | const isMultiLine = properties.length === 1 ? |
| 55 | // If the object has only one property, |
| 56 | // it's multiline if the property is not on the same line as the object parenthesis |
| 57 | properties[0].loc.start.line !== node.loc.start.line : |
| 58 | // If the object has more than one property, |
| 59 | // it's multiline if the first and second properties are not on the same line |
| 60 | properties[0].loc.start.line !== properties[1].loc.start.line; |
| 61 | |
| 62 | const fixText = `__proto__: null,${isMultiLine ? '\n' : ' '}`; |
| 63 | |
| 64 | // Insert the fix suggestion before the first property |
| 65 | return fixer.insertTextBefore(firstPropertyToken, fixText); |
| 66 | }, |
| 67 | }); |
| 68 | } |
| 69 |
nothing calls this directly
no test coverage detected
searching dependent graphs…