Fixed props shadowing (#1433)

* Fixed props shadowing

* Added changeset

* Added prop shadowing test
This commit is contained in:
AsyncBanana 2021-09-27 11:52:57 -04:00 committed by GitHub
parent af1ca5dd13
commit 3083563506
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 13 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixed props shadowing

View file

@ -161,7 +161,7 @@ function generateAttributes(attrs: Record<string, string>): string {
result += JSON.stringify(key) + ':' + val + ','; result += JSON.stringify(key) + ':' + val + ',';
} }
} }
result += `[__astroContext]:props[__astroContext]`; result += `[__astroContext]:$$props[__astroContext]`;
return result + '}'; return result + '}';
} }

View file

@ -159,10 +159,10 @@ const __astroScripts = __astro_hoisted_scripts([${result.components.map((n) => `
const __astroInternal = Symbol('astro.internal'); const __astroInternal = Symbol('astro.internal');
const __astroContext = Symbol.for('astro.context'); const __astroContext = Symbol.for('astro.context');
const __astroSlotted = Symbol.for('astro.slotted'); const __astroSlotted = Symbol.for('astro.slotted');
async function __render(props, ...children) { async function __render($$props, ...children) {
const Astro = Object.create(__TopLevelAstro, { const Astro = Object.create(__TopLevelAstro, {
props: { props: {
value: props, value: $$props,
enumerable: true enumerable: true
}, },
slots: { slots: {
@ -178,19 +178,19 @@ async function __render(props, ...children) {
enumerable: true enumerable: true
}, },
pageCSS: { pageCSS: {
value: (props[__astroContext] && props[__astroContext].pageCSS) || [], value: ($$props[__astroContext] && $$props[__astroContext].pageCSS) || [],
enumerable: true enumerable: true
}, },
pageScripts: { pageScripts: {
value: (props[__astroContext] && props[__astroContext].pageScripts) || [], value: ($$props[__astroContext] && $$props[__astroContext].pageScripts) || [],
enumerable: true enumerable: true
}, },
isPage: { isPage: {
value: (props[__astroInternal] && props[__astroInternal].isPage) || false, value: ($$props[__astroInternal] && $$props[__astroInternal].isPage) || false,
enumerable: true enumerable: true
}, },
request: { request: {
value: (props[__astroContext] && props[__astroContext].request) || {}, value: ($$props[__astroContext] && $$props[__astroContext].request) || {},
enumerable: true enumerable: true
}, },
}); });
@ -202,7 +202,7 @@ export default { isAstroComponent: true, __render, [Symbol.for('astro.hoistedScr
// \`__renderPage()\`: Render the contents of the Astro module as a page. This is a special flow, // \`__renderPage()\`: Render the contents of the Astro module as a page. This is a special flow,
// triggered by loading a component directly by URL. // triggered by loading a component directly by URL.
export async function __renderPage({request, children, props, css, scripts}) { export async function __renderPage({request, children, props: $$props, css, scripts}) {
const currentChild = { const currentChild = {
isAstroComponent: true, isAstroComponent: true,
layout: typeof __layout === 'undefined' ? undefined : __layout, layout: typeof __layout === 'undefined' ? undefined : __layout,
@ -210,10 +210,10 @@ export async function __renderPage({request, children, props, css, scripts}) {
__render, __render,
}; };
const isLayout = (__astroContext in props); const isLayout = (__astroContext in $$props);
if(!isLayout) { if(!isLayout) {
let astroRootUIDCounter = 0; let astroRootUIDCounter = 0;
Object.defineProperty(props, __astroContext, { Object.defineProperty($$props, __astroContext, {
value: { value: {
pageCSS: css, pageCSS: css,
request, request,
@ -225,7 +225,7 @@ export async function __renderPage({request, children, props, css, scripts}) {
}); });
} }
Object.defineProperty(props, __astroInternal, { Object.defineProperty($$props, __astroInternal, {
value: { value: {
isPage: !isLayout isPage: !isLayout
}, },
@ -233,13 +233,13 @@ export async function __renderPage({request, children, props, css, scripts}) {
enumerable: false enumerable: false
}); });
const childBodyResult = await currentChild.__render(props, children); const childBodyResult = await currentChild.__render($$props, children);
// find layout, if one was given. // find layout, if one was given.
if (currentChild.layout) { if (currentChild.layout) {
return currentChild.layout({ return currentChild.layout({
request, request,
props: {content: currentChild.content, [__astroContext]: props[__astroContext]}, props: {content: currentChild.content, [__astroContext]: $$props[__astroContext]},
children: [childBodyResult], children: [childBodyResult],
}); });
} }

View file

@ -34,6 +34,11 @@ Components('Allows Components defined in frontmatter', async ({ runtime }) => {
assert.equal($('h1').length, 1); assert.equal($('h1').length, 1);
}); });
Components('Allows variables named props', async ({ runtime }) => {
const result = await runtime.load('/props-shadowing');
assert.equal(result.statusCode, 500);
});
Components('Still throws an error for undefined components', async ({ runtime }) => { Components('Still throws an error for undefined components', async ({ runtime }) => {
const result = await runtime.load('/undefined-component'); const result = await runtime.load('/undefined-component');
assert.equal(result.statusCode, 500); assert.equal(result.statusCode, 500);

View file

@ -0,0 +1,6 @@
---
export interface Props {
test: string;
}
---
<h1 id="direct-props-h1">{props.test}</h1>

View file

@ -0,0 +1,4 @@
---
import PropsComponent from "../components/Props-Component.astro"
---
<PropsComponent test="test string"/>