Skip to content

Commit

Permalink
[ESLint] replace .forEach with for..of (#3113)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaMachina committed Apr 14, 2023
1 parent a8f21ad commit 2e477eb
Show file tree
Hide file tree
Showing 26 changed files with 148 additions and 123 deletions.
12 changes: 12 additions & 0 deletions .changeset/breezy-laws-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'codemirror-graphql': patch
'graphiql': patch
'@graphiql/react': patch
'@graphiql/toolkit': patch
'graphql-language-service': patch
'graphql-language-service-server': patch
'monaco-graphql': patch
'vscode-graphql-execution': patch
---

replace `.forEach` with `for..of`
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ module.exports = {
'unicorn/prefer-export-from': ['error', { ignoreUsedVariables: true }],
'unicorn/throw-new-error': 'error',
'unicorn/prefer-includes': 'error',
'unicorn/no-array-for-each': 'error',
'no-lonely-if': 'error',
'unicorn/no-lonely-if': 'error',
'unicorn/prefer-optional-catch-binding': 'error',
Expand Down
4 changes: 2 additions & 2 deletions examples/monaco-graphql-webpack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,15 @@ function getSchemaPicker(): HTMLSelectElement {
const schemaPicker = document.createElement('select');
schemaPicker.id = 'schema-picker';

schemaOptions.forEach(option => {
for (const option of schemaOptions) {
const optEl = document.createElement('option');
optEl.value = option.value;
optEl.label = option.label;
if (option.default) {
optEl.selected = true;
}
schemaPicker.appendChild(optEl);
});
}

return schemaPicker;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/codemirror-graphql/src/utils/collectVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ export default function collectVariables(
documentAST: DocumentNode,
) {
const variableToType = Object.create(null);
documentAST.definitions.forEach(definition => {
for (const definition of documentAST.definitions) {
if (definition.kind === 'OperationDefinition') {
const { variableDefinitions } = definition;
if (variableDefinitions) {
variableDefinitions.forEach(({ variable, type }) => {
for (const { variable, type } of variableDefinitions) {
const inputType = typeFromAST(schema, type as NamedTypeNode);
if (inputType) {
variableToType[variable.name.value] = inputType;
}
});
}
}
}
});
}
return variableToType;
}
4 changes: 2 additions & 2 deletions packages/codemirror-graphql/src/utils/runParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export default function runParser(
const state = parser.startState();
const lines = sourceText.split('\n');

lines.forEach(line => {
for (const line of lines) {
const stream = new CharacterStream(line);
while (!stream.eol()) {
const style = parser.token(stream, state);
callbackFn(stream, state, style);
}
});
}
}
12 changes: 6 additions & 6 deletions packages/codemirror-graphql/src/variables/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ function validateVariables(
) {
const errors: CodeMirror.Annotation[] = [];

variablesAST.members.forEach(member => {
for (const member of variablesAST.members) {
if (member) {
const variableName = member.key?.value;
const type = variableToType[variableName];
if (type) {
validateValue(type, member.value).forEach(([node, message]) => {
for (const [node, message] of validateValue(type, member.value)) {
errors.push(lintError(editor, node, message));
});
}
} else {
errors.push(
lintError(
Expand All @@ -103,7 +103,7 @@ function validateVariables(
);
}
}
});
}

return errors;
}
Expand Down Expand Up @@ -169,7 +169,7 @@ function validateValue(
);

// Look for missing non-nullable fields.
Object.keys(type.getFields()).forEach(fieldName => {
for (const fieldName of Object.keys(type.getFields())) {
const field = type.getFields()[fieldName];
if (
!providedFields[fieldName] &&
Expand All @@ -181,7 +181,7 @@ function validateValue(
`Object of type "${type}" is missing required field "${fieldName}".`,
]);
}
});
}

return fieldErrors;
}
Expand Down
10 changes: 7 additions & 3 deletions packages/graphiql-react/src/editor/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,17 @@ export function useAutoCompleteLeafs({
},
),
);
setTimeout(() => markers.forEach(marker => marker.clear()), 7000);
setTimeout(() => {
for (const marker of markers) {
marker.clear();
}
}, 7000);
let newCursorIndex = cursorIndex;
insertions.forEach(({ index, string }) => {
for (const { index, string } of insertions) {
if (index < cursorIndex) {
newCursorIndex += string.length;
}
});
}
queryEditor.setCursor(queryEditor.posFromIndex(newCursorIndex));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ function defaultGetDefaultFieldNames(type: GraphQLType) {

// Include all leaf-type fields.
const leafFieldNames: Array<string> = [];
Object.keys(fields).forEach(fieldName => {
for (const fieldName of Object.keys(fields)) {
if (isLeafType(fields[fieldName].type)) {
leafFieldNames.push(fieldName);
}
});
}
return leafFieldNames;
}

Expand Down Expand Up @@ -174,10 +174,10 @@ function withInsertions(initial: string, insertions: Insertion[]) {
}
let edited = '';
let prevIndex = 0;
insertions.forEach(({ index, string }) => {
for (const { index, string } of insertions) {
edited += initial.slice(prevIndex, index) + string;
prevIndex = index;
});
}
edited += initial.slice(prevIndex);
return edited;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/graphiql/cypress/e2e/init.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ describe('GraphiQL On Initialization', () => {
];
cy.visit(`/`);
cy.get('.graphiql-query-editor').contains('# Welcome to GraphiQL');
containers.forEach(cSelector => cy.get(cSelector).should('be.visible'));
for (const cSelector of containers) {
cy.get(cSelector).should('be.visible');
}
});

it('Executes a GraphQL query over HTTP that has the expected result', () => {
Expand Down
19 changes: 8 additions & 11 deletions packages/graphiql/resources/renderExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@

// Parse the search string to get url parameters.
var parameters = {};
window.location.search
.slice(1)
.split('&')
.forEach(function (entry) {
var eq = entry.indexOf('=');
if (eq >= 0) {
parameters[decodeURIComponent(entry.slice(0, eq))] = decodeURIComponent(
entry.slice(eq + 1),
);
}
});
for (const entry of window.location.search.slice(1).split('&')) {
var eq = entry.indexOf('=');
if (eq >= 0) {
parameters[decodeURIComponent(entry.slice(0, eq))] = decodeURIComponent(
entry.slice(eq + 1),
);
}
}

// When the query and variables string is edited, update the URL bar so
// that it can be easily shared.
Expand Down
Loading

0 comments on commit 2e477eb

Please sign in to comment.