Skip to content

Commit a6eee54

Browse files
authored
ref(nextjs): Always use tsx parser in experimental function-wrapping loader (#5563)
The changes the experimental data-fetching-function-wrapping loader to always use the tsx parser, as it's capable of parsing jsx as well. This in turn allows us to simplify the `makeAST` function.
1 parent b24c7f2 commit a6eee54

File tree

4 files changed

+12
-31
lines changed

4 files changed

+12
-31
lines changed

packages/nextjs/src/config/loaders/ast.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import * as jscsTypes from 'jscodeshift';
33
import { default as jscodeshiftDefault } from 'jscodeshift';
44

5-
import { makeParser } from './parsers';
5+
import { parser } from './parser';
66

77
// In `jscodeshift`, the exports look like this:
88
//
@@ -64,12 +64,10 @@ type VariableDeclarationNode = jscsTypes.VariableDeclaration;
6464
* Create an AST based on the given code.
6565
*
6666
* @param code The code to convert to an AST.
67-
* @param isTS Flag indicating what parser to use.
68-
* @throws Parsing error if the code is unparsable
67+
* @throws Throws parsing error if the code is unparsable
6968
* @returns The AST
7069
*/
71-
export function makeAST(code: string, isTS: boolean): AST {
72-
const parser = isTS ? makeParser('tsx') : makeParser('jsx');
70+
export function makeAST(code: string): AST {
7371
// If this errors, it will be caught in the calling function, where we know more information and can construct a
7472
// better warning message
7573
return jscs(code, { parser });

packages/nextjs/src/config/loaders/dataFetchersLoader.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@ type LoaderOptions = {
5252
*/
5353
function wrapFunctions(userCode: string, templateCode: string, filepath: string): string[] {
5454
let userAST: AST, templateAST: AST;
55-
const isTS = new RegExp('\\.tsx?$').test(filepath);
5655

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

126125
if (!this.resourceQuery.includes('sentry-proxy-loader')) {
127-
const ast = makeAST(userCode, true); // is there a reason to ever parse without typescript?
128-
126+
const ast = makeAST(userCode);
129127
const exportedIdentifiers = getExportIdentifierNames(ast);
130128

131129
let outputFileContent = '';

packages/nextjs/src/config/loaders/parsers.ts renamed to packages/nextjs/src/config/loaders/parser.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type Parser = {
1313
parse: (code: string) => babel.ParseResult<File>;
1414
};
1515

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

46-
const tsxOptions = {
47-
...jsxOptions,
48-
// Because `jsxOptions` is typed as a `ParserOptions` object, TS doesn't discount the possibility of its `plugins`
49-
// property being undefined, even though it is, in fact, very clearly defined - hence the empty array.
50-
plugins: [...(jsxOptions.plugins || []), 'typescript'] as babel.ParserPlugin[],
47+
export const parser: Parser = {
48+
parse: code => babel.parse(code, options),
5149
};
52-
53-
/**
54-
* Create either a jsx or tsx parser to be used by `jscodeshift`.
55-
*
56-
* @param type Either 'jsx' or 'tsx'
57-
* @returns An object with the appropriate `parse` method.
58-
*/
59-
export function makeParser(type: 'jsx' | 'tsx'): Parser {
60-
const options = type === 'jsx' ? jsxOptions : tsxOptions;
61-
return {
62-
parse: code => babel.parse(code, options),
63-
};
64-
}

packages/nextjs/test/config/ast.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test.each([
3535
['export { default } from "module-name";', true],
3636
['export { default, name1 } from "module-name";', true],
3737
])('hasDefaultExport(%s) should return %p', (program, expectedResult) => {
38-
const ast = makeAST(program, true);
38+
const ast = makeAST(program);
3939
expect(hasDefaultExport(ast)).toBe(expectedResult);
4040
});
4141

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

0 commit comments

Comments
 (0)