mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-07 09:22:27 +00:00
fix(nuxt): use node location
instead of range
for route meta property extraction (#30447)
This commit is contained in:
parent
e786d79bee
commit
b00264cd7c
@ -11,7 +11,7 @@ import { transform } from 'esbuild'
|
|||||||
import type { Property } from 'estree'
|
import type { Property } from 'estree'
|
||||||
import type { NuxtPage } from 'nuxt/schema'
|
import type { NuxtPage } from 'nuxt/schema'
|
||||||
|
|
||||||
import { parseAndWalk } from '../core/utils/parse'
|
import { parseAndWalk, withLocations } from '../core/utils/parse'
|
||||||
import { getLoader, uniqueBy } from '../core/utils'
|
import { getLoader, uniqueBy } from '../core/utils'
|
||||||
import { logger, toArray } from '../utils'
|
import { logger, toArray } from '../utils'
|
||||||
|
|
||||||
@ -247,8 +247,10 @@ export async function getRouteMeta (contents: string, absolutePath: string, extr
|
|||||||
const property = pageMetaArgument.properties.find((property): property is Property => property.type === 'Property' && property.key.type === 'Identifier' && property.key.name === key)
|
const property = pageMetaArgument.properties.find((property): property is Property => property.type === 'Property' && property.key.type === 'Identifier' && property.key.name === key)
|
||||||
if (!property) { continue }
|
if (!property) { continue }
|
||||||
|
|
||||||
if (property.value.type === 'ObjectExpression') {
|
const propertyValue = withLocations(property.value)
|
||||||
const valueString = js.code.slice(property.value.range![0], property.value.range![1])
|
|
||||||
|
if (propertyValue.type === 'ObjectExpression') {
|
||||||
|
const valueString = js.code.slice(propertyValue.start, propertyValue.end)
|
||||||
try {
|
try {
|
||||||
extractedMeta[key] = JSON.parse(runInNewContext(`JSON.stringify(${valueString})`, {}))
|
extractedMeta[key] = JSON.parse(runInNewContext(`JSON.stringify(${valueString})`, {}))
|
||||||
} catch {
|
} catch {
|
||||||
@ -258,9 +260,9 @@ export async function getRouteMeta (contents: string, absolutePath: string, extr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (property.value.type === 'ArrayExpression') {
|
if (propertyValue.type === 'ArrayExpression') {
|
||||||
const values: string[] = []
|
const values: string[] = []
|
||||||
for (const element of property.value.elements) {
|
for (const element of propertyValue.elements) {
|
||||||
if (!element) {
|
if (!element) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -275,12 +277,12 @@ export async function getRouteMeta (contents: string, absolutePath: string, extr
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if (property.value.type !== 'Literal' || (typeof property.value.value !== 'string' && typeof property.value.value !== 'boolean')) {
|
if (propertyValue.type !== 'Literal' || (typeof propertyValue.value !== 'string' && typeof propertyValue.value !== 'boolean')) {
|
||||||
logger.debug(`Skipping extraction of \`${key}\` metadata as it is not a string literal or array of string literals (reading \`${absolutePath}\`).`)
|
logger.debug(`Skipping extraction of \`${key}\` metadata as it is not a string literal or array of string literals (reading \`${absolutePath}\`).`)
|
||||||
dynamicProperties.add(key)
|
dynamicProperties.add(key)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
extractedMeta[key] = property.value.value
|
extractedMeta[key] = propertyValue.value
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const property of pageMetaArgument.properties) {
|
for (const property of pageMetaArgument.properties) {
|
||||||
|
@ -78,7 +78,6 @@ definePageMeta({ name: 'bar' })
|
|||||||
const meta = await getRouteMeta(`
|
const meta = await getRouteMeta(`
|
||||||
<script setup>
|
<script setup>
|
||||||
definePageMeta({
|
definePageMeta({
|
||||||
name: 'some-custom-name',
|
|
||||||
path: '/some-custom-path',
|
path: '/some-custom-path',
|
||||||
validate: () => true,
|
validate: () => true,
|
||||||
middleware: [
|
middleware: [
|
||||||
@ -87,19 +86,32 @@ definePageMeta({ name: 'bar' })
|
|||||||
otherValue: {
|
otherValue: {
|
||||||
foo: 'bar',
|
foo: 'bar',
|
||||||
},
|
},
|
||||||
|
// 'name', 'props' and 'alias' are part of 'defaultExtractionKeys'; they're extracted from the component, so we should test the AST walking for different value types
|
||||||
|
name: 'some-custom-name',
|
||||||
|
props: {
|
||||||
|
foo: 'bar',
|
||||||
|
},
|
||||||
|
alias: ['/alias'],
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
`, filePath)
|
`, filePath)
|
||||||
|
|
||||||
expect(meta).toMatchInlineSnapshot(`
|
expect(meta).toMatchInlineSnapshot(`
|
||||||
{
|
{
|
||||||
|
"alias": [
|
||||||
|
"/alias",
|
||||||
|
],
|
||||||
"meta": {
|
"meta": {
|
||||||
"__nuxt_dynamic_meta_key": Set {
|
"__nuxt_dynamic_meta_key": Set {
|
||||||
|
"props",
|
||||||
"meta",
|
"meta",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"name": "some-custom-name",
|
"name": "some-custom-name",
|
||||||
"path": "/some-custom-path",
|
"path": "/some-custom-path",
|
||||||
|
"props": {
|
||||||
|
"foo": "bar",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
`)
|
`)
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user