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

feat(core): warn when using debug option #364

Merged
merged 6 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module.exports = {
},
},
{
files: ['scripts/**/*'],
files: ['scripts/**/*', '*.config.js'],
rules: {
'import/no-commonjs': 'off',
},
Expand Down
7 changes: 4 additions & 3 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable import/no-commonjs */
const wrapWarningWithDevCheck = require('./scripts/babel/wrap-warning-with-dev-check');

module.exports = (api) => {
const isTest = api.env('test');
Expand All @@ -23,12 +23,13 @@ module.exports = (api) => {
],
],
plugins: clean([
!isTest && [
wrapWarningWithDevCheck,
[
'inline-replace-variables',
{
__DEV__: {
type: 'node',
replacement: "process.env.NODE_ENV === 'development'",
replacement: "process.env.NODE_ENV !== 'production'",
},
},
],
Expand Down
10 changes: 5 additions & 5 deletions bundlesize.config.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"files": [
{
"path": "packages/autocomplete-core/dist/umd/index.js",
"path": "packages/autocomplete-core/dist/umd/index.production.js",
"maxSize": "4.5 kB"
},
{
"path": "packages/autocomplete-js/dist/umd/index.js",
"path": "packages/autocomplete-js/dist/umd/index.production.js",
"maxSize": "8 kB"
},
{
"path": "packages/autocomplete-preset-algolia/dist/umd/index.js",
"path": "packages/autocomplete-preset-algolia/dist/umd/index.production.js",
"maxSize": "1.5 kB"
},
{
"path": "packages/autocomplete-plugin-recent-searches/dist/umd/index.js",
"path": "packages/autocomplete-plugin-recent-searches/dist/umd/index.production.js",
"maxSize": "2.5 kB"
},
{
"path": "packages/autocomplete-plugin-query-suggestions/dist/umd/index.js",
"path": "packages/autocomplete-plugin-query-suggestions/dist/umd/index.production.js",
"maxSize": "2.25 kB"
}
]
Expand Down
2 changes: 0 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable import/no-commonjs */

module.exports = {
rootDir: process.cwd(),
setupFilesAfterEnv: [
Expand Down
8 changes: 4 additions & 4 deletions packages/autocomplete-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"source": "src/index.ts",
"types": "dist/esm/index.d.ts",
"module": "dist/esm/index.js",
"main": "dist/umd/index.js",
"umd:main": "dist/umd/index.js",
"unpkg": "dist/umd/index.js",
"jsdelivr": "dist/umd/index.js",
"main": "dist/umd/index.production.js",
"umd:main": "dist/umd/index.production.js",
"unpkg": "dist/umd/index.production.js",
"jsdelivr": "dist/umd/index.production.js",
"sideEffects": false,
"files": [
"dist/"
Expand Down
15 changes: 2 additions & 13 deletions packages/autocomplete-core/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { plugins } from '../../rollup.base.config';
import { getBundleBanner } from '../../scripts/getBundleBanner';
import { createRollupConfigs } from '../../scripts/rollup/config';

import pkg from './package.json';

export default {
input: 'src/index.ts',
output: {
file: 'dist/umd/index.js',
format: 'umd',
sourcemap: true,
name: pkg.name,
banner: getBundleBanner(pkg),
},
plugins,
};
export default createRollupConfigs({ pkg });
11 changes: 11 additions & 0 deletions packages/autocomplete-core/src/__tests__/checkOptions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createAutocomplete } from '../createAutocomplete';

describe('Validate options', () => {
test('debug option warns about development usage', () => {
expect(() => {
createAutocomplete({ debug: true });
}).toWarnDev(
'[Autocomplete] The `debug` option is meant for development debugging and should not be used in production.'
);
});
});
10 changes: 10 additions & 0 deletions packages/autocomplete-core/src/checkOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { warn } from '@algolia/autocomplete-shared';

import { AutocompleteOptions } from './types';

export function checkOptions<TItem>(option: AutocompleteOptions<TItem>) {
warn(
!option.debug,
'The `debug` option is meant for development debugging and should not be used in production.'
);
}
3 changes: 3 additions & 0 deletions packages/autocomplete-core/src/createAutocomplete.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { checkOptions } from './checkOptions';
import { createStore } from './createStore';
import { getAutocompleteSetters } from './getAutocompleteSetters';
import { getDefaultProps } from './getDefaultProps';
Expand All @@ -14,6 +15,8 @@ export function createAutocomplete<
>(
options: AutocompleteOptions<TItem>
): AutocompleteApi<TItem, TEvent, TMouseEvent, TKeyboardEvent> {
checkOptions(options);

const props = getDefaultProps(options);
const store = createStore(stateReducer, props);

Expand Down
8 changes: 4 additions & 4 deletions packages/autocomplete-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"source": "src/index.ts",
"types": "dist/esm/index.d.ts",
"module": "dist/esm/index.js",
"main": "dist/umd/index.js",
"umd:main": "dist/umd/index.js",
"unpkg": "dist/umd/index.js",
"jsdelivr": "dist/umd/index.js",
"main": "dist/umd/index.production.js",
"umd:main": "dist/umd/index.production.js",
"unpkg": "dist/umd/index.production.js",
"jsdelivr": "dist/umd/index.production.js",
"sideEffects": false,
"files": [
"dist/"
Expand Down
15 changes: 2 additions & 13 deletions packages/autocomplete-js/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { plugins } from '../../rollup.base.config';
import { getBundleBanner } from '../../scripts/getBundleBanner';
import { createRollupConfigs } from '../../scripts/rollup/config';

import pkg from './package.json';

export default {
input: 'src/index.ts',
output: {
file: 'dist/umd/index.js',
format: 'umd',
sourcemap: true,
name: pkg.name,
banner: getBundleBanner(pkg),
},
plugins,
};
export default createRollupConfigs({ pkg });
8 changes: 4 additions & 4 deletions packages/autocomplete-plugin-query-suggestions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"source": "src/index.ts",
"types": "dist/esm/index.d.ts",
"module": "dist/esm/index.js",
"main": "dist/umd/index.js",
"umd:main": "dist/umd/index.js",
"unpkg": "dist/umd/index.js",
"jsdelivr": "dist/umd/index.js",
"main": "dist/umd/index.production.js",
"umd:main": "dist/umd/index.production.js",
"unpkg": "dist/umd/index.production.js",
"jsdelivr": "dist/umd/index.production.js",
"sideEffects": false,
"files": [
"dist/"
Expand Down
15 changes: 2 additions & 13 deletions packages/autocomplete-plugin-query-suggestions/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { plugins } from '../../rollup.base.config';
import { getBundleBanner } from '../../scripts/getBundleBanner';
import { createRollupConfigs } from '../../scripts/rollup/config';

import pkg from './package.json';

export default {
input: 'src/index.ts',
output: {
file: 'dist/umd/index.js',
format: 'umd',
sourcemap: true,
name: pkg.name,
banner: getBundleBanner(pkg),
},
plugins,
};
export default createRollupConfigs({ pkg });
8 changes: 4 additions & 4 deletions packages/autocomplete-plugin-recent-searches/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"source": "src/index.ts",
"types": "dist/esm/index.d.ts",
"module": "dist/esm/index.js",
"main": "dist/umd/index.js",
"umd:main": "dist/umd/index.js",
"unpkg": "dist/umd/index.js",
"jsdelivr": "dist/umd/index.js",
"main": "dist/umd/index.production.js",
"umd:main": "dist/umd/index.production.js",
"unpkg": "dist/umd/index.production.js",
"jsdelivr": "dist/umd/index.production.js",
"sideEffects": false,
"files": [
"dist/"
Expand Down
15 changes: 2 additions & 13 deletions packages/autocomplete-plugin-recent-searches/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { plugins } from '../../rollup.base.config';
import { getBundleBanner } from '../../scripts/getBundleBanner';
import { createRollupConfigs } from '../../scripts/rollup/config';

import pkg from './package.json';

export default {
input: 'src/index.ts',
output: {
file: 'dist/umd/index.js',
format: 'umd',
sourcemap: true,
name: pkg.name,
banner: getBundleBanner(pkg),
},
plugins,
};
export default createRollupConfigs({ pkg });
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export function createRecentSearchesPlugin<TItem extends RecentSearchesItem>({
// because we need to resolve the promise before getting the value.
if (!Array.isArray(lastItemsRef.current)) {
warn(
false,
'The `getAlgoliaQuerySuggestionsFacetFilters` function is not supported with storages that return promises in `getAll`.'
);
return params;
Expand Down
8 changes: 4 additions & 4 deletions packages/autocomplete-preset-algolia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"source": "src/index.ts",
"types": "dist/esm/index.d.ts",
"module": "dist/esm/index.js",
"main": "dist/umd/index.js",
"umd:main": "dist/umd/index.js",
"unpkg": "dist/umd/index.js",
"jsdelivr": "dist/umd/index.js",
"main": "dist/umd/index.production.js",
"umd:main": "dist/umd/index.production.js",
"unpkg": "dist/umd/index.production.js",
"jsdelivr": "dist/umd/index.production.js",
"scripts": {
"build:clean": "rm -rf ./dist",
"build:esm": "babel src --root-mode upward --extensions '.ts,.tsx' --out-dir dist/esm --ignore '**/*/__tests__/'",
Expand Down
15 changes: 2 additions & 13 deletions packages/autocomplete-preset-algolia/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { plugins } from '../../rollup.base.config';
import { getBundleBanner } from '../../scripts/getBundleBanner';
import { createRollupConfigs } from '../../scripts/rollup/config';

import pkg from './package.json';

export default {
input: 'src/index.ts',
output: {
file: 'dist/umd/index.js',
format: 'umd',
sourcemap: true,
name: pkg.name,
banner: getBundleBanner(pkg),
},
plugins,
};
export default createRollupConfigs({ pkg });
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe('parseAlgoliaHitHighlight', () => {
},
},
});
}).toWarn(
}).toWarnDev(
'[Autocomplete] The attribute "_highlightResult.description.value" does not exist on the hit. Did you set it in `attributesToHighlight`?' +
'\nSee https://www.algolia.com/doc/api-reference/api-parameters/attributesToHighlight/'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe('parseAlgoliaHitReverseHighlight', () => {
},
},
});
}).toWarn(
}).toWarnDev(
'[Autocomplete] The attribute "_highlightResult.description.value" does not exist on the hit. Did you set it in `attributesToHighlight`?' +
'\nSee https://www.algolia.com/doc/api-reference/api-parameters/attributesToHighlight/'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('parseAlgoliaHitReverseSnippet', () => {
},
},
});
}).toWarn(
}).toWarnDev(
'[Autocomplete] The attribute "_snippetResult.description.value" does not exist on the hit. Did you set it in `attributesToSnippet`?' +
'\nSee https://www.algolia.com/doc/api-reference/api-parameters/attributesToSnippet/'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('parseAlgoliaHitSnippet', () => {
},
},
});
}).toWarn(
}).toWarnDev(
'[Autocomplete] The attribute "_snippetResult.description.value" does not exist on the hit. Did you set it in `attributesToSnippet`?' +
'\nSee https://www.algolia.com/doc/api-reference/api-parameters/attributesToSnippet/'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function parseAlgoliaHitHighlight<THit extends Hit<{}>>({

if (typeof highlightedValue !== 'string') {
warn(
false,
`The attribute ${JSON.stringify(
path
)} does not exist on the hit. Did you set it in \`attributesToHighlight\`?` +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function parseAlgoliaHitSnippet<THit extends Hit<{}>>({

if (typeof highlightedValue !== 'string') {
warn(
false,
`The attribute ${JSON.stringify(
path
)} does not exist on the hit. Did you set it in \`attributesToSnippet\`?` +
Expand Down
1 change: 1 addition & 0 deletions packages/autocomplete-shared/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare const __DEV__: boolean;
22 changes: 17 additions & 5 deletions packages/autocomplete-shared/src/__tests__/warn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@
import { warn } from '../warn';

describe('warn', () => {
test('logs when the condition is unmet', () => {
expect(() => {
warn(false, 'warning');
}).toWarnDev('[Autocomplete] warning');
});

test('does not log when the condition is unmet', () => {
expect(() => {
warn(true, 'warning');
}).not.toWarnDev();
});

test('trims the message', () => {
expect(() => {
warn('\nwarning! ');
}).toWarn('[Autocomplete] warning!');
warn(false, '\nwarning! ');
}).toWarnDev('[Autocomplete] warning!');
});

test('warns a message a single time', () => {
const originalConsoleWarn = console.warn;
console.warn = jest.fn();

warn('warning1');
warn('warning1');
warn('warning2');
warn(false, 'warning1');
warn(false, 'warning1');
warn(false, 'warning2');

expect(console.warn).toHaveBeenCalledTimes(2);
expect(console.warn).toHaveBeenNthCalledWith(1, '[Autocomplete] warning1');
Expand Down
14 changes: 13 additions & 1 deletion packages/autocomplete-shared/src/warn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@ export const warnCache = {
current: {},
};

export function warn(message: string) {
/**
* Logs a warning if the condition is not met.
* This is used to log issues in development environment only.
*/
export function warn(condition: boolean, message: string) {
if (!__DEV__) {
return;
}

if (condition) {
return;
}

const sanitizedMessage = message.trim();
const hasAlreadyPrinted = warnCache.current[sanitizedMessage];

Expand Down
2 changes: 0 additions & 2 deletions packages/website/docusaurus.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable import/no-commonjs */

module.exports = {
title: 'Autocomplete',
tagline: 'JavaScript library for building autocomplete search experiences.',
Expand Down
Loading