MCPcopy Create free account
hub / github.com/ampproject/amphtml / create

Function create

build-system/eslint-rules/prefer-unnested-spread-objects.js:33–77  ·  view source on GitHub ↗
(context)

Source from the content-addressed store, hash-verified

31 },
32
33 create(context) {
34 const sourceCode = context.getSourceCode();
35 const spreadElement =
36 ':matches(ObjectExpression > ExperimentalSpreadProperty, ObjectExpression > SpreadElement)';
37
38 /**
39 * @param {Array<T>} array
40 * @param {T} item
41 * @return {?T}
42 */
43 function findAfter(array, item) {
44 const index = array.indexOf(item);
45 return index < array.length - 1 ? array[index + 1] : null;
46 }
47
48 return {
49 [`${spreadElement} > ObjectExpression`](node) {
50 context.report({
51 node,
52 message: 'Nesting an object under an object spread is not useful',
53
54 fix(fixer) {
55 const {parent, properties} = node;
56 const texts = properties.map((prop) => sourceCode.getText(prop));
57
58 if (texts.length > 0) {
59 return fixer.replaceText(node.parent, texts.join(','));
60 }
61
62 const grandParent = parent.parent;
63 const next = findAfter(grandParent.properties, parent);
64
65 if (next) {
66 return fixer.removeRange([parent.range[0], next.range[0] - 1]);
67 }
68
69 return fixer.removeRange([
70 parent.range[0],
71 grandParent.range[1] - 1,
72 ]);
73 },
74 });
75 },
76 };
77 },
78};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected