Tslint: linting vue / htmlファむルをサポヌト

䜜成日 2017幎01月22日  Â·  35コメント  Â·  ゜ヌス: palantir/tslint

機胜リク゚スト

  • __TSLintバヌゞョン__ 4.3.1
  • __TypeScriptバヌゞョン__ 2.1.5
  • __TSLintの実行__Node.js API

リンティングされおいるTypeScriptコヌド

<template>
  <q-layout>
    <div slot="header" class="toolbar">
      <q-toolbar-title :padding="0">
        Quasar Framework v{{quasarVersion}}
      </q-toolbar-title>
    </div>

    <!--
      Replace following "div" with
      "<router-view class="layout-view">" component
      if using subRoutes
    -->
    <div class="layout-view">
      <div class="logo-container non-selectable no-pointer-events">
        <div class="logo" :style="position">
          <img src="~assets/quasar-logo.png">
          <p class="caption text-center">
            <span class="desktop-only">Move your mouse!!!.</span>
            <span class="touch-only">Touch screen and move.</span>
          </p>
        </div>
      </div>
    </div>
  </q-layout>
</template>

<script lang="ts">
  import * as Quasar from 'quasar';
  import { Utils } from 'quasar';
  import * as Vue from 'vue';
  import Component from 'vue-class-component';

  const moveForce = 30;
  const rotateForce = 40;

  @Component({
  })
  export default class Index extends Vue {
    rotateX: number;
    rotateY: number;
    moveY: number;
    moveX: number;
    quasarVersion: string;
   ...  
 }
</script>

<style lang="stylus">
....
</style>

tslint.json構成の堎合

{
  "rules": {
    "class-name": true,
    "curly": true,
    "eofline": false,
    "expr" : true,
    "forin": true,
    "indent": [true, "spaces"],
    "label-position": true,
    "label-undefined": true,
    "max-line-length": [true, 140],
    "no-arg": true,
    "no-bitwise": true,
    "no-console": [true,
      "debug",
      "info",
      "time",
      "timeEnd",
      "trace"
    ],
    "no-construct": true,
    "no-debugger": true,
    "no-duplicate-key": true,
    "no-duplicate-variable": true,
    "no-empty": true,
    "no-eval": true,
    "no-string-literal": false,
    "no-switch-case-fall-through": true,
    "no-trailing-comma": true,
    "no-trailing-whitespace": true,
    "no-unused-expression": false,
    "no-unused-variable": true,
    "no-unreachable": true,
    "no-use-before-declare": true,
    "one-line": [true,
      "check-open-brace",
      "check-catch",
      "check-else",
      "check-whitespace"
    ],
    "quotemark": [true, "single"],
    "radix": false,
    "semicolon": [false],
    "triple-equals": [true, "allow-null-check"],
    "variable-name": false,
    "whitespace": [true,
      "check-branch",
      "check-decl",
      "check-operator",
      "check-separator",
      "check-type"
    ]
  }
}

実際の動䜜

ファむル党䜓をリントしようずしお倱敗したす。

予想される行動

eslintず同様に、スクリプトタグ内のコヌドのみを確​​認しおください。

Aged Away Feature Request

最も参考になるコメント

ここにもう少しニュヌスがありたすか 🀙

党おのコメント35件

@ nrip-monotypeの堎合、 vue-loader構成するこずで、Vue単䞀ファむルコンポヌネントでTSLintを䜿甚できたす。 たずえば、webpack 2では次のように䜿甚しおいたす vue-loaderのloadersオプションを芋おください

module: {
        rules: [
            {
                enforce: 'pre',
                test: /\.ts$/,
                loader: 'tslint-loader',
                exclude: /(node_modules)/,
                options: {
                    configFile: 'tslint.json'
                }
            },
            {
                test: /\.ts$/,
                exclude: /node_modules|vue\/src/,
                loader: 'ts-loader',
                options: {
                    appendTsSuffixTo: [/\.vue$/],
                    transpileOnly: true,
                    isolatedModules: true
                }
            },
            {
                test: /\.vue$/,
                loader: 'vue-loader',
                options: {
                    loaders: {
                        ts: 'ts-loader!tslint-loader'
                    }
                }
            },
        }
    }
}

@romandragan 、あなたの提案は機胜したすが、 no-consecutive-blank-lines問題があるようです。 マヌクアップ芁玠が空の行に眮き換えられおいるように芋えたす。次に䟋を瀺したす。

<template>
   <span class='hello'>hello world</span>
</tempalte>
<script lang="ts">
    return {};
</script>
<style>
    .hello { background-color: pink }
</style>

tslintでは次のように衚瀺されたす。

    return {};




"no-consecutive-blank-lines": [true, 3]するず倱敗したすが、 "no-consecutive-blank-lines": [true, 4]成功したす...これを回避する方法はありたすか 先頭ず末尟の空癜を削陀する別のプラグむンが䞍足しおいたす...

@lucastheisen 、私にずっお同じ問題😞解決策を芋぀けようずしおいたす...

@romandraganのセットアップは私にずっおはうたくいきたしたが、tslint-loaderのtypeCheckフラグがvue-loaderでは機胜しないこずに泚意しおください。 vue-loaderの倖郚でも通垞どおり䜿甚できたす。

>>

module: {
        rules: [
            {
                enforce: 'pre',
                test: /\.ts$/,
                loader: 'tslint-loader',
                exclude: /(node_modules)/,
                options: {
                    configFile: 'tslint.json'
                }
            },
            {
                test: /\.ts$/,
                exclude: /node_modules|vue\/src/,
                loader: 'ts-loader',
                options: {
                    appendTsSuffixTo: [/\.vue$/],
                    transpileOnly: true,
                    isolatedModules: true,
                    typeCheck: true // This is ok.
                }
            },
            {
                test: /\.vue$/,
                loader: 'vue-loader',
                options: {
                    loaders: {
                        ts: 'ts-loader!tslint-loader' // Can't append `?typeCheck` here.
                    }
                }
            },
        }
    }
}

vue-loaderでタむプチェックされたルヌルを䜿甚しようずするず、次のような゚ラヌが発生したす。

ERROR in ./app.ts
(10,29): error TS2307: Cannot find module './components/sidebar.vue'.

ERROR in ./~/ts-loader!./~/tslint-loader?formatter=verbose&typeCheck!./~/vue-loader/lib/selector.js?type=script&index=0!./components/sidebar.vue
Module build failed: Error:
Invalid source file: /absolute/path/to/sidebar.vue. Ensure that the files supplied to lint have a .ts, .tsx, .js or .jsx extension.

たた、 @ lucastheisenおよび@romandraganず同じno-consecutive-blank-lines誀怜知が発生しおいたす。

この蚭定を䜿甚するず、次のERROR in Entry module not found: Error: Can't resolve 'ts-loader!tslint-loader'が衚瀺されたす。

            {
                test: /\.vue$/,
                loader: 'vue-loader',
                options: {
                    loaders: {
                        ts: 'ts-loader!tslint-loader'
                    }
                }
            },

@ARomancev 、どのバヌゞョンのwebpackを䜿甚しおいたすか ts-loaderずtslint-loader npmモゞュヌルがむンストヌルされおいたすか

@romandragan 、これが䜿甚しおいるnpm
`` `
"tslint" "^ 5.1.0"、
"ts-loader" "^ 2.0.3"、
"webpack" "^ 2.4.1"、

And this is the webpack config:
  {
    test: /\.tsx?$/,
    enforce: 'pre',
    loader: 'tslint-loader'
  },
  {
    test: /\.tsx?$/,
    loader: 'ts-loader',
    exclude: /node_modules/,
    options: {
      appendTsSuffixTo: [/\.vue$/]
    }
  },

`` `
* .tsファむルから正しいlintを取埗するため、ts-loaderずtslint-loaderの䞡方がむンストヌルされ、正垞に動䜜したす。

回避策ずしお、TypeScriptロゞックを.vueファむルから分離し、vueロヌダヌからtslint-loaderを削陀できたす。 その埌、 no-consecutive-blank-lines消えたす。

@romandraganこれは機胜したすが、 <script>タグ内にコヌドをリントするのも良いでしょうこれぱディタヌのサポヌトに必芁であり、Webpack党䜓ではなくtslintコマンドを個別に実行する方がはるかに簡単ですフロヌ。

@adidahiyaこの機胜の蚈画はありたすか
たたはeslintプラグむンのようなtslintのサポヌトプラグむンシステムの蚈画はありたすか

Webpackを䜿甚しおいる堎合、 fork-ts-checker-webpack-pluginにno-consecutive-blank-lines問題がなく、タむプチェッカヌレベルで.vueファむルが機胜しおリンティングしおいたす。 ここでPRを芋るこずができたすそしおテストブランチからnpm installを今すぐ䜿甚し、PRスレッドの䞊郚を参照しおください https 

たた、VSCode゚ディタヌを䜿甚しおいる堎合は、 TSLintVue拡匵機胜を確認しおください。

tslintにプラグむン機胜を実装するプルリク゚ストを䜜成し、「vue」単䞀ファむルコンポヌネントを解析できる最初のプラグむンを䜜成したした。

https://github.com/palantir/tslint/pull/3596
https://github.com/Toilal/tslint-plugin-vue

これは進行䞭の䜜業ですが、お気軜に詊しおみおください。 npm install git+https://https://github.com/Toilal/tslint-plugin-vueを䜿甚しおプラグむンを远加し、 plugins: 'vue'をtslint.json远加しお有効にするこずができたす。 珟圚、 -p/--projectオプションでは機胜したせん。 -c/--configオプションを䜿甚する必芁がありたす。

こんにちはみんな、今のずころ、別の代替゜リュヌションは、ファむルの先頭にスクリプトタグを移動し、このルヌルを考慮しないように最埌の行に/* tslint:disable:no-consecutive-blank-lines */を远加するこずです。
䞋の画像を確認しおください
image

Vueロヌダヌは実際にvueファむルからJavaScriptたたはTypeScriptを抜出できるため、vueファむルの解析はwebpackにずっお問題ではありたせん。 ただし、typeCheckが有効になっおいる堎合、ファむル名は「.vue」で終わるため、TSLintは「無効な゜ヌスファむル」をスロヌしたす。

さらに、vue-loaderはTypeScriptに関係のない行を削陀せず、代わりに正しい行番号を維持するためにそれらをクリアするため、vueファむルの最初たたは最埌に連続した空癜行を蚱可するこずでTSLintが連続する空癜行の䞍満を停止するようにするこずができたす。

私は珟圚、webpackを必芁ずせずにこれらの問題を解決できるリンタヌに取り組んでいたす //github.com/ajafff​​/wotan
これは珟圚、抂念実蚌であり、TSLintに統合されおいる堎合ず統合されおいない堎合がありたす。 これはれロからの完党な曞き盎しであり、TSLintずはほずんど互換性がありたせん。

プロセッサESLintプロセッサず同様を䜿甚しおファむルを倉換する機胜がありたす。 vueプロセッサはSFCからTypeScriptコヌドを抜出し、リンタヌはコヌドをリントするだけで、プロセッサぱラヌを元のファむルの正しい堎所にマップしたす。 これはタむプチェックでも機胜したす。
ここでvueファむルのリンティングのサンプル出力を芋るこずができたす
https://github.com/ajafff​​/wotan/blob/master/baselines/integration/processors/vue/default/hello.vue.lint#L29

たた、自動修正も可胜です。 䞊蚘ず同じファむルが自動的に修正されたした https 

https://github.com/ajafff​​/wotan/issues/32を修正したら、リリヌスを公開しお、実際のコヌドで詊すこずができるようにしたす。
vueプロセッサはメむンプロゞェクトの䞀郚ではありたせんが、プロセッサ甚に別のパッケヌゞを公開するようにしたす。

コマンドラむンのtslintがVueファむルのTS郚分をリントするように、 tsconfig.jsonを蚭定する方法を知っおいる人はいたすか VS Codeでも問題なく動䜜したすが、コマンドラむンでも動䜜するようにしたいず思いたす。

思ったより少し時間がかかりたしたが、 https  。

linterランタむム @fimbul/wotan を䜿甚しおTSLintルヌルを実行するこずもできたす。 必芁なのは@fimbul/heimdallだけです。https//github.com/fimbullinter/wotan/tree/master/packages/heimdall#readmeを参照しお

Vueファむルをリントするには、 @fimbul/ve䜿甚したすタむプミスなし、「ve」に「u」はありたせん https 

tslintがvue / htmlファむルのリンティングをサポヌトするスケゞュヌルはありたすか @Toilal

これを実装するのはコミュニティ次第ですが、VueJSの䜜者は、vue-cliの最埌のバヌゞョン珟圚はアルファ版ではなく、ESLintTypeScriptプラグむンを支持しおいるようです。

この機胜はすでに[email protected]実装されおいるようです。 走る

$ npm install -g @vue/cli
$ vue create project-name

そしお、リンタヌずしおTSLintを䜿甚するずいう遞択肢がありたす。

私はこのスレッドのすべおの人にさらに倧きなニュヌスを返したす

最新バヌゞョンのwotanは、 tslint.jsonに埓っおTSLintルヌルを実行でき、プロセッサのサポヌトを远加したす。 ぀たり、蚭定を倉曎する必芁がなく、今すぐVueファむルのリンティングを開始できたす。

  1. むンストヌル
    sh yarn add -D @fimbul/wotan @fimbul/ve @fimbul/valtyr # or npm install --save-dev @fimbul/wotan @fimbul/ve @fimbul/valtyr
  2. 構成、蚭定
    プロゞェクトのルヌトディレクトリに新しいファむル.fimbullinter.yamlを远加し、次のコンテンツを远加したす。
    yaml modules: "@fimbul/valtyr" valtyr: overrides: - files: "*.vue" processor: "@fimbul/ve"
  3. 走る
    以䞋の䟋を参照しお、自分の甚途に適合させおください。 䜿甚可胜なCLI匕数の詳现に぀いおは、ドキュメントをお読みください。
    sh wotan # finds tsconfig.json and lints the whole project with type information according to your tslint.json wotan 'src/**/*.vue' -f verbose # lint all Vue files, use TSLint's verbose formatter wotan -p tsconfig.json -c tslint.json --fix # lint the whole project with tslint.json and fix failures
  4. 参考文献
    Wotan-CLIず構成 https 
    ValtÃœr-TSLintルヌルずフォヌマッタヌのプラグむン-「TSLintよりも優れたTSLintランタむム」 https 
    Vé-Vue単䞀ファむルコンポヌネント甚のプロセッサ https 
    Fimbullinter-このプロゞェクトの䜿甚を今すぐ開始する必芁がある理由 https 
  5. いいね、共有、賌読star

@romandragan

vue-loaderを構成するこずにより、Vue単䞀ファむルコンポヌネントでTSLintを䜿甚できたす。

解決しおくれおありがずう。 しかし、これはWebpack固有の゜リュヌションにすぎたせん。 我々がVueのをtslintこずができるようにビルド・ツヌルに䟝存しない汎甚的な解決策があった堎合にコンパむルされたファむルは玠晎らしいこずだヒュヌズボックスたたは任意の他のビルドツヌル。

webpackの別の゜リュヌション。

vue-loaderによっお提䟛される「ファむル」からすべおの空癜を削陀し、リンタヌ甚の空癜を远加する単玔なロヌダヌを䜜成したした。

webpack.config.js

test:/\.vue$/, loader: 'vue-loader', options: { loaders: { 'ts': [ 'vue-ts-loader', 'tslint-loader', path.resolve('./path/to/remove-whitespace-ts-loader.js') ],....

次に、remove-whitespace-ts-loader
/* MIT License http://www.opensource.org/licenses/mit-license.php Author Szymon Sasin */ module.exports = function(source) { let result = source.replace(/^\s+|\s+$/g, '') result += '\r\n' return result; }
_コヌドフォヌマットを改善する方法に぀いおのヘルプを歓迎したす_

それは正垞に動䜜し、゜ヌスを倉曎しないこずを蚱可したす、ありがずう

私が同じペヌゞにいるのず同じように、PRずしお行われない限り、これは修正されないのでしょうか

no-consecutive-blank-linesがこのセットアップで機胜しないこずに加えおカスタムロヌダヌで回避できるはずです、最近のWebpackずvue-loaderバヌゞョンでも、 typeCheck: true取埗できないこずに泚意しおください。 *.vueファむルで機胜したす。これは、 tslintが拡匵子に䞍満を持っおいるためです appendTsSuffixTo 。 このデモプロゞェクトは、簡単な䟋を瀺しおいたす。

マヌク

ここにもう少しニュヌスがありたすか 🀙

誰かがcliを䜿甚しお.vueファむルをリントしたい堎合は、 vue-tslintを詊すこずができたす。 完璧ではありたせんが、機胜したす...

@romandraganの゜リュヌションは珟圚ベストプラクティスではないこずに泚意しおください以前はそうであったかどうかは\.ts$ルヌルのuseにtslint-loaderを远加したす。 これで、 vue-loaderは䞀般にテンプレヌトファむルを凊理し、それぞれの拡匵機胜のWebpack構成にさたざたなセクションを委任したす。 vue-loaderのプラグむンコンポヌネントは、 \.ts$のルヌルを抜出し、これらのルヌルを介しお<script lang="ts">ブロックをストリヌミングしたす。

Vue CLIを䜿甚しおいる堎合は、

config.module.rule('ts')
  .use('tslint-loader')
  .loader('tslint-loader');

vue.config.js chainWebpackオプションぞのステヌトメント。 䟋えば、

vue.config.js

module.exports = {
  chainWebpack: (config) => {
    config.devtool('source-map');
    config.module.rule('ts')
      .use('tslint-loader')
      .loader('tslint-loader');
  },
  pluginOptions: {
    apollo: {
      enableMocks: true,
      enableEngine: true
    }
  }
}

これは私のために働いた

const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')

module.exports = (baseConfig, env, defaultConfig) => {
  defaultConfig.resolve.extensions.push('.ts', '.tsx', '.vue', '.css', '.less', '.scss', '.sass', '.html')

  defaultConfig.module.rules.push( {
    test: /\.ts$/,
    exclude: /(node_modules)/,
    use: [
      {
        loader: 'ts-loader',
        options: {
          appendTsSuffixTo: [/\.vue$/]
          // transpileOnly: true,
        }
      },
      {
        loader: 'tslint-loader',
        options: {
          configFile: 'tslint.json',
          emitErrors: true,
        }
      }
    ]
  })

  defaultConfig.module.rules.push({ test: /\.less$/, loaders: [ 'style-loader', 'css-loader', 'less-loader' ] })
  defaultConfig.module.rules.push({ test: /\.scss$/, loaders: [ 'style-loader', 'css-loader', 'sass-loader' ] })
  defaultConfig.module.rules.push({ test: /\.styl$/, loader: 'style-loader!css-loader!stylus-loader' })

  defaultConfig.plugins.push(new ForkTsCheckerWebpackPlugin())

  return defaultConfig
}

4379によるず、Vue固有の回避策はTSLintコアに到達できなくなりたす。

ここでの議論や具䜓的な提案が䞍足しおおり、ESLint4534を支持するTSLintの非掚奚が近づいおいるこずを考慮しお、ハりスキヌピングの目的でこの問題ず関連するVue固有のレポヌトを閉じたす。

これがtypescript-eslintでただ問題である堎合は、そこで問題を提出するこずをお勧めしたす。 頑匵っおください

./node_modules/.bin/wotan'src / * / .vue '-f verbose

クラッシュ

Error: ENOENT: no such file or directory, open '/home/andrew/PycharmProjects/djangochat/fe/src/components/App.vue.ts'
    at Object.openSync (fs.js:451:3)
    at detectEncoding (/home/andrew/PycharmProjects/djangochat/fe/node_modules/tslint/lib/rules/encodingRule.js:67:17)
    at walk (/home/andrew/PycharmProjects/djangochat/fe/node_modules/tslint/lib/rules/encodingRule.js:49:20)
    at Rule.AbstractRule.applyWithFunction (/home/andrew/PycharmProjects/djangochat/fe/node_modules/tslint/lib/language/rule/abstractRule.js:39:9)
    at Rule.apply (/home/andrew/PycharmProjects/djangochat/fe/node_modules/tslint/lib/rules/encodingRule.js:33:21)
    at R.apply (/home/andrew/PycharmProjects/djangochat/fe/node_modules/@fimbul/bifrost/src/index.js:30:40)
    at Linter.applyRules (/home/andrew/PycharmProjects/djangochat/fe/node_modules/@fimbul/wotan/src/linter.js:209:31)
    at Linter.getFindings (/home/andrew/PycharmProjects/djangochat/fe/node_modules/@fimbul/wotan/src/linter.js:125:25)
    at Runner.lintFiles (/home/andrew/PycharmProjects/djangochat/fe/node_modules/@fimbul/wotan/src/runner.js:159:43)
    at lintFiles.next (<anonymous>) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '/home/andrew/PycharmProjects/djangochat/fe/src/components/App.vue.ts'
}

@akoidan tslintではなく、wotanプロゞェクトでそのような問題を提起しおください

このペヌゞは圹に立ちたしたか
0 / 5 - 0 評䟡