Skip to content

Commit

Permalink
🛠️ ESLint: add unicorn/consistent-destructuring rule (#2993)
Browse files Browse the repository at this point in the history
* add `unicorn/consistent-destructuring` rule
  • Loading branch information
dimaMachina authored Jan 22, 2023
1 parent e68cb8b commit bdc966c
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 122 deletions.
10 changes: 10 additions & 0 deletions .changeset/nasty-readers-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'codemirror-graphql': patch
'@graphiql/react': patch
'graphql-language-service': patch
'graphql-language-service-server': patch
'monaco-graphql': patch
'vscode-graphql-execution': patch
---

add `unicorn/consistent-destructuring` rule
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ module.exports = {
// Jest rules
'jest/no-conditional-expect': 0,

'unicorn/consistent-destructuring': 'error',
'prefer-destructuring': ['error', { VariableDeclarator: { object: true } }],
'promise/no-multiple-resolved': 'error',
'sonarjs/no-redundant-jump': 'error',
Expand Down
4 changes: 2 additions & 2 deletions packages/codemirror-graphql/src/hint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ CodeMirror.registerHelper(
editor: CodeMirror.Editor,
options: GraphQLHintOptions,
): IHints | undefined => {
const { schema } = options;
const { schema, externalFragments } = options;
if (!schema) {
return;
}
Expand All @@ -90,7 +90,7 @@ CodeMirror.registerHelper(
editor.getValue(),
position,
token,
options.externalFragments,
externalFragments,
);

const results = {
Expand Down
6 changes: 3 additions & 3 deletions packages/codemirror-graphql/src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ CodeMirror.registerHelper(
'lint',
'graphql',
(text: string, options: GraphQLLintOptions): CodeMirror.Annotation[] => {
const { schema } = options;
const { schema, validationRules, externalFragments } = options;
const rawResults = getDiagnostics(
text,
schema,
options.validationRules,
validationRules,
undefined,
options.externalFragments,
externalFragments,
);

const results = rawResults.map(error => ({
Expand Down
4 changes: 2 additions & 2 deletions packages/codemirror-graphql/src/results/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ function indent(
state: State,
textAfter: string,
) {
const { levels } = state;
const { levels, indentLevel } = state;
// If there is no stack of levels, use the current level.
// Otherwise, use the top level, preemptively dedenting for close braces.
const level =
!levels || levels.length === 0
? state.indentLevel
? indentLevel
: levels[levels.length - 1] -
(this.electricInput?.test(textAfter) ? 1 : 0);
return (level || 0) * (this.config?.indentUnit || 0);
Expand Down
4 changes: 2 additions & 2 deletions packages/codemirror-graphql/src/utils/jump-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ function onKeyDown(cm: CodeMirror.Editor, event: KeyboardEvent) {
};

const onClick = (clickEvent: MouseEvent) => {
const { destination } = cm.state.jump;
const { destination, options } = cm.state.jump;
if (destination) {
cm.state.jump.options.onClick(destination, clickEvent);
options.onClick(destination, clickEvent);
}
};

Expand Down
4 changes: 2 additions & 2 deletions packages/codemirror-graphql/src/utils/mode-indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export default function indent(
state: State,
textAfter: string,
) {
const { levels } = state;
const { levels, indentLevel } = state;
// If there is no stack of levels, use the current level.
// Otherwise, use the top level, preemptively dedenting for close braces.
const level =
!levels || levels.length === 0
? state.indentLevel
? indentLevel
: levels[levels.length - 1] -
(this.electricInput?.test(textAfter) ? 1 : 0);
return (level || 0) * (this.config?.indentUnit || 0);
Expand Down
4 changes: 2 additions & 2 deletions packages/codemirror-graphql/src/variables/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ function indent(
state: State,
textAfter: string,
) {
const { levels } = state;
const { levels, indentLevel } = state;
// If there is no stack of levels, use the current level.
// Otherwise, use the top level, preemptively dedenting for close braces.
const level =
!levels || levels.length === 0
? state.indentLevel
? indentLevel
: levels[levels.length - 1] -
(this.electricInput?.test(textAfter) ? 1 : 0);
return (level || 0) * (this.config?.indentUnit || 0);
Expand Down
13 changes: 4 additions & 9 deletions packages/graphiql-react/src/editor/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -364,17 +364,14 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
headerEditor,
responseEditor,
});
const { onTabChange } = props;
const { onTabChange, defaultHeaders, children } = props;

const addTab = useCallback<EditorContextType['addTab']>(() => {
setTabState(current => {
// Make sure the current tab stores the latest values
const updatedValues = synchronizeActiveTabValues(current);
const updated = {
tabs: [
...updatedValues.tabs,
createTab({ headers: props.defaultHeaders }),
],
tabs: [...updatedValues.tabs, createTab({ headers: defaultHeaders })],
activeTabIndex: updatedValues.tabs.length,
};
storeTabs(updated);
Expand All @@ -383,7 +380,7 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
return updated;
});
}, [
props.defaultHeaders,
defaultHeaders,
onTabChange,
setEditorValues,
storeTabs,
Expand Down Expand Up @@ -530,9 +527,7 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
);

return (
<EditorContext.Provider value={value}>
{props.children}
</EditorContext.Provider>
<EditorContext.Provider value={value}>{children}</EditorContext.Provider>
);
}

Expand Down
19 changes: 9 additions & 10 deletions packages/graphiql-react/src/execution.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function ExecutionContextProvider(props: ExecutionContextProviderProps) {
setSubscription(null);
}, [subscription]);

const { fetcher } = props;
const { fetcher, children, operationName } = props;
const run = useCallback<ExecutionContextType['run']>(async () => {
if (!queryEditor || !responseEditor) {
return;
Expand Down Expand Up @@ -169,14 +169,13 @@ export function ExecutionContextProvider(props: ExecutionContextProviderProps) {
setResponse('');
setIsFetching(true);

const operationName =
props.operationName ?? queryEditor.operationName ?? undefined;
const opName = operationName ?? queryEditor.operationName ?? undefined;

history?.addToHistory({
query,
variables: variablesString,
headers: headersString,
operationName,
operationName: opName,
});

try {
Expand Down Expand Up @@ -228,7 +227,7 @@ export function ExecutionContextProvider(props: ExecutionContextProviderProps) {
} else if (data) {
// If there is no path, we don't know what to do with the payload,
// so we just set it.
payload.data = part.data;
payload.data = data;
}

// Ensures we also bring extensions and alike along for the ride
Expand All @@ -251,7 +250,7 @@ export function ExecutionContextProvider(props: ExecutionContextProviderProps) {
{
query,
variables,
operationName,
operationName: opName,
},
{
headers: headers ?? undefined,
Expand Down Expand Up @@ -312,7 +311,7 @@ export function ExecutionContextProvider(props: ExecutionContextProviderProps) {
fetcher,
headerEditor,
history,
props.operationName,
operationName,
queryEditor,
responseEditor,
stop,
Expand All @@ -326,16 +325,16 @@ export function ExecutionContextProvider(props: ExecutionContextProviderProps) {
() => ({
isFetching,
isSubscribed,
operationName: props.operationName ?? null,
operationName: operationName ?? null,
run,
stop,
}),
[isFetching, isSubscribed, props.operationName, run, stop],
[isFetching, isSubscribed, operationName, run, stop],
);

return (
<ExecutionContext.Provider value={value}>
{props.children}
{children}
</ExecutionContext.Provider>
);
}
Expand Down
6 changes: 2 additions & 4 deletions packages/graphiql-react/src/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export function PluginContextProvider(props: PluginContextProviderProps) {
);
});

const { onTogglePluginVisibility } = props;
const { onTogglePluginVisibility, children } = props;
const setVisiblePlugin = useCallback<PluginContextType['setVisiblePlugin']>(
plugin => {
const newVisiblePlugin = plugin
Expand Down Expand Up @@ -187,9 +187,7 @@ export function PluginContextProvider(props: PluginContextProviderProps) {
);

return (
<PluginContext.Provider value={value}>
{props.children}
</PluginContext.Provider>
<PluginContext.Provider value={value}>{children}</PluginContext.Provider>
);
}

Expand Down
11 changes: 5 additions & 6 deletions packages/graphiql-react/src/schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ export function SchemaContextProvider(props: SchemaContextProviderProps) {
/**
* Fetch the schema
*/
const { fetcher, onSchemaChange } = props;
const { fetcher, onSchemaChange, dangerouslyAssumeSchemaIsValid, children } =
props;
const introspect = useCallback(() => {
/**
* Only introspect if there is no schema provided via props. If the
Expand Down Expand Up @@ -330,11 +331,11 @@ export function SchemaContextProvider(props: SchemaContextProviderProps) {
* Derive validation errors from the schema
*/
const validationErrors = useMemo(() => {
if (!schema || props.dangerouslyAssumeSchemaIsValid) {
if (!schema || dangerouslyAssumeSchemaIsValid) {
return [];
}
return validateSchema(schema);
}, [schema, props.dangerouslyAssumeSchemaIsValid]);
}, [schema, dangerouslyAssumeSchemaIsValid]);

/**
* Memoize context value
Expand All @@ -351,9 +352,7 @@ export function SchemaContextProvider(props: SchemaContextProviderProps) {
);

return (
<SchemaContext.Provider value={value}>
{props.children}
</SchemaContext.Provider>
<SchemaContext.Provider value={value}>{children}</SchemaContext.Provider>
);
}

Expand Down
Loading

0 comments on commit bdc966c

Please sign in to comment.