Skip to content

Commit c78d53b

Browse files
Aware locals
1 parent 252aa45 commit c78d53b

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

src/index.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function processNodes(tree, options, messages) {
5353

5454
const html = parseToPostHtml(fs.readFileSync(filePath, options.encoding));
5555

56-
options.expressions.locals = {...options.locals};
56+
options.expressions.locals = {...options.locals, ...options.aware};
5757

5858
const slotsLocals = parseSlotsLocals(options.slotTagName, html, node.content);
5959
const {attributes, locals} = parseLocals(options, slotsLocals, node, html);
@@ -121,6 +121,7 @@ function parseLocals(options, slotsLocals, {attrs}, html) {
121121
// only for Array or Objects
122122
const mergeAttributeWithDefault = [];
123123
const computedAttributes = [];
124+
const awareAttributes = [];
124125
Object.keys(attributes).forEach(attribute => {
125126
if (attribute.startsWith('merge:')) {
126127
const newAttributeName = attribute.replace('merge:', '');
@@ -132,6 +133,11 @@ function parseLocals(options, slotsLocals, {attrs}, html) {
132133
attributes[newAttributeName] = attributes[attribute];
133134
delete attributes[attribute];
134135
computedAttributes.push(newAttributeName);
136+
} else if (attribute.startsWith('aware:')) {
137+
const newAttributeName = attribute.replace('aware:', '');
138+
attributes[newAttributeName] = attributes[attribute];
139+
delete attributes[attribute];
140+
awareAttributes.push(newAttributeName);
135141
}
136142
});
137143

@@ -168,12 +174,7 @@ function parseLocals(options, slotsLocals, {attrs}, html) {
168174
const attributesToBeMerged = Object.fromEntries(Object.entries(attributes).filter(([attribute]) => mergeAttributeWithDefault.includes(attribute)));
169175
const localsToBeMerged = Object.fromEntries(Object.entries(locals).filter(([local]) => mergeAttributeWithDefault.includes(local)));
170176
if (Object.keys(localsToBeMerged).length > 0) {
171-
console.log({localsToBeMerged, attributesToBeMerged});
172-
console.log(attributes);
173177
mergeAttributeWithDefault.forEach(attribute => {
174-
console.log(attribute);
175-
console.log(typeof localsToBeMerged[attribute]);
176-
console.log(typeof attributesToBeMerged[attribute]);
177178
attributes[attribute] = merge(localsToBeMerged[attribute], attributesToBeMerged[attribute]);
178179
});
179180
}
@@ -186,6 +187,10 @@ function parseLocals(options, slotsLocals, {attrs}, html) {
186187
});
187188
}
188189

190+
if (awareAttributes.length > 0) {
191+
options.aware = Object.fromEntries(Object.entries(attributes).filter(([attribute]) => awareAttributes.includes(attribute)));
192+
}
193+
189194
return {attributes, locals};
190195
}
191196

@@ -479,6 +484,7 @@ module.exports = (options = {}) => {
479484
}
480485

481486
options.locals = {...options.expressions.locals};
487+
options.aware = {};
482488

483489
return function (tree) {
484490
tree = processNodes(tree, options, tree.messages);

test/templates/components/child.html

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
anObject2: { one: 'One2', two: 'Two2', three: 'Three2'}
1010
};
1111
</script>
12+
CHILD:
1213
<div>
1314
aBoolean
1415
value: {{ aBoolean }}

test/test-locals.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ test('Must process component with locals as JSON and string', async t => {
5050

5151
test('Must process parent and child locals via component', async t => {
5252
const actual = `<x-parent aString="I am custom aString for PARENT via component (1)"><x-child></x-child></x-parent>`;
53-
const expected = `PARENT:<div> aBoolean value: true type: boolean aString value: I am custom aString for PARENT via component (1) type: string aString2 value: I am not string 2 type: string anArray value: ["one","two","three"] type: object anObject value: {"one":"One","two":"Two","three":"Three"} type: object anArray2 value: ["one2","two2","three2"] type: object anObject2 value: {"one":"One2","two":"Two2","three":"Three2"} type: object</div> <div> aBoolean value: true type: boolean aString value: My String type: string aString2 value: I am not string 2 type: string anArray value: ["one","two","three"] type: object anObject value: {"one":"One","two":"Two","three":"Three"} type: object anArray2 value: ["one2","two2","three2"] type: object anObject2 value: {"one":"One2","two":"Two2","three":"Three2"} type: object</div>`;
53+
const expected = `PARENT:<div> aBoolean value: true type: boolean aString value: I am custom aString for PARENT via component (1) type: string aString2 value: I am not string 2 type: string anArray value: ["one","two","three"] type: object anObject value: {"one":"One","two":"Two","three":"Three"} type: object anArray2 value: ["one2","two2","three2"] type: object anObject2 value: {"one":"One2","two":"Two2","three":"Three2"} type: object</div> CHILD:<div> aBoolean value: true type: boolean aString value: My String type: string aString2 value: I am not string 2 type: string anArray value: ["one","two","three"] type: object anObject value: {"one":"One","two":"Two","three":"Three"} type: object anArray2 value: ["one2","two2","three2"] type: object anObject2 value: {"one":"One2","two":"Two2","three":"Three2"} type: object</div>`;
5454

5555
const html = await posthtml([plugin({root: './test/templates/components'})]).process(actual).then(result => clean(result.html));
5656

@@ -59,7 +59,7 @@ test('Must process parent and child locals via component', async t => {
5959

6060
test('Must process parent and child locals via slots', async t => {
6161
const actual = `<x-parent aString="I am custom aString for PARENT via component (1)"><slot name="child"></slot></x-parent>`;
62-
const expected = `PARENT:<div> aBoolean value: true type: boolean aString value: I am custom aString for PARENT via component (1) type: string aString2 value: I am not string 2 type: string anArray value: ["one","two","three"] type: object anObject value: {"one":"One","two":"Two","three":"Three"} type: object anArray2 value: ["one2","two2","three2"] type: object anObject2 value: {"one":"One2","two":"Two2","three":"Three2"} type: object</div> <div> aBoolean value: true type: boolean aString value: I am custom aString for PARENT via component (1) type: string aString2 value: I am not string 2 type: string anArray value: ["one","two","three"] type: object anObject value: {"one":"One","two":"Two","three":"Three"} type: object anArray2 value: ["one2","two2","three2"] type: object anObject2 value: {"one":"One2","two":"Two2","three":"Three2"} type: object</div>`;
62+
const expected = `PARENT:<div> aBoolean value: true type: boolean aString value: I am custom aString for PARENT via component (1) type: string aString2 value: I am not string 2 type: string anArray value: ["one","two","three"] type: object anObject value: {"one":"One","two":"Two","three":"Three"} type: object anArray2 value: ["one2","two2","three2"] type: object anObject2 value: {"one":"One2","two":"Two2","three":"Three2"} type: object</div> CHILD:<div> aBoolean value: true type: boolean aString value: I am custom aString for PARENT via component (1) type: string aString2 value: I am not string 2 type: string anArray value: ["one","two","three"] type: object anObject value: {"one":"One","two":"Two","three":"Three"} type: object anArray2 value: ["one2","two2","three2"] type: object anObject2 value: {"one":"One2","two":"Two2","three":"Three2"} type: object</div>`;
6363

6464
const html = await posthtml([plugin({root: './test/templates/components'})]).process(actual).then(result => clean(result.html));
6565

@@ -74,3 +74,12 @@ test('Must has access to $slots in script locals', async t => {
7474

7575
t.is(html, expected);
7676
});
77+
78+
test('Must pass locals from parent to child via aware', async t => {
79+
const actual = `<x-parent aware:aString="I am custom aString for PARENT via component (1)"><x-child></x-child></x-parent>`;
80+
const expected = `PARENT:<div> aBoolean value: true type: boolean aString value: I am custom aString for PARENT via component (1) type: string aString2 value: I am not string 2 type: string anArray value: ["one","two","three"] type: object anObject value: {"one":"One","two":"Two","three":"Three"} type: object anArray2 value: ["one2","two2","three2"] type: object anObject2 value: {"one":"One2","two":"Two2","three":"Three2"} type: object</div> CHILD:<div> aBoolean value: true type: boolean aString value: I am custom aString for PARENT via component (1) type: string aString2 value: I am not string 2 type: string anArray value: ["one","two","three"] type: object anObject value: {"one":"One","two":"Two","three":"Three"} type: object anArray2 value: ["one2","two2","three2"] type: object anObject2 value: {"one":"One2","two":"Two2","three":"Three2"} type: object</div>`;
81+
82+
const html = await posthtml([plugin({root: './test/templates/components'})]).process(actual).then(result => clean(result.html));
83+
84+
t.is(html, expected);
85+
});

0 commit comments

Comments
 (0)