Skip to content

ref(nextjs): Always use tsx parser in experimental function-wrapping loader #5563

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

Merged
merged 1 commit into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
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
8 changes: 3 additions & 5 deletions packages/nextjs/src/config/loaders/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as jscsTypes from 'jscodeshift';
import { default as jscodeshiftDefault } from 'jscodeshift';

import { makeParser } from './parsers';
import { parser } from './parser';

// In `jscodeshift`, the exports look like this:
//
Expand Down Expand Up @@ -64,12 +64,10 @@ type VariableDeclarationNode = jscsTypes.VariableDeclaration;
* Create an AST based on the given code.
*
* @param code The code to convert to an AST.
* @param isTS Flag indicating what parser to use.
* @throws Parsing error if the code is unparsable
* @throws Throws parsing error if the code is unparsable
* @returns The AST
*/
export function makeAST(code: string, isTS: boolean): AST {
const parser = isTS ? makeParser('tsx') : makeParser('jsx');
export function makeAST(code: string): AST {
// If this errors, it will be caught in the calling function, where we know more information and can construct a
// better warning message
return jscs(code, { parser });
Expand Down
8 changes: 3 additions & 5 deletions packages/nextjs/src/config/loaders/dataFetchersLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ type LoaderOptions = {
*/
function wrapFunctions(userCode: string, templateCode: string, filepath: string): string[] {
let userAST: AST, templateAST: AST;
const isTS = new RegExp('\\.tsx?$').test(filepath);

try {
userAST = makeAST(userCode, isTS);
templateAST = makeAST(templateCode, false);
userAST = makeAST(userCode);
templateAST = makeAST(templateCode);
} catch (err) {
logger.warn(`Couldn't add Sentry to ${filepath} because there was a parsing error: ${err}`);
// Replace the template code with an empty string, so in the end the user code is untouched
Expand Down Expand Up @@ -124,8 +123,7 @@ export default function wrapDataFetchersLoader(this: LoaderThis<LoaderOptions>,
// we know we're in a proxied file and do not need to proxy again.

if (!this.resourceQuery.includes('sentry-proxy-loader')) {
const ast = makeAST(userCode, true); // is there a reason to ever parse without typescript?

const ast = makeAST(userCode);
const exportedIdentifiers = getExportIdentifierNames(ast);

let outputFileContent = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Parser = {
parse: (code: string) => babel.ParseResult<File>;
};

const jsxOptions: babel.ParserOptions = {
const options: babel.ParserOptions = {
// Nextjs supports dynamic import, so this seems like a good idea
allowImportExportEverywhere: true,
// We're only supporting wrapping in ESM pages
Expand All @@ -40,25 +40,10 @@ const jsxOptions: babel.ParserOptions = {
['pipelineOperator', { proposal: 'hack', topicToken: '^' }],
'regexpUnicodeSets',
'throwExpressions',
'typescript',
] as babel.ParserPlugin[],
};

const tsxOptions = {
...jsxOptions,
// Because `jsxOptions` is typed as a `ParserOptions` object, TS doesn't discount the possibility of its `plugins`
// property being undefined, even though it is, in fact, very clearly defined - hence the empty array.
plugins: [...(jsxOptions.plugins || []), 'typescript'] as babel.ParserPlugin[],
export const parser: Parser = {
parse: code => babel.parse(code, options),
};

/**
* Create either a jsx or tsx parser to be used by `jscodeshift`.
*
* @param type Either 'jsx' or 'tsx'
* @returns An object with the appropriate `parse` method.
*/
export function makeParser(type: 'jsx' | 'tsx'): Parser {
const options = type === 'jsx' ? jsxOptions : tsxOptions;
return {
parse: code => babel.parse(code, options),
};
}
4 changes: 2 additions & 2 deletions packages/nextjs/test/config/ast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test.each([
['export { default } from "module-name";', true],
['export { default, name1 } from "module-name";', true],
])('hasDefaultExport(%s) should return %p', (program, expectedResult) => {
const ast = makeAST(program, true);
const ast = makeAST(program);
expect(hasDefaultExport(ast)).toBe(expectedResult);
});

Expand Down Expand Up @@ -95,6 +95,6 @@ test.each([
['export { default } from "module-name";', []],
['export { default, name1 } from "module-name";', ['name1']],
])('getExportIdentifiers(%s) should return %p', (program, expectedIdentifiers) => {
const ast = makeAST(program, true);
const ast = makeAST(program);
expect(getExportIdentifierNames(ast)).toStrictEqual(expectedIdentifiers);
});