Skip to content

Commit

Permalink
ci: fail linting for forEach loops (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Jun 4, 2021
1 parent 09ed9b8 commit 455eae8
Show file tree
Hide file tree
Showing 14 changed files with 16,634 additions and 209 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
docs
lib
lib
output
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"plugins": [
"@typescript-eslint",
"sonarjs",
"security"
"security",
"github"
],
"extends": [
"eslint:recommended",
Expand All @@ -14,6 +15,7 @@
],
"rules": {
"strict": 0,
"github/array-foreach": 2,
"eol-last": ["error", "always"],
"@typescript-eslint/no-explicit-any": 0,
"require-await": "error",
Expand Down
16,681 changes: 16,551 additions & 130 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"conventional-changelog-conventionalcommits": "^4.5.0",
"cross-env": "^7.0.2",
"eslint": "^7.19.0",
"eslint-plugin-github": "^4.1.3",
"eslint-plugin-security": "^1.4.0",
"eslint-plugin-sonarjs": "^0.5.0",
"jest": "^26.6.3",
Expand Down
4 changes: 2 additions & 2 deletions src/generators/AbstractGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export abstract class AbstractGenerator<Options extends CommonGeneratorOptions =
}

const presets = this.options.presets || [];
presets.forEach(p => {
for (const p of presets) {
if (isPresetWithOptions(p)) {
const preset = p.preset[String(presetType)];
if (preset) {
Expand All @@ -77,7 +77,7 @@ export abstract class AbstractGenerator<Options extends CommonGeneratorOptions =
filteredPresets.push([preset, undefined]);
}
}
});
}

return filteredPresets;
}
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/FormatHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class FormatHelpers {
static renderJSONExamples(examples: any[]): string {
let renderedExamples = '';
if (Array.isArray(examples)) {
examples.forEach(example => {
for (const example of examples) {
if (renderedExamples !== '') {renderedExamples += ', ';}
if (typeof example === 'object') {
try {
Expand All @@ -114,7 +114,7 @@ export class FormatHelpers {
} else {
renderedExamples += example;
}
});
}
}
return renderedExamples;
}
Expand Down
7 changes: 3 additions & 4 deletions src/interpreter/Interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,9 @@ export class Interpreter {
* @param options to control the interpret process
*/
interpretAndCombineMultipleSchemas(schema: (Schema | boolean)[] | undefined, currentModel: CommonModel, rootSchema: Schema, interpreterOptions: InterpreterOptions = Interpreter.defaultInterpreterOptions): void {
if (Array.isArray(schema)) {
schema.forEach((forEachSchema) => {
this.interpretAndCombineSchema(forEachSchema, currentModel, rootSchema, interpreterOptions);
});
if (!Array.isArray(schema)) { return; }
for (const forEachSchema of schema) {
this.interpretAndCombineSchema(forEachSchema, currentModel, rootSchema, interpreterOptions);
}
}

Expand Down
29 changes: 15 additions & 14 deletions src/models/CommonModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ export class CommonModel extends CommonSchema<CommonModel> {
*/
addTypes(types: string[] | string): void {
if (Array.isArray(types)) {
types.forEach((value) => {
this.addTypes(value);
});
for (const type of types) {
this.addTypes(type);
}
} else if (this.type === undefined) {
this.type = types;
} else if (!Array.isArray(this.type) && this.type !== types) {
Expand Down Expand Up @@ -145,9 +145,9 @@ export class CommonModel extends CommonSchema<CommonModel> {
removeEnum(enumsToRemove: any | any[]): void {
if (this.enum === undefined || enumsToRemove === undefined) {return;}
if (Array.isArray(enumsToRemove)) {
enumsToRemove.forEach((enumToRemove) => {
for (const enumToRemove of enumsToRemove) {
this.removeEnum(enumToRemove);
});
}
return;
}
const filteredEnums = this.enum.filter((el) => {
Expand Down Expand Up @@ -248,12 +248,12 @@ export class CommonModel extends CommonSchema<CommonModel> {
}
if (this.items !== undefined) {
const items = Array.isArray(this.items) ? this.items : [this.items];
items.forEach((item) => {
for (const item of items) {
const itemRef = item.$ref;
if (itemRef !== undefined) {
dependsOn.push(itemRef);
}
});
}
}
if (this.properties !== undefined && Object.keys(this.properties).length) {
const referencedProperties = Object.values(this.properties)
Expand Down Expand Up @@ -366,14 +366,15 @@ export class CommonModel extends CommonSchema<CommonModel> {
* @param originalSchema
* @param alreadyIteratedModels
*/
private static mergeItems(mergeTo: CommonModel, mergeFrom: CommonModel, originalSchema: Schema, alreadyIteratedModels: Map<CommonModel, CommonModel> = new Map()) {
// eslint-disable-next-line sonarjs/cognitive-complexity
private static mergeItems(mergeTo: CommonModel, mergeFrom: CommonModel, originalSchema: Schema, alreadyIteratedModels: Map<CommonModel, CommonModel> = new Map()) { // NOSONAR
const merge = (models: CommonModel | CommonModel[] | undefined): CommonModel | undefined => {
if (!Array.isArray(models)) {return models;}
let mergedItemsModel: CommonModel | undefined = undefined;
models.forEach((model, index) => {
for (const [index, model] of models.entries()) {
Logger.warn(`Found duplicate items at index ${index} for model. Model item for ${mergeFrom.$id || 'unknown'} merged into ${mergeTo.$id || 'unknown'}`, mergeTo, mergeFrom, originalSchema);
mergedItemsModel = CommonModel.mergeCommonModels(mergedItemsModel, model, originalSchema, alreadyIteratedModels);
});
}
return mergedItemsModel;
};
if (mergeFrom.items !== undefined) {
Expand Down Expand Up @@ -413,11 +414,11 @@ export class CommonModel extends CommonSchema<CommonModel> {
if (mergeFrom.type !== undefined) {
if (mergeTo.type === undefined) {
mergeTo.type = mergeFrom.type;
} else {
if (Array.isArray(mergeFrom.type)) {
mergeFrom.type.forEach(addToType);
return;
} else if (Array.isArray(mergeFrom.type)) {
for (const type of mergeFrom.type) {
addToType(type);
}
} else {
addToType(mergeFrom.type);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/models/CommonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export class CommonSchema<T> {
}
if (schema.properties !== undefined) {
const properties : {[key: string]: T | boolean} = {};
Object.entries(schema.properties).forEach(([propertyName, propertySchema]) => {
for (const [propertyName, propertySchema] of Object.entries(schema.properties)) {
properties[String(propertyName)] = transformationSchemaCallback(propertySchema, seenSchemas);
});
}
schema.properties = properties;
}
if (typeof schema.additionalProperties === 'object' &&
Expand All @@ -43,9 +43,9 @@ export class CommonSchema<T> {
if (typeof schema.patternProperties === 'object' &&
schema.patternProperties !== undefined) {
const patternProperties : {[key: string]: T | boolean} = {};
Object.entries(schema.patternProperties).forEach(([pattern, patternSchema]) => {
for (const [pattern, patternSchema] of Object.entries(schema.patternProperties)) {
patternProperties[String(pattern)] = transformationSchemaCallback(patternSchema, seenSchemas);
});
}
schema.patternProperties = patternProperties;
}
return schema;
Expand Down
8 changes: 4 additions & 4 deletions src/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ export class Schema extends CommonSchema<Schema | boolean> {
}
if (schema.dependencies !== undefined) {
const dependencies: { [key: string]: Schema | boolean | string[] } = {};
Object.entries(schema.dependencies).forEach(([propertyName, property]) => {
for (const [propertyName, property] of Object.entries(schema.dependencies)) {
//We only care about object dependencies
if (typeof property === 'object' && !Array.isArray(property)) {
dependencies[String(propertyName)] = Schema.toSchema(property, seenSchemas);
} else {
dependencies[String(propertyName)] = property as string[];
}
});
}
schema.dependencies = dependencies;
}

Expand All @@ -113,9 +113,9 @@ export class Schema extends CommonSchema<Schema | boolean> {

if (schema.definitions !== undefined) {
const definitions: { [key: string]: Schema | boolean } = {};
Object.entries(schema.definitions).forEach(([propertyName, property]) => {
for (const [propertyName, property] of Object.entries(schema.definitions)) {
definitions[String(propertyName)] = Schema.toSchema(property, seenSchemas);
});
}
schema.definitions = definitions;
}
return schema;
Expand Down
20 changes: 10 additions & 10 deletions src/processors/AsyncAPIInputProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
}
common.originalInput = doc;

doc.allMessages().forEach((message) => {
for (const [, message] of doc.allMessages()) {
const schema = AsyncAPIInputProcessor.convertToInternalSchema(message.payload());
const commonModels = JsonSchemaInputProcessor.convertSchemaToCommonModel(schema);
common.models = {...common.models, ...commonModels};
});
}
return common;
}

Expand Down Expand Up @@ -106,34 +106,34 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {

if (schema.properties() !== null && Object.keys(schema.properties()).length) {
const properties : {[key: string]: Schema | boolean} = {};
Object.entries(schema.properties()).forEach(([propertyName, propertySchema]) => {
for (const [propertyName, propertySchema] of Object.entries(schema.properties())) {
properties[String(propertyName)] = this.convertToInternalSchema(propertySchema, alreadyIteratedSchemas);
});
}
convertedSchema.properties = properties;
}
if (schema.dependencies() !== null && Object.keys(schema.dependencies()).length) {
const dependencies: { [key: string]: Schema | boolean | string[] } = {};
Object.entries(schema.dependencies()).forEach(([dependencyName, dependency]) => {
for (const [dependencyName, dependency] of Object.entries(schema.dependencies())) {
if (typeof dependency === 'object' && !Array.isArray(dependency)) {
dependencies[String(dependencyName)] = this.convertToInternalSchema(dependency, alreadyIteratedSchemas);
} else {
dependencies[String(dependencyName)] = dependency as string[];
}
});
}
convertedSchema.dependencies = dependencies;
}
if (schema.patternProperties() !== null && Object.keys(schema.patternProperties()).length) {
const patternProperties: { [key: string]: Schema | boolean } = {};
Object.entries(schema.patternProperties()).forEach(([patternPropertyName, patternProperty]) => {
for (const [patternPropertyName, patternProperty] of Object.entries(schema.patternProperties())) {
patternProperties[String(patternPropertyName)] = this.convertToInternalSchema(patternProperty, alreadyIteratedSchemas);
});
}
convertedSchema.patternProperties = patternProperties;
}
if (schema.definitions() !== null && Object.keys(schema.definitions()).length) {
const definitions: { [key: string]: Schema | boolean } = {};
Object.entries(schema.definitions()).forEach(([definitionName, definition]) => {
for (const [definitionName, definition] of Object.entries(schema.definitions())) {
definitions[String(definitionName)] = this.convertToInternalSchema(definition, alreadyIteratedSchemas);
});
}
convertedSchema.definitions = definitions;
}

Expand Down
30 changes: 15 additions & 15 deletions src/processors/JsonSchemaInputProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,34 +158,34 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {

if (schema.properties !== undefined) {
const properties : {[key: string]: Schema | boolean} = {};
Object.entries(schema.properties).forEach(([propertyName, propertySchema]) => {
for (const [propertyName, propertySchema] of Object.entries(schema.properties)) {
properties[String(propertyName)] = this.reflectSchemaNames(propertySchema, namesStack, this.ensureNamePattern(name, propertyName));
});
}
schema.properties = properties;
}
if (schema.dependencies !== undefined) {
const dependencies: { [key: string]: Schema | boolean | string[] } = {};
Object.entries(schema.dependencies).forEach(([dependencyName, dependency]) => {
for (const [dependencyName, dependency] of Object.entries(schema.dependencies)) {
if (typeof dependency === 'object' && !Array.isArray(dependency)) {
dependencies[String(dependencyName)] = this.reflectSchemaNames(dependency, namesStack, this.ensureNamePattern(name, dependencyName));
} else {
dependencies[String(dependencyName)] = dependency as string[];
}
});
}
schema.dependencies = dependencies;
}
if (schema.patternProperties !== undefined) {
const patternProperties: { [key: string]: Schema | boolean } = {};
Object.entries(schema.patternProperties).forEach(([patternPropertyName, patternProperty], idx) => {
for (const [idx, [patternPropertyName, patternProperty]] of Object.entries(Object.entries(schema.patternProperties))) {
patternProperties[String(patternPropertyName)] = this.reflectSchemaNames(patternProperty, namesStack, this.ensureNamePattern(name, 'pattern_property', idx));
});
}
schema.patternProperties = patternProperties;
}
if (schema.definitions !== undefined) {
const definitions: { [key: string]: Schema | boolean } = {};
Object.entries(schema.definitions).forEach(([definitionName, definition]) => {
for (const [definitionName, definition] of Object.entries(schema.definitions)) {
definitions[String(definitionName)] = this.reflectSchemaNames(definition, namesStack, this.ensureNamePattern(name, definitionName));
});
}
schema.definitions = definitions;
}

Expand Down Expand Up @@ -215,16 +215,16 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {
const interpreter = new Interpreter();
const commonModels = interpreter.interpret(schema);
const commonModelsMap: Record<string, CommonModel> = {};
commonModels.forEach(value => {
if (value.$id) {
if (commonModelsMap[value.$id] !== undefined) {
Logger.warn(`Overwriting existing model with $id ${value.$id}, are there two models with the same id present?`, value);
for (const commonModel of commonModels) {
if (commonModel.$id) {
if (commonModelsMap[commonModel.$id] !== undefined) {
Logger.warn(`Overwriting existing model with $id ${commonModel.$id}, are there two models with the same id present?`, commonModel);
}
commonModelsMap[value.$id] = value;
commonModelsMap[commonModel.$id] = commonModel;
} else {
Logger.warn('Model did not have $id, ignoring.', value);
Logger.warn('Model did not have $id, ignoring.', commonModel);
}
});
}
return commonModelsMap;
}
}
8 changes: 4 additions & 4 deletions test/models/CommonModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ describe('CommonModel', () => {
const d = CommonModel.toCommonModel(doc);
expect(d.items).not.toBeUndefined();
expect(Array.isArray(d.items)).toEqual(true);
(d.items as CommonModel[]).forEach((s, i) => {
for (const [i, s] of (d.items as CommonModel[]).entries()) {
expect(s.constructor.name).toEqual('CommonModel');
expect(s).toEqual(doc.items[i]);
});
}
});
});

Expand All @@ -66,11 +66,11 @@ describe('CommonModel', () => {
const d = CommonModel.toCommonModel(doc);
expect(d.properties).not.toBeUndefined();
expect(typeof d.properties).toEqual('object');
Object.keys(d.properties!).forEach(key => {
for (const key of Object.keys(d.properties!)) {
const s = d.properties![key];
expect(s.constructor.name).toEqual('CommonModel');
expect(s).toEqual(doc.properties[key]);
});
}
});
});

Expand Down
Loading

0 comments on commit 455eae8

Please sign in to comment.