Skip to content

Commit

Permalink
add meta props support
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegDokuka committed Dec 5, 2024
1 parent 300f763 commit 7a709b8
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
9 changes: 8 additions & 1 deletion openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
109 changes: 109 additions & 0 deletions openrewrite/test/javascript/e2e/twenty.files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
},
);
`)
);
});

});

0 comments on commit 7a709b8

Please sign in to comment.