diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index 87a3fdae..999c3948 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -2305,7 +2305,14 @@ export class JavaScriptParserVisitor { } visitMetaProperty(node: ts.MetaProperty) { - return this.visitUnknown(node); + return new J.FieldAccess( + randomId(), + this.prefix(node), + Markers.EMPTY, + node.keywordToken === ts.SyntaxKind.NewKeyword ? this.mapIdentifier(node, 'new') : this.mapIdentifier(node, 'import'), + this.leftPadded(this.prefix(node.getChildAt(1, this.sourceFile)), this.convert(node.name)), + this.mapType(node) + ); } visitSyntheticExpression(node: ts.SyntheticExpression) { diff --git a/openrewrite/test/javascript/e2e/twenty.files.test.ts b/openrewrite/test/javascript/e2e/twenty.files.test.ts index 16408eba..a74f2bbf 100644 --- a/openrewrite/test/javascript/e2e/twenty.files.test.ts +++ b/openrewrite/test/javascript/e2e/twenty.files.test.ts @@ -694,4 +694,113 @@ describe('highlight.js files tests', () => { ); }); + test('packages/twenty-chrome-extension/src/background/index.ts', () => { + rewriteRun( + //language=typescript + typeScript(` + import {isDefined} from '~/utils/isDefined'; + + // Open options page programmatically in a new tab. + // chrome.runtime.onInstalled.addListener((details) => { + // if (details.reason === 'install') { + // openOptionsPage(); + // } + // }); + + chrome.sidePanel.setPanelBehavior({openPanelOnActionClick: true}); + + // This listens for an event from other parts of the extension, such as the content script, and performs the required tasks. + // The cases themselves are labelled such that their operations are reflected by their names. + chrome.runtime.onMessage.addListener((message, _, sendResponse) => { + switch (message.action) { + case 'getActiveTab': { + // e.g. "https://linkedin.com/company/twenty/" + chrome.tabs.query({active: true, currentWindow: true}, ([tab]) => { + if (isDefined(tab) && isDefined(tab.id)) { + sendResponse({tab}); + } + }); + break; + } + case 'openSidepanel': { + chrome.tabs.query({active: true, currentWindow: true}, ([tab]) => { + if (isDefined(tab) && isDefined(tab.id)) { + chrome.sidePanel.open({tabId: tab.id}); + } + }); + break; + } + default: + break; + } + + return true; + }); + + chrome.tabs.onUpdated.addListener(async (tabId, _, tab) => { + const isDesiredRoute = + tab.url?.match(/^https?:\\/\\/(?:www\\.)?linkedin\\.com\\/company(?:\\/\\S+)?/) || + tab.url?.match(/^https?:\\/\\/(?:www\\.)?linkedin\\.com\\/in(?:\\/\\S+)?/); + + if (tab.active === true) { + if (isDefined(isDesiredRoute)) { + chrome.tabs.sendMessage(tabId, {action: 'executeContentScript'}); + } + } + + await chrome.sidePanel.setOptions({ + tabId, + path: tab.url?.match(/^https?:\\/\\/(?:www\\.)?linkedin\\.com/ + ) + ? 'sidepanel.html' + : 'page-inaccessible.html', + enabled + : + true, + }) + ; + }); + + const setTokenStateFromCookie = (cookie: string) => { + const decodedValue = decodeURIComponent(cookie); + const tokenPair = JSON.parse(decodedValue); + if (isDefined(tokenPair)) { + chrome.storage.local.set({ + isAuthenticated: true, + accessToken: tokenPair.accessToken, + refreshToken: tokenPair.refreshToken, + }); + } + }; + + chrome.cookies.onChanged.addListener(async ({cookie}) => { + if (cookie.name === 'tokenPair') { + const store = await chrome.storage.local.get(['clientUrl']); + const clientUrl = isDefined(store.clientUrl) + ? store.clientUrl + : import.meta.env.VITE_FRONT_BASE_URL; + chrome.cookies.get({name: 'tokenPair', url: \`\${clientUrl}\`}, (cookie) => { + if (isDefined(cookie)) { + setTokenStateFromCookie(cookie.value); + } + }); + } + }); + + // This will only run the very first time the extension loads, after we have stored the + // cookiesRead variable to true, this will not allow to change the token state everytime background script runs + chrome.cookies.get( + {name: 'tokenPair', url: \`\${import.meta.env.VITE_FRONT_BASE_URL}\`}, + async (cookie) => { + const store = await chrome.storage.local.get(['cookiesRead']); + if (isDefined(cookie) && !isDefined(store.cookiesRead)) { + setTokenStateFromCookie(cookie.value); + chrome.storage.local.set({cookiesRead: true}); + } + }, + ); + `) + ); + }); + });