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::{ ...@@ -10,7 +10,8 @@ use swc_core::{
ecma::{ ecma::{
ast::{ ast::{
ClassExpr, Decl, ExportSpecifier, Expr, ExprStmt, FnExpr, Lit, ModuleDecl, 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, utils::IsDirective,
}, },
...@@ -609,12 +610,14 @@ async fn parse_config_value( ...@@ -609,12 +610,14 @@ async fn parse_config_value(
) -> Result<()> { ) -> Result<()> {
let get_value = || { let get_value = || {
let init = init.as_deref(); let init = init.as_deref();
// Unwrap `export const config = { .. } satisfies ProxyConfig`, usually this is already // Unwrap typecasts such as `export const config = { .. } satisfies ProxyConfig`, usually
// transpiled away, but we are looking at the original source here. // this is already transpiled away, but we are looking at the original source here.
let init = if let Some(Expr::TsSatisfies(TsSatisfiesExpr { expr, .. })) = init { let init = match init {
Some(&**expr) Some(Expr::TsAs(TsAsExpr { expr, .. }))
} else { | Some(Expr::TsTypeAssertion(TsTypeAssertion { expr, .. }))
init | Some(Expr::TsConstAssertion(TsConstAssertion { expr, .. }))
| Some(Expr::TsSatisfies(TsSatisfiesExpr { expr, .. })) => Some(&**expr),
_ => init,
}; };
init.map(|init| eval_context.eval(init)).map(|v| { init.map(|init| eval_context.eval(init)).map(|v| {
// Special case, as we don't call `link` here: assume that `undefined` is a free // Special case, as we don't call `link` here: assume that `undefined` is a free
......
...@@ -12,6 +12,9 @@ import type { ...@@ -12,6 +12,9 @@ import type {
RegExpLiteral, RegExpLiteral,
StringLiteral, StringLiteral,
TemplateLiteral, TemplateLiteral,
TsAsExpression,
TsConstAssertion,
TsTypeAssertion,
TsSatisfiesExpression, TsSatisfiesExpression,
VariableDeclaration, VariableDeclaration,
} from '@swc/core' } from '@swc/core'
...@@ -64,6 +67,18 @@ function isTemplateLiteral(node: Node): node is TemplateLiteral { ...@@ -64,6 +67,18 @@ function isTemplateLiteral(node: Node): node is TemplateLiteral {
return node.type === '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 { function isTsSatisfiesExpression(node: Node): node is TsSatisfiesExpression {
return node.type === 'TsSatisfiesExpression' return node.type === 'TsSatisfiesExpression'
} }
...@@ -196,7 +211,12 @@ function extractValue(node: Node, path?: string[]): ExtractValueResult { ...@@ -196,7 +211,12 @@ function extractValue(node: Node, path?: string[]): ExtractValueResult {
const [{ cooked, raw }] = node.quasis const [{ cooked, raw }] = node.quasis
return { value: cooked ?? raw } return { value: cooked ?? raw }
} else if (isTsSatisfiesExpression(node)) { } else if (
isTsSatisfiesExpression(node) ||
isTsAsExpression(node) ||
isTsTypeAssertion(node) ||
isTsConstAssertion(node)
) {
return extractValue(node.expression) return extractValue(node.expression)
} else { } else {
return { 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