Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS plugin: warn about amp config in app #45254

Merged
merged 2 commits into from
Jan 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/next/src/server/next-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const DISALLOWED_SERVER_REACT_APIS: string[] = [
'createFactory',
]

const LEGACY_CONFIG_EXPORT = 'config'
const ALLOWED_EXPORTS = ['config', 'generateStaticParams']

const ALLOWED_PAGE_PROPS = ['params', 'searchParams']
Expand All @@ -39,6 +40,7 @@ const NEXT_TS_ERRORS = {
INVALID_OPTION_VALUE: 71003,
MISPLACED_CLIENT_ENTRY: 71004,
INVALID_PAGE_PROP: 71005,
INVALID_CONFIG_OPTION: 71006,
}

const API_DOCS: Record<
Expand Down Expand Up @@ -803,6 +805,28 @@ export function createTSPlugin(modules: {
})
}
}
} else if (name.text === LEGACY_CONFIG_EXPORT) {
// export const config = { ... }
// Error if using `amp: ...`
const value = declarartion.initializer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: declaration typo (also happening in other places). Not sure if is intentional

Suggested change
const value = declarartion.initializer
const value = declaration.initializer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good point, thanks! I'll fix all them together.

if (value && ts.isObjectLiteralExpression(value)) {
for (const prop of value.properties) {
if (
ts.isPropertyAssignment(prop) &&
ts.isIdentifier(prop.name) &&
prop.name.text === 'amp'
) {
prior.push({
file: source,
category: ts.DiagnosticCategory.Error,
code: NEXT_TS_ERRORS.INVALID_CONFIG_OPTION,
messageText: `AMP is not supported in the app directory. If you need to use AMP it will continue to be supported in the pages directory.`,
start: prop.getStart(),
length: prop.getWidth(),
})
}
}
}
}
}
}
Expand Down