Tslint: لا يوجد متغير غير مستخدم للنوع المستعار والواجهة عند استخدامها كأدوية.

تم إنشاؤها على ٢ أبريل ٢٠١٧  ·  24تعليقات  ·  مصدر: palantir/tslint

تقرير الشوائب

  • __ إصدار TSLint__: 5.0.0
  • __نسخة TypeScript__: 2.2.2
  • __ تشغيل TSLint عبر__: CLI

يتم فحص كود TypeScript

describe('Action', () => {
    describe('actionCreator', () => {
        type DummyActionType = 'FOO' | 'BAR';

        interface Point {
            readonly x: number;
            readonly y: number;
        }

        it('creates the correct FSA for primitives', () => {
            const creator = actionCreator<DummyActionType, number>('FOO');
            creator(7).should.deep.equal({ type: 'FOO', payload: 7 });
        });

        it('creates the correct FSA for objects', () => {
            const creator = actionCreator<DummyActionType, Point>('BAR');
            creator({ x: 1, y: 3 }).should.deep.equal({
                type: 'BAR',
                payload: { x: 1, y: 3 },
            });
        });
    });
});

بتهيئة tslint.json :

{
    "defaultSeverity": "error",
    "extends": [
        // "tslint-microsoft-contrib",
        "tslint:recommended",
        "tslint-react"
    ],

    "rulesDirectory": [
        "./node_modules/tslint-immutable/rules"
    ],

    // additional rules aside from inherited ones
    "rules": {
        "arrow-parens": [true, "ban-single-arg-parens"],

        // possible errors (core TSLint)
        "no-switch-case-fall-through": true,
        "no-string-throw": true,

        // stylistic (core TSLint)
        "array-type": [true, "array"],
        "interface-name": [true, "never-prefix"],
        "no-null-keyword": true,
        "no-require-imports": true,
        "object-literal-sort-keys": false,
        "ordered-imports": [false], // array makes VSCode happy
        "quotemark": [true, "single", "jsx-double", "avoid-escape"],

        // best practices (core TSLint)
        "linebreak-style": [true, "LF"],
        "max-file-line-count": [true, 300],
        "max-line-length": [true, 100],
        "no-magic-numbers": true,
        "no-unused-expression": true,
        "no-unused-variable": [true, "check-parameters", "react"],
        "one-line": [false],
        "prefer-const": true,

        // functional programming
        "no-let": true, // no-var on by default
        "no-this": true,
        "no-class": true,
        "no-new": false, // records?
        "readonly-interface": true,
        "readonly-indexer": true,
        "readonly-array": true,
        "no-mixed-interface": true,

        // react
        "jsx-no-multiline-js": false,

        // legal
        "file-header": [true, "[+ ]{10,}"]
    }
}

السلوك الفعلي

يشير إلى نوع الاسم المستعار والواجهة على أنهما غير مستخدمين.

src/frontend/actions/Action.test.ts
'DummyActionType' is declared but never used. (no-unused-variable)
  15 | describe('Action', () => {
  16 |     describe('actionCreator', () => {
> 17 |         type DummyActionType = 'FOO' | 'BAR';
     |             ^
  18 | 
  19 |         interface Point {
  20 |             readonly x: number;

'Point' is declared but never used. (no-unused-variable)
  17 |         type DummyActionType = 'FOO' | 'BAR';
  18 | 
> 19 |         interface Point {
     |                  ^
  20 |             readonly x: number;
  21 |             readonly y: number;
  22 |         }

سلوك متوقع

يتم استخدام الاسم المستعار للنوع والواجهة كوسائط عامة ، ويجب ألا تحتوي على أخطاء النسق.

P1 Requires Type Checker Fixed Bug

التعليق الأكثر فائدة

ما زلنا نشهد هذا أيضًا. إليك الحد الأدنى من الريبو (أصغر ما يمكنني العثور عليه):

box.ts:

export class Box<T> {
  value: T;
}

box_holder.ts:

import { Box } from './box';

export class BoxHolder<T> {
  box: Box<T>;
}

نتيجة:

ERROR: src/base/tests/box_holder.ts[3, 24]: 'T' is declared but never used.

إصدارات:

  • tslint: 5.1.0
  • مطبوعة: 2.3.0

ال 24 كومينتر

يشبه Microsoft / TypeScript # 14953

يقوم الكود الخاص بي بفحص الكتابة ، لذلك لا يبدو أنه خطأ من النوع. كحل بديل ، تم تشغيل "noUnusedLocals" و "noUnusedParameters" في tsconfig ، وتم إيقاف no-unused-variable .

في هذا الملف

import {buildModel} from '../src/compile/common';
import {FacetModel} from '../src/compile/facet';
import {LayerModel} from '../src/compile/layer';
import {Model} from '../src/compile/model';
import {UnitModel} from '../src/compile/unit';
import {initConfig} from '../src/config';
import {ExtendedSpec, FacetSpec, LayerSpec, normalize, TopLevel, UnitSpec} from '../src/spec';

export function parseModel(inputSpec: TopLevel<ExtendedSpec>): Model {
  const spec = normalize(inputSpec);
  return buildModel(spec, null, '', initConfig(inputSpec.config));
}

export function parseUnitModel(spec: TopLevel<UnitSpec>) {
  return new UnitModel(spec, null, '', initConfig(spec.config));
}

export function parseLayerModel(spec: TopLevel<LayerSpec>) {
  return new LayerModel(spec, null, '', initConfig(spec.config));
}

export function parseFacetModel(spec: TopLevel<FacetSpec>) {
  return new FacetModel(spec, null, '', initConfig(spec.config));
}

أحصل على هذه الأخطاء:

ERROR: test/util.ts[7, 9]: 'ExtendedSpec' is declared but never used.
ERROR: test/util.ts[7, 23]: 'FacetSpec' is declared but never used.
ERROR: test/util.ts[7, 34]: 'LayerSpec' is declared but never used.
ERROR: test/util.ts[7, 66]: 'UnitSpec' is declared but never used.

يبدو أن tslint يعتقد أن المتغيرات في الأدوية غير مستخدمة.

هذا أمر مزعج للغاية لأنني لا أستطيع إزالة الواردات. في الوقت الحالي ، لا بد لي من تعطيل خيار التحقق من المتغيرات غير المستخدمة.

لا أعتقد أن هذا هو https://github.com/Microsoft/TypeScript/issues/14953. أنتج joscha نسخة نظيفة في https://github.com/palantir/tslint/issues/2621 حيث لم يتم كسر الواردات (https://gist.github.com/joscha/6633bae73fb4b143cfb685b2754259c9). اهتزاز الأولوية على هذا.

في هذه الحالة لا يزال هناك استيراد React (غير معروض). إذا كنت تستخدم declare namespace React { class Component<T, U> {} } بدلاً من ذلك ، فسيختفي الخطأ.

ما زلت أواجه هذه المشكلة ولا يزال no-unused-variable غير قابل للاستخدام في tslint 5. يسعدني تقديم المساعدة من خلال تقديم أمثلة.

ما زلنا نشهد هذا أيضًا. إليك الحد الأدنى من الريبو (أصغر ما يمكنني العثور عليه):

box.ts:

export class Box<T> {
  value: T;
}

box_holder.ts:

import { Box } from './box';

export class BoxHolder<T> {
  box: Box<T>;
}

نتيجة:

ERROR: src/base/tests/box_holder.ts[3, 24]: 'T' is declared but never used.

إصدارات:

  • tslint: 5.1.0
  • مطبوعة: 2.3.0

من المثير للاهتمام ، أنه يبدو أن اسم الوحدة المستوردة مهم. أخذ المثال أعلاه مع استيراد box_holder.ts من box.ts ، وإعادة تسمية box_holder.ts إلى عدة أسماء مختلفة ، ينتج عنه:

bo.ts : خطأ
bo_.ts : خطأ
bow.ts : خطأ
box_.ts : خطأ
box_holder.ts : خطأ
boxa.ts : لا خطأ
boy.ts : لا خطأ
bx.ts : لا خطأ

جئت هنا من أجل هذا. أتلقى مجموعة كاملة من أخطاء النسالة الجديدة على غرار:

ERROR: src/app/app-state.ts[1, 1]: All imports are unused.
ERROR: src/app/app.component.ts[2, 15]: 'Platform' is declared but never used.
ERROR: src/auth/auth.service.ts[2, 10]: 'Http' is declared but never used.

كل هذه _لا _ يتم استخدامها كمتغيرات ، ولكن بالأحرى تعريفات النوع.

import { AuthState } from '../auth/models/auth-state'; // <-- "All imports are unused"

export declare interface AppState {
  auth: AuthState;
}

يجب أن يتم إصلاح هذا في typescript@next الآن. هل يمكن لكم جميعا اختباره؟

لا تزال تواجه نفس المشكلة على [email protected]
باستخدام [email protected]

import * as TypeMoq from 'typemoq';
import { MockedMethod } from '../mocking';
import { IComponent } from '../component';  // <-- All imports are unused
import { IComponentFactory } from '../componentfactory';

export class CreateComponentXMethodMock extends MockedMethod<IComponentFactory, IComponent> {
    constructor(mock: TypeMoq.IMock<IComponentFactory>) {
        super(mock, x => x.createComponentX(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAnyNumber()));
    }
}

حسنًا ، تمت إعادة فتح المشكلة. https://github.com/Microsoft/TypeScript/issues/14953#issuecomment -302101264

ثابت في TS 2.4

أرى هذا بدون خطأ في tsc :

interface SurveyAssetLayersProxyProps {
  survey: Survey
}

export default class SurveyAssetLayersControl extends React.Component<SurveyAssetLayersProxyProps, void> {
}

يعطيني: ERROR: 19:11 no-unused-variable 'SurveyAssetLayersProxyProps' is declared but never used.

mikew ربما يتم تشغيل tslint بإصدار TypeScript مختلف عن إصدار tsc الخاص بك؟

لم أكن أعلم أن هذا ممكن. كلاهما متعلق بالمشروع ، في ./node_modules/.bin/ . كيف سأبدأ في تأكيد ذلك؟

في 2 حزيران (يونيو) 2017 ، الساعة 2:52 مساءً ، كتب Andy [email protected] :

mikew https://github.com/mikew ربما يعمل tslint الخاص بك بإصدار TypeScript مختلف عن tsc؟

-
أنت تتلقى هذا لأنه تم ذكرك.
قم بالرد على هذه الرسالة الإلكترونية مباشرةً ، أو قم بعرضها على GitHub https://github.com/palantir/tslint/issues/2470#issuecomment-305864709 ، أو كتم صوت سلسلة المحادثات https://github.com/notifications/unsubscribe-auth/AAASeRvhBbEUpy0tQwk_wtObSYT9bsM3 .

استخدام tsc في tslint غير ممكن حقًا ... mikew ما هو الإصدار الدقيق 2.4 الذي تستخدمه كل ليلة؟

adidahiya في الوقت الحالي ، لا يعمل لأنكم تقومون بترقية devDependencies لكن ليس بـ peerDependencies . انظر هنا https://github.com/palantir/tslint/blob/3323ed2b0824a12c8b35c421ebf23c7d17cf788f/package.json#L51. نأمل في الحصول على إصدار التصحيح في أسرع وقت ممكن: ابتسم:

أوه ، لقد حاولت إزالة tslint وإعادة تثبيته. واختفى كتابته المطبعية المتداخلة. شيء ما خطأ في الغزل.

أواجه هذه المشكلة مع v5.6.0

tolgaek هل يمكنك تقديم نموذج التعليمات البرمجية؟ ما هو إصدار TS الخاص بك؟ هل يمكنك إعادة إنتاج الخطأ باستخدام خيار المترجم --noUnusedLocals بدلاً من tslint؟

hi @ andy-ms كنت أحاول إعادة إنشاء هذه المشكلة برمز بسيط ولكن الخطأ لن يظهر بعد ذلك. يبدو أن هذا الخطأ قد يكون بسبب تعقيد تطبيقنا بطريقة ما.

كما أنني لم أتمكن من الجري مع noUnusedLocals لكنني تلقيت خطأ ": خيار غير معروف" - noUnusedLocals '".

هذا هو الرمز الذي يظهر لي الخطأ:

import { StepUpProvider } from './step-up.provider';
import { Injectable } from '@angular/core';
import {
  Http,
  XHRBackend,
  RequestOptions,
  RequestOptionsArgs,
  Response,
  Request
} from '@angular/http';
import { Observable } from 'rxjs';

@Injectable()
export class RequestInterceptor extends Http {

  constructor(
    public backend: XHRBackend,
    public defaultOptions: RequestOptions,
    private stepUpProvider: StepUpProvider
  ) {
    super(backend, defaultOptions);
  }

  request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
    return this.stepUpProvider.intercept(super.request.bind(this, url, options));
  }
}

في الملف أعلاه ، أحصل على **.ts[8, 3]: 'Response' is declared but never used. على الرغم من استخدام Response لكتابة قيمة الإرجاع للدالة request

أرى هذا باستخدام tslint 5.10.0 ونسخة مطبوعة 2.9.2. أيه أفكار؟

🤖 بيب بوب! 👉 تم إهمال TSLint 👈 ويجب عليك التبديل إلى print-eslint ! 🤖

🔒 تم إقفال هذه المشكلة لمنع المزيد من المناقشات غير الضرورية. شكرا لك! 👋

هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات