Skip to content

Commit 9eb9447

Browse files
committed
always use tsx parser
1 parent 64c7204 commit 9eb9447

File tree

3 files changed

+9
-27
lines changed

3 files changed

+9
-27
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
//
@@ -54,12 +54,10 @@ type VariableDeclarationNode = jscsTypes.VariableDeclaration;
5454
* Create an AST based on the given code.
5555
*
5656
* @param code The code to convert to an AST.
57-
* @param isTS Flag indicating what parser to use.
58-
* @throws Parsing error if the code is unparsable
57+
* @throws Throws parsing error if the code is unparsable
5958
* @returns The AST
6059
*/
61-
export function makeAST(code: string, isTS: boolean): AST {
62-
const parser = isTS ? makeParser('tsx') : makeParser('jsx');
60+
export function makeAST(code: string): AST {
6361
// If this errors, it will be caught in the calling function, where we know more information and can construct a
6462
// better warning message
6563
return jscs(code, { parser });

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ type LoaderOptions = {
4141
*/
4242
function wrapFunctions(userCode: string, templateCode: string, filepath: string): string[] {
4343
let userAST: AST, templateAST: AST;
44-
const isTS = new RegExp('\\.tsx?$').test(filepath);
4544

4645
try {
47-
userAST = makeAST(userCode, isTS);
48-
templateAST = makeAST(templateCode, false);
46+
userAST = makeAST(userCode);
47+
templateAST = makeAST(templateCode);
4948
} catch (err) {
5049
logger.warn(`Couldn't add Sentry to ${filepath} because there was a parsing error: ${err}`);
5150
// Replace the template code with an empty string, so in the end the user code is untouched

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-
}

0 commit comments

Comments
 (0)