Skip to content

Commit

Permalink
Add prefer-destructuring lint rule
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzroeder committed Dec 1, 2023
1 parent c56b17e commit 5b8e608
Show file tree
Hide file tree
Showing 52 changed files with 463 additions and 670 deletions.
6 changes: 3 additions & 3 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,13 @@ const install = async () => {
let exists = await access(node_modules);
if (exists) {
const dependencies = Object.assign({}, configuration.dependencies, configuration.devDependencies);
const matches = await Promise.all(Object.entries(dependencies).map(async (entry) => {
const file = path.join('node_modules', entry[0], 'package.json');
const matches = await Promise.all(Object.entries(dependencies).map(async ([name, version]) => {
const file = path.join('node_modules', name, 'package.json');
const exists = await access(file);
if (exists) {
const content = await fs.readFile(file, 'utf8');
const obj = JSON.parse(content);
return obj.version === entry[1];
return obj.version === version;
}
return false;
}));
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
"no-unused-private-class-members": "error",
"no-use-before-define": "error",
"object-curly-spacing": [ "error", "always" ],
"prefer-const": [ "error", { "destructuring": "all" } ],
"prefer-const": [ "error" ],
"prefer-destructuring": [ "error", { "array": false } ],
"require-atomic-updates": "error",
"semi": [ "error", "always" ],
"space-before-blocks": "error",
Expand Down
16 changes: 6 additions & 10 deletions source/acuity.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ acuity.Graph = class {
});
}
acuity.Inference.infer(model.Layers);
for (const entry of values) {
const type = new acuity.TensorType(null, new acuity.TensorShape(entry[1].shape));
const value = new acuity.Value(entry[0], type, null, null);
values.set(entry[0], value);
for (const [name, obj] of values) {
const type = new acuity.TensorType(null, new acuity.TensorShape(obj.shape));
const value = new acuity.Value(name, type, null, null);
values.set(name, value);
}
for (const layerName of Object.keys(model.Layers)) {
const layer = model.Layers[layerName];
Expand Down Expand Up @@ -241,9 +241,7 @@ acuity.Inference = class {
'reduceany', 'reducemax', 'reducemean', 'reducemin', 'reduceprod', 'reducesum'
]);
const operators = new Map();
operators.set('broadcast', (inputs) => {
const a = inputs[0];
const b = inputs[1];
operators.set('broadcast', ([a, b]) => {
const longer = a.length >= b.length ? a.slice() : b.slice();
const shorter = a.length < b.length ? a.slice() : b.slice();
const remain = longer.length - shorter.length;
Expand Down Expand Up @@ -305,9 +303,7 @@ acuity.Inference = class {
const newShape = params.return_sequences ? [ inputs[0][0], inputs[0][1], output ] : [ batch, output ];
return [ newShape, [batch, output], [batch, params.weights] ];
});
operators.set('matmul', (inputs, params) => {
const a = inputs[0];
const b = inputs[1];
operators.set('matmul', ([a, b], params) => {
let newShape = a.slice(0, -2);
if (params.transpose_a) {
newShape = newShape.concat(a.slice(-1));
Expand Down
24 changes: 10 additions & 14 deletions source/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,9 +767,7 @@ app.View = class {
}

update(data) {
for (const entry of Object.entries(data)) {
const name = entry[0];
const value = entry[1];
for (const [name, value] of Object.entries(data)) {
switch (name) {
case 'path': {
if (value) {
Expand Down Expand Up @@ -1035,15 +1033,14 @@ app.MenuService = class {

_updateLabel(view) {
let rebuild = false;
for (const entry of this._commandTable.entries()) {
for (const [name, command] of this._commandTable.entries()) {
if (this._menu) {
const menuItem = this._menu.getMenuItemById(entry[0]);
const command = entry[1];
const item = this._menu.getMenuItemById(name);
if (command && command.label) {
const label = command.label(view);
if (label !== menuItem.label) {
if (this._itemTable.has(entry[0])) {
this._itemTable.get(entry[0]).label = label;
if (label !== item.label) {
if (this._itemTable.has(name)) {
this._itemTable.get(name).label = label;
rebuild = true;
}
}
Expand All @@ -1054,12 +1051,11 @@ app.MenuService = class {
}

_updateEnabled(view) {
for (const entry of this._commandTable.entries()) {
for (const [name, command] of this._commandTable.entries()) {
if (this._menu) {
const menuItem = this._menu.getMenuItemById(entry[0]);
const command = entry[1];
if (menuItem && command.enabled) {
menuItem.enabled = command.enabled(view);
const item = this._menu.getMenuItemById(name);
if (item && command.enabled) {
item.enabled = command.enabled(view);
}
}
}
Expand Down
18 changes: 7 additions & 11 deletions source/armnn.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ armnn.ModelFactory = class {
const extension = identifier.split('.').pop().toLowerCase();
const stream = context.stream;
if (stream && extension === 'armnn') {
return [ 'armnn.flatbuffers', stream ];
return { name: 'armnn.flatbuffers', value: stream };
}
if (extension === 'json') {
const obj = context.peek('json');
if (obj && obj.layers && obj.inputIds && obj.outputIds) {
return [ 'armnn.flatbuffers.json', obj ];
return { name: 'armnn.flatbuffers.json', value: obj };
}
}
return undefined;
Expand All @@ -24,10 +24,10 @@ armnn.ModelFactory = class {
await context.require('./armnn-schema');
armnn.schema = flatbuffers.get('armnn').armnnSerializer;
let model = null;
switch (target[0]) {
switch (target.name) {
case 'armnn.flatbuffers': {
try {
const stream = target[1];
const stream = target.value;
const reader = flatbuffers.BinaryReader.open(stream);
model = armnn.schema.SerializedGraph.create(reader);
} catch (error) {
Expand All @@ -38,7 +38,7 @@ armnn.ModelFactory = class {
}
case 'armnn.flatbuffers.json': {
try {
const obj = target[1];
const obj = target.value;
const reader = flatbuffers.TextReader.open(obj);
model = armnn.schema.SerializedGraph.createText(reader);
} catch (error) {
Expand Down Expand Up @@ -172,16 +172,12 @@ armnn.Node = class {
}
if (layer.layer) {
if (layer.layer.descriptor && this.type.attributes) {
for (const entry of Object.entries(layer.layer.descriptor)) {
const name = entry[0];
const value = entry[1];
for (const [name, value] of Object.entries(layer.layer.descriptor)) {
const attribute = new armnn.Attribute(metadata.attribute(type, name), name, value);
this.attributes.push(attribute);
}
}
for (const entry of Object.entries(layer.layer).filter((entry) => entry[1] instanceof armnn.schema.ConstTensor)) {
const name = entry[0];
const tensor = entry[1];
for (const [name, tensor] of Object.entries(layer.layer).filter((entry) => entry[1] instanceof armnn.schema.ConstTensor)) {
const value = new armnn.Value('', tensor.info, new armnn.Tensor(tensor));
this.inputs.push(new armnn.Argument(name, [ value ]));
}
Expand Down
4 changes: 2 additions & 2 deletions source/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1050,10 +1050,10 @@ base.Telemetry = class {
params.engagement_time_msec = this._engagement_time_msec;
this._engagement_time_msec = 0;
}
const build = (entires) => entires.map((entry) => entry[0] + '=' + encodeURIComponent(entry[1])).join('&');
const build = (entires) => entires.map(([name, value]) => name + '=' + encodeURIComponent(value)).join('&');
this._cache = this._cache || build(Array.from(this._config));
const key = (name, value) => this._schema.get(name) || ('number' === typeof value && !isNaN(value) ? 'epn.' : 'ep.') + name;
const body = build(Object.entries(params).map((entry) => [ key(entry[0], entry[1]), entry[1] ]));
const body = build(Object.entries(params).map(([name, value]) => [ key(name, value), value ]));
const url = 'https://analytics.google.com/g/collect?' + this._cache;
this._navigator.sendBeacon(url, body);
this._session[2] = this.get('session_engaged') || '0';
Expand Down
2 changes: 1 addition & 1 deletion source/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ host.BrowserHost = class {
} else {
const match = error.stack.match(/.*\n\s*(.*)\s*/);
if (match) {
stack = match[1];
[, stack] = match;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/caffe.js
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ caffe.Utility = class {
if (type) {
caffe.Utility._enumKeyMap = caffe.Utility._enumKeyMap || new Map();
if (!caffe.Utility._enumKeyMap.has(name)) {
const map = new Map(Object.entries(type).map((pair) => [ pair[1], pair[0] ]));
const map = new Map(Object.entries(type).map(([name, value]) => [ value, name ]));
caffe.Utility._enumKeyMap.set(name, map);
}
const map = caffe.Utility._enumKeyMap.get(name);
Expand Down
2 changes: 1 addition & 1 deletion source/caffe2.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ caffe2.ModelFactory = class {
return undefined;
}
const schema = [[1,2],[2,2],[3,2],[4,0],[5,2],[6,2],[7,2],[8,2],[9,2]];
if (schema.every((pair) => !tags.has(pair[0]) || tags.get(pair[0]) === pair[1])) {
if (schema.every(([key, value]) => !tags.has(key) || tags.get(key) === value)) {
const stream = context.stream;
if (stream.length > 3) {
const buffer = stream.peek(Math.min(stream.length, 67));
Expand Down
12 changes: 4 additions & 8 deletions source/circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ circle.ModelFactory = class {
try {
const archive = zip.Archive.open(stream);
if (archive) {
for (const entry of archive.entries) {
attachments.set(entry[0], entry[1]);
for (const [name, value] of archive.entries) {
attachments.set(name, value);
}
}
} catch (error) {
Expand Down Expand Up @@ -334,9 +334,7 @@ circle.Node = class {
this._attributes.push(attribute);
decoded = true;
} else if (custom_options) {
for (const pair of Object.entries(custom_options)) {
const key = pair[0];
const value = pair[1];
for (const [key, value] of Object.entries(custom_options)) {
const schema = metadata.attribute(type.name, key);
const attribute = new circle.Attribute(schema, key, value);
this._attributes.push(attribute);
Expand All @@ -355,9 +353,7 @@ circle.Node = class {
}
const options = node.builtin_options;
if (options) {
for (const entry of Object.entries(options)) {
const name = entry[0];
const value = entry[1];
for (const [name, value] of Object.entries(options)) {
if (name === 'fused_activation_function' && value !== 0) {
const activationFunctionMap = { 1: 'Relu', 2: 'ReluN1To1', 3: 'Relu6', 4: 'Tanh', 5: 'SignBit' };
if (!activationFunctionMap[value]) {
Expand Down
9 changes: 4 additions & 5 deletions source/cntk.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,7 @@ cntk.Node = class {
const type = obj.__type__;
this._type = metadata.type(type) || { name: type };
this._name = obj.name;
for (const entry of Object.entries(obj)) {
const name = entry[0];
const value = entry[1];
for (const [name, value] of Object.entries(obj)) {
if (name != '__type__' && name != 'name' && name != 'inputs' && name != 'precision') {
this._attributes.push(new cntk.Attribute(metadata.attribute(type, name), name, value));
}
Expand All @@ -374,8 +372,9 @@ cntk.Node = class {
}
}
if (obj.attributes) {
for (const entry of Object.entries(obj.attributes)) {
this._attributes.push(new cntk.Attribute(metadata.attribute(this._type, entry[0]), entry[0], entry[1]));
for (const [name, value] of Object.entries(obj.attributes)) {
const attribute = new cntk.Attribute(metadata.attribute(this._type, name), name, value);
this._attributes.push(attribute);
}
}
inputs = obj.inputs.map((input) => arg(input, version));
Expand Down
33 changes: 14 additions & 19 deletions source/coreml.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,10 @@ coreml.ModelFactory = class {
}
const weightPaths = new Set();
const walkProgram = (program) => {
for (const entry of Object.entries(program.functions)) {
const func = entry[1];
for (const entry of Object.entries(func.block_specializations)) {
const block = entry[1];
for (const func of Object.values(program.functions)) {
for (const block of Object.values(func.block_specializations)) {
for (const operation of block.operations) {
for (const entry of Object.entries(operation.attributes)) {
const value = entry[1];
for (const value of Object.values(operation.attributes)) {
if (value.blobFileValue && value.blobFileValue.fileName) {
weightPaths.add(value.blobFileValue.fileName);
}
Expand Down Expand Up @@ -266,8 +263,8 @@ coreml.Node = class {
const values = argument.value.map((value) => value.obj);
return new coreml.Argument(argument.name, argument.visible, values);
});
this.attributes = Object.entries(obj.attributes).map((entry) => {
return new coreml.Attribute(metadata.attribute(obj.type, entry[0]), entry[0], entry[1]);
this.attributes = Object.entries(obj.attributes).map(([name, value]) => {
return new coreml.Attribute(metadata.attribute(obj.type, name), name, value);
});
}
};
Expand Down Expand Up @@ -720,10 +717,9 @@ coreml.Transformer = class {
};
if (data) {
const map = weights(type, data, initializers);
for (const entry of Object.entries(data)) {
const name = entry[0];
for (const [name, value] of Object.entries(data)) {
if (!map[name]) {
obj.attributes[name] = entry[1];
obj.attributes[name] = value;
}
}
}
Expand Down Expand Up @@ -1205,20 +1201,19 @@ coreml.Transformer = class {
type: op.type,
attributes: {}
};
for (const entry of Object.entries(op.attributes)) {
const key = entry[0];
operation.attributes[key] = convertValue(entry[1]);
for (const [key, value] of Object.entries(op.attributes)) {
operation.attributes[key] = convertValue(value);
}
operation.inputs = Object.entries(op.inputs).map((entry) => {
const args = entry[1].arguments.map((argument) => {
operation.inputs = Object.entries(op.inputs).map(([name, input]) => {
const args = input.arguments.map((argument) => {
if (argument.name) {
const value = this.input(argument.name);
value.to.push(operation);
return value;
}
return { value: argument.value };
});
return { name: entry[0], value: args };
return { name: name, value: args };
});
operation.outputs = op.outputs.map((output) => {
const value = this.input(output.name);
Expand Down Expand Up @@ -1322,7 +1317,7 @@ coreml.Utility = class {
if (type) {
coreml.Utility._enumKeyMap = coreml.Utility._enumKeyMap || new Map();
if (!coreml.Utility._enumKeyMap.has(name)) {
const map = new Map(Object.entries(type).map((pair) => [ pair[1], pair[0] ]));
const map = new Map(Object.entries(type).map(([key, value]) => [ value, key ]));
coreml.Utility._enumKeyMap.set(name, map);
}
const map = coreml.Utility._enumKeyMap.get(name);
Expand Down Expand Up @@ -1403,7 +1398,7 @@ coreml.Utility = class {

static tensorType(type) {
if (!coreml.Utility._dataTypes) {
coreml.Utility._dataTypes = new Map(Object.entries(coreml.proto.MILSpec.DataType).map(((entry) => [entry[1], entry[0].toLowerCase()])));
coreml.Utility._dataTypes = new Map(Object.entries(coreml.proto.MILSpec.DataType).map((([key, value]) => [value, key.toLowerCase()])));
coreml.Utility._dataTypes.delete(0);
coreml.Utility._dataTypes.set(1, 'boolean');
}
Expand Down
4 changes: 1 addition & 3 deletions source/dagre.js
Original file line number Diff line number Diff line change
Expand Up @@ -1737,9 +1737,7 @@ dagre.layout = (graph, layout) => {
for (const xs of Object.values(xss)) {
let max = Number.NEGATIVE_INFINITY;
let min = Number.POSITIVE_INFINITY;
for (const entry of Object.entries(xs)) {
const v = entry[0];
const x = entry[1];
for (const [v, x] of Object.entries(xs)) {
const halfWidth = g.node(v).label.width / 2;
max = Math.max(x + halfWidth, max);
min = Math.min(x - halfWidth, min);
Expand Down
Loading

0 comments on commit 5b8e608

Please sign in to comment.