Unverified Commit 1d58c0ab authored by Shu Ding's avatar Shu Ding Committed by GitHub

Improve TypeScript plugin error when the configuration is not statically analyzable (#42062)

As per
https://github.com/vercel/next.js/issues/41951#issuecomment-1294199692,
this PR improves the TS plugin error message when a non-literal
configuration is used:

<img width="902" alt="CleanShot 2022-10-28 at 07 36 31@2x"
src="https://user-images.githubusercontent.com/3676859/198650771-026a0430-d5bc-4826-9aef-b1bfb7947c6f.png">

Also a bug is fixed so string template literals (``const revalidate =
`auto`;``) will no longer result in an error.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent d651e279
...@@ -130,7 +130,7 @@ function getAPIDescription(api: string): string { ...@@ -130,7 +130,7 @@ function getAPIDescription(api: string): string {
} }
function removeStringQuotes(str: string): string { function removeStringQuotes(str: string): string {
return str.replace(/^['"]|['"]$/g, '') return str.replace(/^['"`]|['"`]$/g, '')
} }
export function createTSPlugin(modules: { export function createTSPlugin(modules: {
...@@ -527,8 +527,16 @@ export function createTSPlugin(modules: { ...@@ -527,8 +527,16 @@ export function createTSPlugin(modules: {
} else if (API_DOCS[name.text]) { } else if (API_DOCS[name.text]) {
// Check if the value is valid // Check if the value is valid
const value = declarartion.initializer const value = declarartion.initializer
if (value) { if (value) {
if (value.kind === ts.SyntaxKind.StringLiteral) { let displayedValue = ''
let errorMessage = ''
let isInvalid = false
if (
ts.isStringLiteral(value) ||
ts.isNoSubstitutionTemplateLiteral(value)
) {
const text = removeStringQuotes(value.getText()) const text = removeStringQuotes(value.getText())
const allowedValues = Object.keys( const allowedValues = Object.keys(
API_DOCS[name.text].options API_DOCS[name.text].options
...@@ -537,35 +545,24 @@ export function createTSPlugin(modules: { ...@@ -537,35 +545,24 @@ export function createTSPlugin(modules: {
.map(removeStringQuotes) .map(removeStringQuotes)
if (!allowedValues.includes(text)) { if (!allowedValues.includes(text)) {
prior.push({ isInvalid = true
file: source, displayedValue = `'${text}'`
category: ts.DiagnosticCategory.Error,
code: 71003,
messageText: `"'${text}'" is not a valid value for the "${name.text}" option.`,
start: value.getStart(),
length: value.getEnd() - value.getStart(),
})
} }
} else if ( } else if (
value.kind === ts.SyntaxKind.NumericLiteral || ts.isNumericLiteral(value) ||
(value.kind === ts.SyntaxKind.PrefixUnaryExpression && (ts.isPrefixUnaryExpression(value) &&
(value as any).operator === ts.isMinusToken((value as any).operator) &&
ts.SyntaxKind.MinusToken && (ts.isNumericLiteral((value as any).operand.kind) ||
(value as any).operand.kind === (ts.isIdentifier((value as any).operand.kind) &&
ts.SyntaxKind.NumericLiteral) || (value as any).operand.kind.getText() ===
(value.kind === ts.SyntaxKind.Identifier && 'Infinity'))) ||
(ts.isIdentifier(value) &&
value.getText() === 'Infinity') value.getText() === 'Infinity')
) { ) {
const v = value.getText() const v = value.getText()
if (API_DOCS[name.text].isValid?.(v) === false) { if (API_DOCS[name.text].isValid?.(v) === false) {
prior.push({ isInvalid = true
file: source, displayedValue = v
category: ts.DiagnosticCategory.Error,
code: 71003,
messageText: `"${v}" is not a valid value for the "${name.text}" option.`,
start: value.getStart(),
length: value.getEnd() - value.getStart(),
})
} }
} else if ( } else if (
value.kind === ts.SyntaxKind.TrueKeyword || value.kind === ts.SyntaxKind.TrueKeyword ||
...@@ -573,23 +570,33 @@ export function createTSPlugin(modules: { ...@@ -573,23 +570,33 @@ export function createTSPlugin(modules: {
) { ) {
const v = value.getText() const v = value.getText()
if (API_DOCS[name.text].isValid?.(v) === false) { if (API_DOCS[name.text].isValid?.(v) === false) {
prior.push({ isInvalid = true
file: source, displayedValue = v
category: ts.DiagnosticCategory.Error,
code: 71003,
messageText: `"${v}" is not a valid value for the "${name.text}" option.`,
start: value.getStart(),
length: value.getEnd() - value.getStart(),
})
} }
} else if (
// Other literals
ts.isBigIntLiteral(value) ||
ts.isArrayLiteralExpression(value) ||
ts.isObjectLiteralExpression(value) ||
ts.isRegularExpressionLiteral(value)
) {
isInvalid = true
displayedValue = value.getText()
} else { } else {
// Not a literal, error because it's not statically analyzable
isInvalid = true
displayedValue = value.getText()
errorMessage = `"${displayedValue}" is not a valid value for the "${name.text}" option. The configuration must be statically analyzable.`
}
if (isInvalid) {
prior.push({ prior.push({
file: source, file: source,
category: ts.DiagnosticCategory.Error, category: ts.DiagnosticCategory.Error,
code: 71003, code: 71003,
messageText: `"${value.getText()}" is not a valid value for the "${ messageText:
name.text errorMessage ||
}" option.`, `"${displayedValue}" is not a valid value for the "${name.text}" option.`,
start: value.getStart(), start: value.getStart(),
length: value.getEnd() - value.getStart(), length: value.getEnd() - value.getStart(),
}) })
......
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