Skip to content

Commit

Permalink
enable prefer-destructuring rule (#2963)
Browse files Browse the repository at this point in the history
* enable `prefer-destructuring` rule

* fixes
  • Loading branch information
dimaMachina authored Jan 12, 2023
1 parent 0434d94 commit f263f77
Show file tree
Hide file tree
Showing 29 changed files with 71 additions and 73 deletions.
10 changes: 10 additions & 0 deletions .changeset/strange-cheetahs-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'codemirror-graphql': patch
'graphiql': patch
'@graphiql/react': patch
'graphql-language-service': patch
'graphql-language-service-server': patch
'vscode-graphql-execution': patch
---

enable `prefer-destructuring` rule
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ module.exports = {
'no-continue': 0,
'no-inline-comments': 0,
'no-mixed-operators': 0,
'no-negated-condition': 'error',
'no-negated-condition': 'off',
'unicorn/no-negated-condition': 'error',
'no-nested-ternary': 0,
'no-new-object': 1,
'no-plusplus': 0,
Expand Down Expand Up @@ -260,6 +261,7 @@ module.exports = {
// Jest rules
'jest/no-conditional-expect': 0,

'prefer-destructuring': ['error', { VariableDeclarator: { object: true } }],
'promise/no-multiple-resolved': 'error',
'sonarjs/no-redundant-jump': 'error',
'unicorn/prefer-logical-operator-over-ternary': 'error',
Expand Down
2 changes: 1 addition & 1 deletion 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.schema;
const { schema } = options;
if (!schema) {
return;
}
Expand Down
9 changes: 3 additions & 6 deletions packages/codemirror-graphql/src/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ CodeMirror.registerHelper(
if (!options.schema || !token.state) {
return;
}

const state = token.state;
const kind = state.kind;
const step = state.step;
const { kind, step } = token.state;
const typeInfo = getTypeInfo(options.schema, token.state);

// Given a Schema and a Token, produce the contents of an info tooltip.
Expand Down Expand Up @@ -238,7 +235,7 @@ function renderDescription(
| GraphQLEnumValue
| GraphQLType,
) {
const description = (def as GraphQLInputField).description;
const { description } = def as GraphQLInputField;
if (description) {
const descriptionDiv = document.createElement('div');
descriptionDiv.className = 'info-description';
Expand Down Expand Up @@ -293,7 +290,7 @@ function text(
ref: Maybe<SchemaReference> = null,
) {
if (className) {
const onClick = options.onClick;
const { onClick } = options;
let node;
if (onClick) {
node = document.createElement('a');
Expand Down
5 changes: 2 additions & 3 deletions packages/codemirror-graphql/src/jump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ CodeMirror.registerHelper(
// Given a Schema and a Token, produce a "SchemaReference" which refers to
// the particular artifact from the schema (such as a type, field, argument,
// or directive) that token references.
const state = token.state;
const kind = state.kind;
const step = state.step;
const { state } = token;
const { kind, step } = state;
const typeInfo = getTypeInfo(options.schema, state);

if (
Expand Down
2 changes: 1 addition & 1 deletion packages/codemirror-graphql/src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ CodeMirror.registerHelper(
'lint',
'graphql',
(text: string, options: GraphQLLintOptions): CodeMirror.Annotation[] => {
const schema = options.schema;
const { schema } = options;
const rawResults = getDiagnostics(
text,
schema,
Expand Down
2 changes: 1 addition & 1 deletion packages/codemirror-graphql/src/results/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function indent(
state: State,
textAfter: string,
) {
const levels = state.levels;
const { levels } = state;
// If there is no stack of levels, use the current level.
// Otherwise, use the top level, preemptively dedenting for close braces.
const level =
Expand Down
2 changes: 1 addition & 1 deletion packages/codemirror-graphql/src/utils/collectVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function collectVariables(
const variableToType = Object.create(null);
documentAST.definitions.forEach(definition => {
if (definition.kind === 'OperationDefinition') {
const variableDefinitions = definition.variableDefinitions;
const { variableDefinitions } = definition;
if (variableDefinitions) {
variableDefinitions.forEach(({ variable, type }) => {
const inputType = typeFromAST(schema, type as NamedTypeNode);
Expand Down
4 changes: 2 additions & 2 deletions packages/codemirror-graphql/src/utils/info-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function createState(options: GraphQLInfoOptions) {
}

function getHoverTime(cm: CodeMirror.Editor) {
const options = cm.state.info.options;
const { options } = cm.state.info;
return options?.hoverTime || 500;
}

Expand Down Expand Up @@ -96,7 +96,7 @@ function onMouseHover(cm: CodeMirror.Editor, box: DOMRect) {
});

const state = cm.state.info;
const options = state.options;
const { options } = state;
const render = options.render || cm.getHelper(pos, 'info');
if (render) {
const token = cm.getTokenAt(pos, true);
Expand Down
8 changes: 3 additions & 5 deletions packages/codemirror-graphql/src/utils/jump-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function onKeyDown(cm: CodeMirror.Editor, event: KeyboardEvent) {
};

const onClick = (clickEvent: MouseEvent) => {
const destination = cm.state.jump.destination;
const { destination } = cm.state.jump;
if (destination) {
cm.state.jump.options.onClick(destination, clickEvent);
}
Expand Down Expand Up @@ -134,11 +134,9 @@ function enableJumpMode(cm: CodeMirror.Editor) {
return;
}

const cursor = cm.state.jump.cursor;
const { cursor, options } = cm.state.jump;
const pos = cm.coordsChar(cursor);
const token = cm.getTokenAt(pos, true);

const options = cm.state.jump.options;
const getDestination = options.getDestination || cm.getHelper(pos, 'jump');
if (getDestination) {
const destination = getDestination(token, options, cm);
Expand All @@ -156,7 +154,7 @@ function enableJumpMode(cm: CodeMirror.Editor) {
}

function disableJumpMode(cm: CodeMirror.Editor) {
const marker = cm.state.jump.marker;
const { marker } = cm.state.jump;
cm.state.jump.marker = null;
cm.state.jump.destination = null;

Expand Down
2 changes: 1 addition & 1 deletion packages/codemirror-graphql/src/utils/mode-indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function indent(
state: State,
textAfter: string,
) {
const levels = state.levels;
const { levels } = state;
// If there is no stack of levels, use the current level.
// Otherwise, use the top level, preemptively dedenting for close braces.
const level =
Expand Down
6 changes: 2 additions & 4 deletions packages/codemirror-graphql/src/variables/hint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,13 @@ function getVariablesHint(
const state =
token.state.kind === 'Invalid' ? token.state.prevState : token.state;

const kind = state.kind;
const step = state.step;

const { kind, step } = state;
// Variables can only be an object literal.
if (kind === 'Document' && step === 0) {
return hintList(cur, token, [{ text: '{' }]);
}

const variableToType = options.variableToType;
const { variableToType } = options;
if (!variableToType) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/codemirror-graphql/src/variables/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ CodeMirror.registerHelper(
}

// If there are not yet known variables, do nothing.
const variableToType = options.variableToType;
const { variableToType } = options;
if (!variableToType) {
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/codemirror-graphql/src/variables/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function indent(
state: State,
textAfter: string,
) {
const levels = state.levels;
const { levels } = state;
// If there is no stack of levels, use the current level.
// Otherwise, use the top level, preemptively dedenting for close braces.
const level =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function tokenToURL(token: Token) {
const value = token.string.slice(1).slice(0, -1).trim();

try {
const location = window.location;
const { location } = window;
return new URL(value, location.protocol + '//' + location.host);
} catch {
return;
Expand Down
2 changes: 1 addition & 1 deletion packages/graphiql/src/components/GraphiQL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ export function GraphiQLInterface(props: GraphiQLInterfaceProps) {
type="button"
id="disable-persist-headers"
className={
!editorContext.shouldPersistHeaders ? 'active' : undefined
editorContext.shouldPersistHeaders ? undefined : 'active'
}
onClick={() => {
editorContext.setShouldPersistHeaders(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ describe('GraphiQL', () => {
const callback = async () => {
try {
await findByText('Persist headers');
} catch (e) {
} catch {
// eslint-disable-next-line no-throw-literal
throw 'failed';
}
Expand Down
2 changes: 1 addition & 1 deletion packages/graphiql/test/e2e-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ app.post('/graphql-error/graphql', (_req, res, next) => {
app.use(express.static(path.resolve(__dirname, '../')));

app.listen(process.env.PORT || 0, function () {
const port = this.address().port;
const { port } = this.address();

console.log(`Started on http://localhost:${port}/`);
console.log('PID', process.pid);
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-language-service-server/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class Logger implements VSCodeLogger {
_log(message: string, severityKey: SeverityEnum): void {
const timestamp = new Date().toLocaleString(undefined);
const severity = DIAGNOSTIC_SEVERITY[severityKey];
const pid = process.pid;
const { pid } = process;

const stringMessage = String(message).trim();
const logMessage = `${timestamp} [${severity}] (pid: ${pid}) graphql-language-service-usage-logs: ${stringMessage}\n`;
Expand Down
25 changes: 11 additions & 14 deletions packages/graphql-language-service-server/src/MessageProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,14 @@ export class MessageProcessor {
'`textDocument`, `textDocument.uri`, and `contentChanges` arguments are required.',
);
}
const textDocument = params.textDocument;
const uri = textDocument.uri;
const { textDocument } = params;
const { uri } = textDocument;
const project = this._graphQLCache.getProjectForFile(uri);
try {
const contentChanges = params.contentChanges;
const { contentChanges } = params;
const contentChange = contentChanges[contentChanges.length - 1];

// As `contentChanges` is an array and we just want the
// As `contentChanges` is an array, and we just want the
// latest update to the text, grab the last entry from the array.

// If it's a .js file, try parsing the contents to see if GraphQL queries
Expand Down Expand Up @@ -512,8 +512,8 @@ export class MessageProcessor {
if (!params || !params.textDocument) {
throw new Error('`textDocument` is required.');
}
const textDocument = params.textDocument;
const uri = textDocument.uri;
const { textDocument } = params;
const { uri } = textDocument;

if (this._textDocumentCache.has(uri)) {
this._textDocumentCache.delete(uri);
Expand Down Expand Up @@ -560,8 +560,7 @@ export class MessageProcessor {

this.validateDocumentAndPosition(params);

const textDocument = params.textDocument;
const position = params.position;
const { textDocument, position } = params;

// `textDocument/completion` event takes advantage of the fact that
// `textDocument/didChange` event always fires before, which would have
Expand Down Expand Up @@ -617,8 +616,7 @@ export class MessageProcessor {

this.validateDocumentAndPosition(params);

const textDocument = params.textDocument;
const position = params.position;
const { textDocument, position } = params;

const cachedDocument = this._getCachedDocument(textDocument.uri);
if (!cachedDocument) {
Expand Down Expand Up @@ -679,7 +677,7 @@ export class MessageProcessor {
change.type === FileChangeTypeKind.Created ||
change.type === FileChangeTypeKind.Changed
) {
const uri = change.uri;
const { uri } = change;

const text = readFileSync(URI.parse(uri).fsPath, 'utf-8');
const contents = this._parser(text, uri);
Expand Down Expand Up @@ -747,8 +745,7 @@ export class MessageProcessor {
if (!params || !params.textDocument || !params.position) {
throw new Error('`textDocument` and `position` arguments are required.');
}
const textDocument = params.textDocument;
const position = params.position;
const { textDocument, position } = params;
const project = this._graphQLCache.getProjectForFile(textDocument.uri);
if (project) {
await this._cacheSchemaFilesForProject(project);
Expand Down Expand Up @@ -846,7 +843,7 @@ export class MessageProcessor {
throw new Error('`textDocument` argument is required.');
}

const textDocument = params.textDocument;
const { textDocument } = params;
const cachedDocument = this._getCachedDocument(textDocument.uri);
if (!cachedDocument || !cachedDocument.contents[0]) {
return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export function findGraphQLTags(
const asts = parsedASTs;

const parseTemplateLiteral = (node: TemplateLiteral) => {
const loc = node.quasis[0].loc;
const { loc } = node.quasis[0];
if (loc) {
if (node.quasis.length > 1) {
const last = node.quasis.pop();
Expand All @@ -183,7 +183,7 @@ export function findGraphQLTags(
const visitors = {
CallExpression: (node: Expression) => {
if ('callee' in node) {
const callee = node.callee;
const { callee } = node;

if (
callee.type === 'Identifier' &&
Expand All @@ -203,7 +203,7 @@ export function findGraphQLTags(
TaggedTemplateExpression: (node: TaggedTemplateExpression) => {
const tagName = getGraphQLTagName(node.tag);
if (tagName) {
const loc = node.quasi.quasis[0].loc;
const { loc } = node.quasi.quasis[0];
const template =
node.quasi.quasis.length > 1
? node.quasi.quasis.map(quasi => quasi.value.raw).join('')
Expand Down
3 changes: 1 addition & 2 deletions packages/graphql-language-service-server/src/startServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ export default async function startServer(
process.exit(1);
}

const port = options.port;
const hostname = options.hostname;
const { port, hostname } = options;
const socket = net
.createServer(client => {
client.setEncoding('utf8');
Expand Down
Loading

0 comments on commit f263f77

Please sign in to comment.