Skip to content

Commit

Permalink
Merge branch 'canary' into shu/a15a
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding authored Apr 30, 2023
2 parents 4859f7e + acd8735 commit 41234b0
Showing 1 changed file with 43 additions and 13 deletions.
56 changes: 43 additions & 13 deletions packages/next/src/server/typescript/rules/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const API_DOCS: Record<
link?: string
type?: string
isValid?: (value: string) => boolean
getHint?: (value: any) => string
getHint?: (value: any) => string | undefined
}
> = {
dynamic: {
Expand Down Expand Up @@ -61,14 +61,32 @@ const API_DOCS: Record<
},
preferredRegion: {
description:
'Specify the perferred region that this layout or page should be deployed to. If the region option is not specified, it inherits the option from the nearest parent layout. The root defaults to `"auto"`.',
'Specify the perferred region that this layout or page should be deployed to. If the region option is not specified, it inherits the option from the nearest parent layout. The root defaults to `"auto"`.\n\nYou can also specify a region, such as "iad1", or an array of regions, such as `["iad1", "sfo1"]`.',
options: {
'"auto"':
'Next.js will first deploy to the `"home"` region. Then if it doesn’t detect any waterfall requests after a few requests, it can upgrade that route, to be deployed globally to `"edge"`. If it detects any waterfall requests after that, it can eventually downgrade back to `"home`".',
'Next.js will first deploy to the `"home"` region. Then if it doesn’t detect any waterfall requests after a few requests, it can upgrade that route, to be deployed globally. If it detects any waterfall requests after that, it can eventually downgrade back to `"home`".',
'"global"': 'Prefer deploying globally.',
'"home"': 'Prefer deploying to the Home region.',
'"edge"': 'Prefer deploying to the Edge globally.',
},
link: 'https://beta.nextjs.org/docs/api-reference/segment-config#preferredregion',
isValid: (value: string) => {
try {
const parsed = JSON.parse(value)
return (
typeof parsed === 'string' ||
(Array.isArray(parsed) && !parsed.some((v) => typeof v !== 'string'))
)
} catch (err) {
return false
}
},
getHint: (value: any) => {
if (value === 'auto') return `Automatically chosen by Next.js.`
if (value === 'global') return `Prefer deploying globally.`
if (value === 'home') return `Prefer deploying to the Home region.`
if (Array.isArray(value)) return `Deploy to regions: ${value.join(', ')}.`
if (typeof value === 'string') return `Deploy to region: ${value}.`
},
},
revalidate: {
description:
Expand Down Expand Up @@ -375,14 +393,17 @@ const config = {
ts.isStringLiteral(value) ||
ts.isNoSubstitutionTemplateLiteral(value)
) {
const text = removeStringQuotes(value.getText())
const allowedValues = Object.keys(options)
.filter((v) => /^['"]/.test(v))
.map(removeStringQuotes)
const val = '"' + removeStringQuotes(value.getText()) + '"'
const allowedValues = Object.keys(options).filter((v) =>
/^['"]/.test(v)
)

if (!allowedValues.includes(text)) {
if (
!allowedValues.includes(val) &&
!API_DOCS[name.text].isValid?.(val)
) {
isInvalid = true
displayedValue = `'${text}'`
displayedValue = val
}
} else if (
ts.isNumericLiteral(value) ||
Expand All @@ -394,7 +415,7 @@ const config = {
(ts.isIdentifier(value) && value.getText() === 'Infinity')
) {
const v = value.getText()
if (API_DOCS[name.text].isValid?.(v) === false) {
if (!API_DOCS[name.text].isValid?.(v)) {
isInvalid = true
displayedValue = v
}
Expand All @@ -403,14 +424,23 @@ const config = {
value.kind === ts.SyntaxKind.FalseKeyword
) {
const v = value.getText()
if (API_DOCS[name.text].isValid?.(v) === false) {
if (!API_DOCS[name.text].isValid?.(v)) {
isInvalid = true
displayedValue = v
}
} else if (ts.isArrayLiteralExpression(value)) {
const v = value.getText()
if (
!API_DOCS[name.text].isValid?.(
JSON.stringify(value.elements.map((e) => e.getText()))
)
) {
isInvalid = true
displayedValue = v
}
} else if (
// Other literals
ts.isBigIntLiteral(value) ||
ts.isArrayLiteralExpression(value) ||
ts.isObjectLiteralExpression(value) ||
ts.isRegularExpressionLiteral(value) ||
ts.isPrefixUnaryExpression(value)
Expand Down

0 comments on commit 41234b0

Please sign in to comment.