Преглед на файлове

Add consistent-this lint rule

Lutz Roeder преди 9 месеца
родител
ревизия
6de2fae854
променени са 5 файла, в които са добавени 38 реда и са изтрити 22 реда
  1. 1 1
      eslint.config.js
  2. 25 15
      source/python.js
  3. 2 0
      source/pytorch.js
  4. 2 0
      test/worker.js
  5. 8 6
      tools/protoc.js

+ 1 - 1
eslint.config.js

@@ -29,7 +29,7 @@ export default [
             // 'complexity': 'error',
             'computed-property-spacing': 'error',
             'consistent-return': 'error',
-            // 'consistent-this': 'error',
+            'consistent-this': 'error',
             'constructor-super': 'error',
             'curly': 'error',
             'default-case': 'error',

+ 25 - 15
source/python.js

@@ -6,7 +6,9 @@ const python = {};
 python.Execution = class {
 
     constructor(sources) {
+        /* eslint-disable consistent-this */
         const self = this;
+        /* eslint-enable consistent-this */
         const execution = self;
         this._sources = sources || new Map();
         this._events = new Map();
@@ -11988,30 +11990,28 @@ python.Execution = class {
                 return torch._C.isCustomClass(this);
             }
             equals(rhs) {
-                const lhs = this;
-                switch (lhs.tag) {
+                switch (this.tag) {
                     case 'None': return rhs.isNone();
-                    case 'Bool': return rhs.isBool() && lhs.toBool() === rhs.toBool();
-                    case 'Int': return rhs.isInt() && lhs.toInt() === rhs.toInt();
-                    case 'Double': return rhs.isDouble() && lhs.toDouble() === rhs.toDouble();
-                    case 'String': return rhs.isString() && lhs.toString() === rhs.toString();
-                    case 'Tensor': return rhs.isTensor() && lhs.toTensor() === rhs.toTensor();
-                    case 'Object': return rhs.isObject() && lhs.toObject() === rhs.toObject();
-                    case 'Device': return rhs.isObject() && lhs.toDevice() === rhs.toDevice();
+                    case 'Bool': return rhs.isBool() && this.toBool() === rhs.toBool();
+                    case 'Int': return rhs.isInt() && this.toInt() === rhs.toInt();
+                    case 'Double': return rhs.isDouble() && this.toDouble() === rhs.toDouble();
+                    case 'String': return rhs.isString() && this.toString() === rhs.toString();
+                    case 'Tensor': return rhs.isTensor() && this.toTensor() === rhs.toTensor();
+                    case 'Object': return rhs.isObject() && this.toObject() === rhs.toObject();
+                    case 'Device': return rhs.isObject() && this.toDevice() === rhs.toDevice();
                     case 'GenericList': {
                         if (rhs.isList()) {
-                            const a = lhs.toList();
+                            const a = this.toList();
                             const b = rhs.toList();
                             return (a.length === b.length) && a.every((v, i) => v === b[i]);
                         }
                         return false;
                     }
-                    default: throw new python.Error(`IValue.equals() not implemented for '${lhs.tag}.`);
+                    default: throw new python.Error(`IValue.equals() not implemented for '${this.tag}.`);
                 }
             }
             is(rhs) {
-                const lhs = this;
-                return lhs.equals(rhs);
+                return this.equals(rhs);
             }
             type() {
                 switch (this.tag) {
@@ -13010,7 +13010,9 @@ python.Execution = class {
                 this.error_messages = new Map();
             }
             setVariableTypeError(name, msg) {
+                /* eslint-disable consistent-this */
                 let runner = this;
+                /* eslint-enable consistent-this */
                 while (runner.next) {
                     runner = runner.next;
                 }
@@ -13045,7 +13047,10 @@ python.Execution = class {
                 this.type_table.set(name, type);
             }
             findInAnyFrame(name) {
-                for (let runner = this; runner; runner = runner.next) {
+                /* eslint-disable consistent-this */
+                const self = this;
+                /* eslint-enable consistent-this */
+                for (let runner = self; runner; runner = runner.next) {
                     const r = runner.findInThisFrame(name);
                     if (r) {
                         return r;
@@ -16635,7 +16640,10 @@ python.Execution = class {
                 return null;
             }
             findInAnyFrame(name) {
-                for (let runner = this; runner; runner = runner.next) {
+                /* eslint-disable consistent-this */
+                const self = this;
+                /* eslint-enable consistent-this */
+                for (let runner = self; runner; runner = runner.next) {
                     const r = runner.findInThisFrame(name);
                     if (r) {
                         return r;
@@ -19672,7 +19680,9 @@ python.Execution = class {
             return this.expression(stmt.value, context);
         } else if (stmt instanceof ast.FunctionDef) {
             const module = context.get('__name__');
+            /* eslint-disable consistent-this */
             const self = this;
+            /* eslint-enable consistent-this */
             const parent = context.get('__class__');
             const type = (parent === builtins.module) ? builtins.function : builtins.method;
             const func = {

+ 2 - 0
source/pytorch.js

@@ -1473,7 +1473,9 @@ pytorch.Execution = class extends python.Execution {
     constructor(sources, metadata) {
         super(sources);
         this._metadata = metadata;
+        /* eslint-disable consistent-this */
         const execution = this;
+        /* eslint-enable consistent-this */
         const torch = this.torch;
         this.registerFunction('torch.jit.jit_module_from_flatbuffer', (f) => {
             const cu = new torch.jit.CompilationUnit();

+ 2 - 0
test/worker.js

@@ -436,7 +436,9 @@ export class Target {
             const reader = response.body.getReader();
             const length = response.headers.has('Content-Length') ? parseInt(response.headers.get('Content-Length'), 10) : -1;
             let position = 0;
+            /* eslint-disable consistent-this */
             const target = this;
+            /* eslint-enable consistent-this */
             const stream = new ReadableStream({
                 async start(controller) {
                     const read = async () => {

+ 8 - 6
tools/protoc.js

@@ -43,19 +43,21 @@ protoc.Namespace = class extends protoc.Object {
         if (path && path.length > 0 && path[0] === '') {
             throw new protoc.Error('Invalid path.');
         }
-        let parent = this;
+        /* eslint-disable consistent-this */
+        let current = this;
+        /* eslint-enable consistent-this */
         while (path.length > 0) {
             const part = path.shift();
-            if (parent.children && parent.children.get(part)) {
-                parent = parent.children.get(part);
-                if (!(parent instanceof protoc.Namespace)) {
+            if (current.children && current.children.get(part)) {
+                current = current.children.get(part);
+                if (current instanceof protoc.Namespace === false) {
                     throw new protoc.Error('Invalid path.');
                 }
             } else {
-                parent = new protoc.Namespace(parent, part);
+                current = new protoc.Namespace(current, part);
             }
         }
-        return parent;
+        return current;
     }
 
     defineType(name) {