Skip to content

Commit

Permalink
Upgrade @vercel/style-guide (#110)
Browse files Browse the repository at this point in the history
* Upgrade @vercel/style-guide

* Run prettier
  • Loading branch information
tobiaslins authored Oct 6, 2023
1 parent c8a5031 commit c57f273
Show file tree
Hide file tree
Showing 13 changed files with 2,285 additions and 444 deletions.
2 changes: 1 addition & 1 deletion apps/nextjs/app/api/edge-no-context/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function handler(request: Request) {
},
{
request,
},
}
);

return new Response('OK');
Expand Down
2 changes: 1 addition & 1 deletion apps/nextjs/components/withAnalytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';

export function withAnalytics<P extends Omit<AnalyticsProps, 'mode'>>(
Component: React.ComponentType,
props?: P,
props?: P
) {
function WithAnalytics(props?: P) {
return (
Expand Down
2 changes: 2 additions & 0 deletions apps/nextjs/e2e/development/pageview.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ test.describe('pageview', () => {
page.on('console', (msg) => {
const message = msg.text();

console.log(message);

if (
message.includes('[Vercel Web Analytics]') ||
message.includes('[Vercel Analytics]')
Expand Down
2 changes: 1 addition & 1 deletion apps/nextjs/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function middleware(request: NextRequest, event: NextFetchEvent) {
track('Redirect', {
path: request.nextUrl.pathname,
type: 'waitUntil',
}),
})
);
return NextResponse.redirect(new URL('/server-actions', request.url));
}
Expand Down
2 changes: 1 addition & 1 deletion apps/nextjs/pages/api/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async function handler(request: NextRequest, event: NextFetchEvent) {
track('Pages Api Route', {
runtime: 'edge',
router: 'pages',
}),
})
);

return new Response('OK');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"lint-staged": "^13.2.2"
},
"devDependencies": {
"@vercel/style-guide": "^4.0.2",
"@vercel/style-guide": "^5.0.1",
"husky": "^8.0.3",
"prettier": "2.8.8",
"typescript": "^5.1.6"
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"private": true,
"main": "index.js",
"dependencies": {
"@next/eslint-plugin-next": "13.4.7",
"@vercel/style-guide": "4.0.2"
"@next/eslint-plugin-next": "13.4.7"
},
"devDependencies": {
"@vercel/style-guide": "5.0.1",
"eslint": "8.43.0",
"prettier": "2.8.8",
"typescript": "5.1.3"
Expand Down
14 changes: 7 additions & 7 deletions packages/web/src/generic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('inject', () => {
}

expect(script.src).toEqual(
'https://va.vercel-scripts.com/v1/script.debug.js',
'https://va.vercel-scripts.com/v1/script.debug.js'
);
expect(script).toHaveAttribute('defer');
});
Expand Down Expand Up @@ -95,10 +95,10 @@ describe('track custom events', () => {
track('custom event', {
string: 'string',
number: 1,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- This is intentional
nested: {
object: '',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- This is intentional
} as any,
});

Expand All @@ -125,7 +125,7 @@ describe('track custom events', () => {
inject({
mode: 'development',
});
// eslint-disable-next-line @typescript-eslint/no-empty-function
// eslint-disable-next-line @typescript-eslint/no-empty-function -- This is intentional
jest.spyOn(global.console, 'error').mockImplementation(() => {});
});

Expand Down Expand Up @@ -171,14 +171,14 @@ describe('track custom events', () => {
track('custom event', {
string: 'string',
number: 1,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- This is intentional
nested: {
object: '',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- This is intentional
} as any,
});

// eslint-disable-next-line no-console
// eslint-disable-next-line no-console -- Logging to console is intentional
expect(console.error).toHaveBeenCalledTimes(1);
});
});
Expand Down
14 changes: 7 additions & 7 deletions packages/web/src/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
function inject(
props: AnalyticsProps = {
debug: true,
},
}
): void {
if (!isBrowser()) return;

Expand Down Expand Up @@ -51,9 +51,9 @@ function inject(
? 'Please check if any ad blockers are enabled and try again.'
: 'Be sure to enable Web Analytics for your project and deploy again. See https://vercel.com/docs/analytics/quickstart for more information.';

// eslint-disable-next-line no-console
// eslint-disable-next-line no-console -- Logging to console is intentional
console.log(
`[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`,
`[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`
);
};

Expand All @@ -72,14 +72,14 @@ function inject(
*/
function track(
name: string,
properties?: Record<string, AllowedPropertyValues>,
properties?: Record<string, AllowedPropertyValues>
): void {
if (!isBrowser()) {
const msg =
'[Vercel Web Analytics] Please import `track` from `@vercel/analytics/server` when using this function in a server environment';

if (isProduction()) {
// eslint-disable-next-line no-console
// eslint-disable-next-line no-console -- Show warning in production
console.warn(msg);
} else {
throw new Error(msg);
Expand All @@ -104,7 +104,7 @@ function track(
});
} catch (err) {
if (err instanceof Error && isDevelopment()) {
// eslint-disable-next-line no-console
// eslint-disable-next-line no-console -- Logging to console is intentional
console.error(err);
}
}
Expand All @@ -113,7 +113,7 @@ function track(
export { inject, track };
export type { AnalyticsProps };

// eslint-disable-next-line import/no-default-export
// eslint-disable-next-line import/no-default-export -- Default export is intentional
export default {
inject,
track,
Expand Down
14 changes: 6 additions & 8 deletions packages/web/src/react.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ import { cleanup, render } from '@testing-library/react';
import { Analytics, track } from './react';

describe('<Analytics />', () => {
afterEach(() => cleanup());
afterEach(() => {
cleanup();
});

describe('in development mode', () => {
it('should add the script tag correctly', () => {
render(<Analytics mode="development" />);

// eslint-disable-next-line testing-library/no-node-access
const scripts = document.getElementsByTagName('script');
expect(scripts).toHaveLength(1);

// eslint-disable-next-line testing-library/no-node-access
const script = document.head.querySelector('script');

if (!script) {
throw new Error('Could not find script tag');
}

expect(script.src).toEqual(
'https://va.vercel-scripts.com/v1/script.debug.js',
'https://va.vercel-scripts.com/v1/script.debug.js'
);
expect(script).toHaveAttribute('defer');
});
Expand All @@ -31,11 +31,9 @@ describe('<Analytics />', () => {
it('should add the script tag correctly', () => {
render(<Analytics mode="production" />);

// eslint-disable-next-line testing-library/no-node-access
const scripts = document.getElementsByTagName('script');
expect(scripts).toHaveLength(1);

// eslint-disable-next-line testing-library/no-node-access
const script = document.head.querySelector('script');

if (!script) {
Expand Down Expand Up @@ -101,10 +99,10 @@ describe('<Analytics />', () => {
track('custom event', {
string: 'string',
number: 1,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- Intentional to trigger error
nested: {
object: '',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Allow
} as any,
});

Expand Down
11 changes: 5 additions & 6 deletions packages/web/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable no-console */
/* eslint-disable no-console -- Allow loggin on the server */
import 'server-only';
import type { AllowedPropertyValues } from '../types';
import { isProduction, parseProperties } from '../utils';
Expand Down Expand Up @@ -37,7 +36,7 @@ const symbol = Symbol.for('@vercel/request-context');
export async function track(
eventName: string,
properties?: Record<string, AllowedPropertyValues>,
context?: Context,
context?: Context
): Promise<void> {
const props = parseProperties(properties, {
strip: isProduction(),
Expand All @@ -46,13 +45,13 @@ export async function track(
if (!ENDPOINT) {
if (isProduction()) {
console.log(
`[Vercel Web Analytics] Can't find VERCEL_URL in environment variables.`,
`[Vercel Web Analytics] Can't find VERCEL_URL in environment variables.`
);
} else if (!DISABLE_LOGS) {
console.log(
`[Vercel Web Analytics] Track "${eventName}" ${
props ? `with data ${JSON.stringify(props)}` : ''
}`,
}`
);
}
return;
Expand Down Expand Up @@ -99,7 +98,7 @@ export async function track(

if (!hasHeaders) {
throw new Error(
'No session context found. Pass `request` or `headers` to the `track` function.',
'No session context found. Pass `request` or `headers` to the `track` function.'
);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function isDevelopment(): boolean {

function removeKey(
key: string,
{ [key]: _, ...rest },
{ [key]: _, ...rest }
): Record<string, unknown> {
return rest;
}
Expand All @@ -49,7 +49,7 @@ export function parseProperties(
properties: Record<string, unknown> | undefined,
options: {
strip?: boolean;
},
}
): Error | Record<string, AllowedPropertyValues> | undefined {
if (!properties) return undefined;
let props = properties;
Expand All @@ -67,8 +67,8 @@ export function parseProperties(
if (errorProperties.length > 0 && !options.strip) {
throw Error(
`The following properties are not valid: ${errorProperties.join(
', ',
)}. Only strings, numbers, booleans, and null are allowed.`,
', '
)}. Only strings, numbers, booleans, and null are allowed.`
);
}
return props as Record<string, AllowedPropertyValues>;
Expand Down
Loading

0 comments on commit c57f273

Please sign in to comment.