| 1 |
module.exports = { |
| 2 |
meta: { |
| 3 |
type: "problem", |
| 4 |
fixable: "code", |
| 5 |
|
| 6 |
schema: [{ |
| 7 |
type: "array", |
| 8 |
items: { |
| 9 |
type: "object", |
| 10 |
properties: { |
| 11 |
path: { type: "string" }, |
| 12 |
|
| 13 |
defaultValue: { type: "any" } |
| 14 |
} |
| 15 |
} |
| 16 |
}], |
| 17 |
}, |
| 18 |
create(context) { |
| 19 |
function checker(propName, defaultValue) { |
| 20 |
return (node) => { |
| 21 |
const nodeValue = node.value; |
| 22 |
|
| 23 |
if (nodeValue.type === "Literal" && nodeValue.value === defaultValue) { |
| 24 |
context.report({ |
| 25 |
node, |
| 26 |
message: |
| 27 |
`The ${propName} prop if ${defaultValue} by default and can be omitted`, |
| 28 |
fix(fixer) { |
| 29 |
const [start, end] = node.range; |
| 30 |
return fixer.removeRange([start, end + 1]); |
| 31 |
}, |
| 32 |
}); |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
return context.options[0].reduce((traverseObject, setting) => { |
| 38 |
const { path, defaultValue } = setting |
| 39 |
const pathComponents = path.split(".") |
| 40 |
pathComponents.reduce((previousTraversePaths, component) => { |
| 41 |
return previousTraversePaths.flatMap((currentPath) => { |
| 42 |
if (component.startsWith("[*]")) { |
| 43 |
const returnedPaths = [] |
| 44 |
if (component.endsWith("?")) { |
| 45 |
returnedPaths.push(currentPath) |
| 46 |
} |
| 47 |
returnedPaths.push(currentPath + 'ArrayExpression > ') |
| 48 |
return returnedPaths |
| 49 |
} else { |
| 50 |
return `${currentPath}ObjectExpression > Property[key.name="${component}"] > ` |
| 51 |
} |
| 52 |
}) |
| 53 |
}, [""]).map(path => path.slice(0, -3)).forEach(traversePath => { |
| 54 |
traverseObject[traversePath] = checker(pathComponents[pathComponents.length - 1], defaultValue) |
| 55 |
}); |
| 56 |
return traverseObject |
| 57 |
}, {}) |
| 58 |
}, |
| 59 |
}; |