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

For typescript, retry without jsx plugin in case of error #807

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

znarf
Copy link
Collaborator

@znarf znarf commented Jun 8, 2023

No description provided.

@rumpl
Copy link
Member

rumpl commented Jun 8, 2023

I've seen a lot of React code in the wild that use .ts for their components, wouldn't this break depcheck for all of these users?

@znarf
Copy link
Collaborator Author

znarf commented Jun 8, 2023

@rumpl You're probably right, if that's the case then I have an alternative approach to propose.

We can first parse with the jsx plugin activated. And if we get an exception, we can then try to parse without the jsx plugin.

@rumpl
Copy link
Member

rumpl commented Jun 8, 2023

We can first parse with the jsx plugin activated. And if we get an exception, we can then try to parse without the jsx plugin.

Sounds good to me!

@rumpl
Copy link
Member

rumpl commented Jun 8, 2023

@znarf What was the rationale for this PR though? Are there cases where the parser with the jsx plugin would fail to parse a valid typescript file?

@znarf
Copy link
Collaborator Author

znarf commented Jun 9, 2023

In my codebase at https://github.com/opencollective/opencollective-api, I get the following errors when the jsx plugin is active, it works just fine without.

depcheck:checkFile:error /Dev/opencollective/api/test/test-helpers/fake-data.ts [AsyncFunction: parseTypescript] Unexpected token, expected "}" (317:81) +0ms
depcheck:checkFile:error /Dev/opencollective/api/test/stories/ledger.test.ts [AsyncFunction: parseTypescript] Unexpected token, expected "}" (132:6) +0ms
depcheck:checkFile:error /Dev/opencollective/api/test/server/paymentProviders/paypal/webhook.test.ts [AsyncFunction: parseTypescript] Unexpected token, expected "}" (52:12) +0ms
depcheck:checkFile:error /Dev/opencollective/api/test/server/lib/merge-accounts.test.ts [AsyncFunction: parseTypescript] Unexpected token (222:6) +0ms
depcheck:checkFile:error /Dev/opencollective/api/test/server/lib/oauth/model.test.ts [AsyncFunction: parseTypescript] Unexpected token, expected "}" (76:98) +0ms
depcheck:checkFile:error /Dev/opencollective/api/test/server/graphql/loaders/helpers.test.ts [AsyncFunction: parseTypescript] Unexpected token (98:6) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/paymentProviders/transferwise/index.ts [AsyncFunction: parseTypescript] Unexpected token (80:28) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/paymentProviders/stripe/virtual-cards.ts [AsyncFunction: parseTypescript] Unexpected token, expected "}" (87:16) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/paymentProviders/stripe/webhook.ts [AsyncFunction: parseTypescript] Unexpected token (718:4) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/paymentProviders/paypal/webhook.ts [AsyncFunction: parseTypescript] Unexpected token (212:45) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/paymentProviders/paypal/payment.ts [AsyncFunction: parseTypescript] Unexpected token, expected "}" (234:20) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/paymentProviders/paypal/subscription.ts [AsyncFunction: parseTypescript] Unexpected token, expected "}" (182:47) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/models/Collective.ts [AsyncFunction: parseTypescript] Unexpected token (503:6) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/models/HostApplication.ts [AsyncFunction: parseTypescript] Unexpected token (49:23) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/models/PayoutMethod.ts [AsyncFunction: parseTypescript] Unexpected token (118:35) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/lib/awsS3.ts [AsyncFunction: parseTypescript] Unexpected token (45:24) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/lib/data.ts [AsyncFunction: parseTypescript] Unterminated JSX contents. (66:42) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/lib/paypal.ts [AsyncFunction: parseTypescript] Unexpected token (195:23) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/lib/transferwise.ts [AsyncFunction: parseTypescript] Unexpected token, expected "..." (58:29) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/lib/oauth/index.ts [AsyncFunction: parseTypescript] Unexpected token, expected "}" (86:5) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/lib/oauth/model.ts [AsyncFunction: parseTypescript] Unexpected token, expected "}" (92:14) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/graphql/v2/query/PaypalPlanQuery.ts [AsyncFunction: parseTypescript] Unexpected token, expected "}" (56:74) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/graphql/v2/query/TierQuery.ts [AsyncFunction: parseTypescript] Unexpected token, expected "}" (23:49) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/graphql/v2/mutation/GuestMutations.ts [AsyncFunction: parseTypescript] Unexpected token, expected "}" (70:9) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/graphql/v2/interface/AccountWithContributions.ts [AsyncFunction: parseTypescript] Unexpected token, expected "}" (70:20) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/graphql/v2/input/ExpenseReferenceInput.ts [AsyncFunction: parseTypescript] Unexpected token (30:4) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/graphql/v2/enum/TierFrequency.ts [AsyncFunction: parseTypescript] Unexpected token (34:2) +0ms
depcheck:checkFile:error /Dev/opencollective/api/server/graphql/common/expenses.ts [AsyncFunction: parseTypescript] Unexpected token (993:75) +0ms
depcheck:checkFile:error /Dev/opencollective/api/scripts/paypal/cancel-all-subscriptions.ts [AsyncFunction: parseTypescript] Unexpected token (107:64) +0ms
depcheck:checkFile:error /Dev/opencollective/api/scripts/paypal/payment-reconciliator.ts [AsyncFunction: parseTypescript] Unexpected token (35:18) +0ms

@znarf znarf changed the title When parsing typescript, only load babel jsx plugin when the file is .tsx For typescript, retry without jsx plugin in case of error Jun 9, 2023
@rumpl
Copy link
Member

rumpl commented Jun 9, 2023

Oooh, it's because of things like this return <string[]>options['hosts']; isn't this the deprecated way of doing casts? Sorry, my TS is rusty

@znarf
Copy link
Collaborator Author

znarf commented Jun 9, 2023

It's valid but not compatible with jsx and there is no point updating the syntax on our side, it's a server side project. Some related discussion: microsoft/playwright#21462

So, we need a way to skip jsx in projects that don't expect it. Or use the latest approach that I submitted, retry without jsx if there is an error.

@simllll
Copy link
Contributor

simllll commented Aug 8, 2023

Experience the same thing rgith now, the jsx parsing fails silently and therefore depcheck reports issues that are non existing.

TSC FAILED SyntaxError: Unexpected token (32:2)
    at Parser._raise (/home/simon/.nvm/versions/node/v18.16.1/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:541:17)
    at Parser.raiseWithData (/home/simon/.nvm/versions/node/v18.16.1/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:534:17)
    at Parser.raise (/home/simon/.nvm/versions/node/v18.16.1/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:495:17)
    at Parser.unexpected (/home/simon/.nvm/versions/node/v18.16.1/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:3580:16)
    at Parser.parseExprAtom (/home/simon/.nvm/versions/node/v18.16.1/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:12026:22)
    at Parser.parseExprAtom (/home/simon/.nvm/versions/node/v18.16.1/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:7477:20)
    at Parser.parseExprSubscripts (/home/simon/.nvm/versions/node/v18.16.1/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:11584:23)
    at Parser.parseUpdate (/home/simon/.nvm/versions/node/v18.16.1/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:11564:21)
    at Parser.parseMaybeUnary (/home/simon/.nvm/versions/node/v18.16.1/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:11539:23)
    at Parser.parseMaybeUnary (/home/simon/.nvm/versions/node/v18.16.1/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:9918:20) {
  loc: Position { line: 32, column: 2 },
  pos: 1079,
  code: 'BABEL_PARSER_SYNTAX_ERROR',
  reasonCode: 'UnexpectedToken'
}

removing jsx check, solves it. what is blokcing this PR? :)

we are working around with tit by using a custom patch, but less than ideal.

Furthermore we have another isuse, the babel/parser that is used is fixed to 7.16.4, which does not support e.g. satisfies operator.
Therefore we also updated the dep to @babel/parser 7.22.x

the patch we are using:

diff --git a/dist/parser/typescript.js b/dist/parser/typescript.js
index 862f9980aabfd3eaef6d7c1a2935f8b09b532447..e2415f96e5022cf425046cc3b0d26ec0cf9ac28e 100644
--- a/dist/parser/typescript.js
+++ b/dist/parser/typescript.js
@@ -14,15 +14,20 @@ async function parseTypescript(filename) {
   // Because we only parse them, not evaluate any code, it is safe to do so.
   // note that babel/parser 7+ does not support *, due to plugin incompatibilities
 
+	try {
   return (0, _parser.parse)(content, {
     sourceType: 'module',
-    plugins: ['typescript', 'jsx', 'asyncGenerators', 'bigInt', 'classProperties', 'classPrivateProperties', 'classPrivateMethods', // { decorators: { decoratorsBeforeExport: true } },
-    'decorators-legacy', 'doExpressions', 'dynamicImport', 'exportDefaultFrom', 'exportNamespaceFrom', 'functionBind', 'functionSent', 'importMeta', 'logicalAssignment', 'nullishCoalescingOperator', 'numericSeparator', 'objectRestSpread', 'optionalCatchBinding', 'optionalChaining', {
-      pipelineOperator: {
+    plugins: ['typescript', 'asyncGenerators', 'bigInt', 'classProperties', 'classPrivateProperties', 'classPrivateMethods', // { decorators: { decoratorsBeforeExport: true } },
+    'decorators-legacy', 'doExpressions', 'dynamicImport', 'exportDefaultFrom', 'exportNamespaceFrom', 'functionBind', 'functionSent', 'importMeta', 'logicalAssignment', 'nullishCoalescingOperator', 'numericSeparator', 'objectRestSpread', 'optionalCatchBinding', 'optionalChaining', 'importAssertions', [
+      'pipelineOperator', {
         proposal: 'minimal'
-      }
-    }, 'throwExpressions']
+      }]
+    , 'throwExpressions']
   });
+	} catch (err) {
+		console.error('tsc parsing failed', filename, err);
+		throw err;
+	}
 }
 
 module.exports = exports.default;
diff --git a/package.json b/package.json
index 9e4a6236868994b460a799c09edd1688c6bfde0e..d204b9330ddcdc836800dadee830952c962298ff 100644
--- a/package.json
+++ b/package.json
@@ -49,7 +49,7 @@
   "license": "MIT",
   "readmeFilename": "README.md",
   "dependencies": {
-    "@babel/parser": "7.16.4",
+    "@babel/parser": "^7.22.0",
     "@babel/traverse": "^7.12.5",
     "@vue/compiler-sfc": "^3.0.5",
     "camelcase": "^6.2.0",

@znarf
Copy link
Collaborator Author

znarf commented Jan 13, 2024

Would be great to merge that or the similar solution proposed at #849

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants