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

chore: refactor imports and exports to explicit form #2485

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { render } from 'react-dom';
import { AppType } from '../types';
import { styles } from './styles';
import { withStyles } from '@material-ui/core';
Expand Down Expand Up @@ -52,7 +52,7 @@ loadFromStorage()

const StyledApp = withStyles(styles)(App);

ReactDOM.render(
render(
<StyledApp
settings={storage.settings}
isPermissionAlertDismissed={storage.isPermissionAlertDismissed}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/* eslint-disable node/no-extraneous-import */

import { capitalCase } from 'change-case';
import * as json5 from 'json5';
import {parse} from 'json5';

// From https://github.com/TypeStrong/ts-loader/blob/main/src/interfaces.ts
interface WebpackLoaderContext {
Expand All @@ -35,7 +35,7 @@ export default function (this: WebpackLoaderContext, source: string): string {
const p = require('../../package.json');
const options = this.getOptions();

const manifest5 = json5.parse(source);
const manifest5 = parse(source);

const sizes = ['16', '32', '48', '128'];
manifest5.icons = sizes.reduce((result: IconSet, size: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import { ProgrammaticContentScriptInjector } from '../src/background/ProgrammaticContentScriptInjector';
import * as chromeMock from 'sinon-chrome';
import * as assert from 'assert';
import {ok,deepStrictEqual} from 'assert';
import sinon = require('sinon');

import { TAB_ID } from './utils';
Expand All @@ -41,7 +41,7 @@ describe('ProgrammaticContentScriptInjector', () => {
});

it('should subscribe on chrome.tabs.onUpdated', () => {
assert.ok(chromeMock.tabs.onUpdated.addListener.calledOnce);
ok(chromeMock.tabs.onUpdated.addListener.calledOnce);
});

it('should only be triggered on tab status "loading"', () => {
Expand All @@ -52,14 +52,14 @@ describe('ProgrammaticContentScriptInjector', () => {
chromeMock.tabs.onUpdated.dispatch(TAB_ID, {
status: TabStatus.UNLOADED,
});
assert.ok(spy.notCalled);
assert.ok(chromeMock.tabs.get.notCalled);
ok(spy.notCalled);
ok(chromeMock.tabs.get.notCalled);

chromeMock.tabs.onUpdated.dispatch(TAB_ID, {
status: TabStatus.LOADING,
});
assert.ok(spy.calledOnce, 'injectContentScript not triggered on "loading"');
assert.ok(chromeMock.tabs.get.calledOnce);
ok(spy.calledOnce, 'injectContentScript not triggered on "loading"');
ok(chromeMock.tabs.get.calledOnce);
});

it('should inject the content script if the url property of a tab is accessible', () => {
Expand All @@ -78,13 +78,13 @@ describe('ProgrammaticContentScriptInjector', () => {
status: TabStatus.LOADING,
});

assert.ok(chromeMock.tabs.executeScript.notCalled);
ok(chromeMock.tabs.executeScript.notCalled);

chromeMock.tabs.onUpdated.dispatch(TAB_ID, {
status: TabStatus.LOADING,
});

assert.ok(
ok(
chromeMock.tabs.executeScript.calledOnceWith(TAB_ID, {
file: CONTENT_SCRIPT_NAME,
allFrames: true,
Expand All @@ -99,7 +99,7 @@ describe('ProgrammaticContentScriptInjector', () => {

chromeMockV3.scripting = {
executeScript: args => {
assert.deepStrictEqual(args, {
deepStrictEqual(args, {
target: {
allFrames: true,
tabId: TAB_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

/* eslint-disable node/no-unpublished-import */

import * as chromeMock from 'sinon-chrome';
import * as assert from 'assert';
import * as sinon from 'sinon';
import {reset,storage,chromeMock,runtime} from 'sinon-chrome';
import {ok,deepStrictEqual} from 'assert';
import {SinonSandbox,createSandbox} from 'sinon';

import { InstrumentationInjector } from '../src/contentScript/InstrumentationInjector';
import { JSDOM } from 'jsdom';
Expand All @@ -31,12 +31,12 @@ import {
import { TEST_URL } from './utils';

describe('InstrumentationInjector', () => {
let sandbox: sinon.SinonSandbox;
let sandbox: SinonSandbox;
let injector: InstrumentationInjector;
let jsdom: JSDOM;

beforeEach(() => {
sandbox = sinon.createSandbox();
sandbox = createSandbox();
jsdom = new JSDOM('<!doctype html><html><body></body></html>', {
url: TEST_URL,
});
Expand All @@ -51,36 +51,36 @@ describe('InstrumentationInjector', () => {

afterEach(async () => {
sandbox.restore();
chromeMock.reset();
reset();
});

describe('checkUrlFilter', () => {
it('matches on parts of the URL', () => {
assert.ok(
ok(
InstrumentationInjector.checkUrlFilter(
'example',
'http://www.example.com'
)
);

assert.ok(
ok(
InstrumentationInjector.checkUrlFilter(
'www.exa',
'http://www.example.com'
)
);

assert.ok(
ok(
!InstrumentationInjector.checkUrlFilter('123', 'http://www.example.com')
);
});

it('accepts "*" as a catch all', () => {
assert.ok(
ok(
InstrumentationInjector.checkUrlFilter('*', 'http://www.example.com')
);

assert.ok(
ok(
InstrumentationInjector.checkUrlFilter(
'*',
'http://www.opentelemetry.io'
Expand All @@ -92,49 +92,49 @@ describe('InstrumentationInjector', () => {
describe('execute', () => {
it('should load settings from storage', () => {
injector.execute();
assert.ok(chromeMock.storage.local.get.calledOnceWith('settings'));
ok(storage.local.get.calledOnceWith('settings'));
});

it('should only inject instrumentation if urlFilter matches', () => {
const spy = sandbox.spy(injector, 'inject');
chromeMock.storage.local.get.onFirstCall().callsArgWith(1, {
storage.local.get.onFirstCall().callsArgWith(1, {
settings: {
urlFilter: '123',
},
});
chromeMock.storage.local.get.onSecondCall().callsArgWith(1, {
storage.local.get.onSecondCall().callsArgWith(1, {
settings: {
urlFilter: 'example',
},
});

injector.execute();
assert.ok(spy.notCalled);
ok(spy.notCalled);

injector.execute();
assert.ok(spy.calledOnce);
ok(spy.calledOnce);
});
});

describe('inject', () => {
it('adds a script element to the DOM that loads the instrumentation code', () => {
const scriptName = `chrome-extension://id/${INSTRUMENTATION_SCRIPT_NAME}`;

chromeMock.runtime.getURL.onFirstCall().returns(scriptName);
runtime.getURL.onFirstCall().returns(scriptName);

const settings = { exporters: {} };
injector.inject(settings as Settings);
const configTag = jsdom.window.document.getElementById(
DomElements.CONFIG_TAG
);
assert.ok(configTag instanceof jsdom.window.HTMLScriptElement);
assert.deepStrictEqual(
ok(configTag instanceof jsdom.window.HTMLScriptElement);
deepStrictEqual(
settings,
JSON.parse(
String(configTag.getAttribute(`data-${DomAttributes.CONFIG}`))
)
);
assert.ok(configTag.getAttribute('src'), scriptName);
ok(configTag.getAttribute('src'), scriptName);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

/* eslint-disable node/no-unpublished-import */

import * as chromeMock from 'sinon-chrome';
import * as assert from 'assert';
import * as sinon from 'sinon';
import {reset} from 'sinon-chrome';
import {ok} from 'assert';
import {SinonSandbox,createSandbox,spy} from 'sinon';
import { WebInstrumentation } from '../src/instrumentation/WebInstrumentation';
import {
ExporterType,
Expand All @@ -30,11 +30,11 @@ import { JSDOM } from 'jsdom';
import { TEST_URL } from './utils';

describe('WebInstrumentation', () => {
let sandbox: sinon.SinonSandbox;
let sandbox: SinonSandbox;
let provider: WebTracerProvider;

beforeEach(() => {
sandbox = sinon.createSandbox();
sandbox = createSandbox();
provider = new WebTracerProvider();
const { window } = new JSDOM('<!doctype html><html><body></body></html>', {
url: TEST_URL,
Expand All @@ -51,7 +51,7 @@ describe('WebInstrumentation', () => {
});

it('adds exporters to the trace provider', () => {
const addSpanProcessorSpy = sinon.spy(provider, 'addSpanProcessor');
const addSpanProcessorSpy = spy(provider, 'addSpanProcessor');
const instrumentation = new WebInstrumentation(
{
exporters: {
Expand Down Expand Up @@ -83,6 +83,6 @@ describe('WebInstrumentation', () => {
provider
);
instrumentation.register();
assert.ok(addSpanProcessorSpy.callCount === 3);
ok(addSpanProcessorSpy.callCount === 3);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

/* eslint-disable node/no-unpublished-import */

import * as path from 'path';
import {resolve} from 'path';
import { mergeWithRules } from 'webpack-merge';
import * as HtmlWebpackPlugin from 'html-webpack-plugin';
import {HtmlWebpackPlugin as HtmlWebpackPlugin} from 'html-webpack-plugin';

// Read the environment variables, and check for the existence of the "MV" variable
// This can be used to only build the one or the other target.
Expand All @@ -34,23 +34,23 @@ module.exports = (env: { MV?: string; WEBPACK_BUILD: boolean }) => {
module: {
rules: [
{
include: [path.resolve(__dirname, 'src/manifest.json5')],
include: [resolve(__dirname, 'src/manifest.json5')],
test: /manifest.json5$/,
use: [
{
loader: 'null-loader',
options: {},
},
{
loader: path.resolve('src/utils/manifest-loader.ts'),
loader: resolve('src/utils/manifest-loader.ts'),
options: {
manifestVersion: 2,
},
},
],
},
{
include: [path.resolve(__dirname, 'src')],
include: [resolve(__dirname, 'src')],
test: /\.tsx?$/,
use: [
{
Expand All @@ -63,7 +63,7 @@ module.exports = (env: { MV?: string; WEBPACK_BUILD: boolean }) => {
],
},
{
include: [path.resolve(__dirname, 'src/icons')],
include: [resolve(__dirname, 'src/icons')],
test: /\.(jpe?g|png|webp)$/i,
use: [
// We are not going to use any of the images for real, throw away all output
Expand Down Expand Up @@ -118,18 +118,18 @@ module.exports = (env: { MV?: string; WEBPACK_BUILD: boolean }) => {
const targetMV2 = merge(baseConfig, {
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'build/mv2'),
path: resolve(__dirname, 'build/mv2'),
},
});
const targetMV3 = merge(baseConfig, {
module: {
rules: [
{
include: [path.resolve(__dirname, 'src/manifest.json5')],
include: [resolve(__dirname, 'src/manifest.json5')],
test: /manifest.json5$/,
use: [
{
loader: path.resolve('src/utils/manifest-loader.ts'),
loader: resolve('src/utils/manifest-loader.ts'),
options: {
manifestVersion: 3,
},
Expand All @@ -140,7 +140,7 @@ module.exports = (env: { MV?: string; WEBPACK_BUILD: boolean }) => {
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'build/mv3'),
path: resolve(__dirname, 'build/mv3'),
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
SEMRESATTRS_HOST_TYPE,
} from '@opentelemetry/semantic-conventions';

import * as http from 'http';
import { RequestOptions, request } from 'http';

/**
* The AlibabaCloudEcsDetector can be used to detect if a process is running in
Expand Down Expand Up @@ -121,13 +121,13 @@ class AlibabaCloudEcsDetector implements DetectorSync {
return await this._fetchString(options);
}

private async _fetchString(options: http.RequestOptions): Promise<string> {
private async _fetchString(options: RequestOptions): Promise<string> {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
req.destroy(new Error('ECS metadata api request timed out.'));
}, this.MILLISECONDS_TIME_OUT);

const req = http.request(options, res => {
const req = request(options, res => {
clearTimeout(timeoutId);
const { statusCode } = res;
if (
Expand Down
Loading