Unverified Commit a2265319 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Support type cast expressions in page static info extractor (#92837)

So that
```ts
export const runtime = "node" as const
export const runtime = "node" as string
export const runtime = "node" satisfies string // was already working
```
works
parent 40a4e666
......@@ -10,7 +10,8 @@ use swc_core::{
ecma::{
ast::{
ClassExpr, Decl, ExportSpecifier, Expr, ExprStmt, FnExpr, Lit, ModuleDecl,
ModuleExportName, ModuleItem, Program, Stmt, Str, TsSatisfiesExpr,
ModuleExportName, ModuleItem, Program, Stmt, Str, TsAsExpr, TsConstAssertion,
TsSatisfiesExpr, TsTypeAssertion,
},
utils::IsDirective,
},
......@@ -609,12 +610,14 @@ async fn parse_config_value(
) -> Result<()> {
let get_value = || {
let init = init.as_deref();
// Unwrap `export const config = { .. } satisfies ProxyConfig`, usually this is already
// transpiled away, but we are looking at the original source here.
let init = if let Some(Expr::TsSatisfies(TsSatisfiesExpr { expr, .. })) = init {
Some(&**expr)
} else {
init
// Unwrap typecasts such as `export const config = { .. } satisfies ProxyConfig`, usually
// this is already transpiled away, but we are looking at the original source here.
let init = match init {
Some(Expr::TsAs(TsAsExpr { expr, .. }))
| Some(Expr::TsTypeAssertion(TsTypeAssertion { expr, .. }))
| Some(Expr::TsConstAssertion(TsConstAssertion { expr, .. }))
| Some(Expr::TsSatisfies(TsSatisfiesExpr { expr, .. })) => Some(&**expr),
_ => init,
};
init.map(|init| eval_context.eval(init)).map(|v| {
// Special case, as we don't call `link` here: assume that `undefined` is a free
......
......@@ -12,6 +12,9 @@ import type {
RegExpLiteral,
StringLiteral,
TemplateLiteral,
TsAsExpression,
TsConstAssertion,
TsTypeAssertion,
TsSatisfiesExpression,
VariableDeclaration,
} from '@swc/core'
......@@ -64,6 +67,18 @@ function isTemplateLiteral(node: Node): node is TemplateLiteral {
return node.type === 'TemplateLiteral'
}
function isTsAsExpression(node: Node): node is TsAsExpression {
return node.type === 'TsAsExpression'
}
function isTsConstAssertion(node: Node): node is TsConstAssertion {
return node.type === 'TsConstAssertion'
}
function isTsTypeAssertion(node: Node): node is TsTypeAssertion {
return node.type === 'TsTypeAssertion'
}
function isTsSatisfiesExpression(node: Node): node is TsSatisfiesExpression {
return node.type === 'TsSatisfiesExpression'
}
......@@ -196,7 +211,12 @@ function extractValue(node: Node, path?: string[]): ExtractValueResult {
const [{ cooked, raw }] = node.quasis
return { value: cooked ?? raw }
} else if (isTsSatisfiesExpression(node)) {
} else if (
isTsSatisfiesExpression(node) ||
isTsAsExpression(node) ||
isTsTypeAssertion(node) ||
isTsConstAssertion(node)
) {
return extractValue(node.expression)
} else {
return {
......
export const maxDuration = 1000 as const
export default function Page() {
return <main>hello</main>
}
export const maxDuration = 1000 as number
export default function Page() {
return <main>hello</main>
}
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export const maxDuration = 1000 satisfies number
export default function Page() {
return <main>hello</main>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
}
module.exports = nextConfig
export const config = { maxDuration: 1000 } as const
export default function Page() {
return <main>hello</main>
}
export const config = { maxDuration: 1000 } as {
maxDuration: number
}
export default function Page() {
return <main>hello</main>
}
export const config = { maxDuration: 1000 } satisfies {
maxDuration: number
}
export default function Page() {
return <main>hello</main>
}
import { nextTestSetup } from 'e2e-utils'
describe('TypeScript type expressions in route segment config', () => {
const { next, isNextStart } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
describe('app directory', () => {
it('should pick up maxDuration declared with `as` type assertion', async () => {
const $ = await next.render$('/as')
expect($('main').text()).toBe('hello')
})
it('should pick up maxDuration declared with `as const` assertion', async () => {
const $ = await next.render$('/as-const')
expect($('main').text()).toBe('hello')
})
it('should pick up maxDuration declared with `satisfies`', async () => {
const $ = await next.render$('/satisfies')
expect($('main').text()).toBe('hello')
})
})
describe('pages directory', () => {
it('should pick up maxDuration from config object declared with `as`', async () => {
const $ = await next.render$('/config-as')
expect($('main').text()).toBe('hello')
})
it('should pick up maxDuration from config object declared with `as const`', async () => {
const $ = await next.render$('/config-as-const')
expect($('main').text()).toBe('hello')
})
it('should pick up maxDuration from config object declared with `satisfies`', async () => {
const $ = await next.render$('/config-satisfies')
expect($('main').text()).toBe('hello')
})
})
if (isNextStart) {
it('should parse the config correctly', async () => {
const config = await next.readJSON(
'.next/server/functions-config-manifest.json'
)
expect(config).toMatchInlineSnapshot(`
{
"functions": {
"/as": {
"maxDuration": 1000,
},
"/as-const": {
"maxDuration": 1000,
},
"/config-as": {
"maxDuration": 1000,
},
"/config-as-const": {
"maxDuration": 1000,
},
"/config-satisfies": {
"maxDuration": 1000,
},
"/satisfies": {
"maxDuration": 1000,
},
},
"version": 1,
}
`)
})
}
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment