Code Monkey home page Code Monkey logo

tslint-plugin-prettier's Introduction

tslint-plugin-prettier

npm build coverage

Runs Prettier as a TSLint rule and reports differences as individual TSLint issues.

Changelog

Sample

a();;;
    ~~
;;;
~~~ [Delete `;;⏎;;;`]
var foo = ''
          ~~ [Replace `''` with `"";⏎`]
var foo= "";
       ~ [Insert `·`]

Install

# using npm
npm install --save-dev tslint-plugin-prettier prettier

# using yarn
yarn add --dev tslint-plugin-prettier prettier

(require prettier@^1.9.0)

Usage

(tslint.json)

for tslint@^5.2.0

{
  "rulesDirectory": ["tslint-plugin-prettier"],
  "rules": {
    "prettier": true
  }
}

for tslint@^5.0.0

{
  "extends": ["tslint-plugin-prettier"],
  "rules": {
    "prettier": true
  }
}

NOTE: To use this plugin, it'd better to also use tslint-config-prettier to disable all prettier-related rules, so as to avoid conflicts between existed rules.

Options

  • If there is no option provided, it'll try to load config file and/or .editorconfig if possible, uses Prettier's default option if not found.

    {
      "extends": ["tslint-plugin-prettier"],
      "rules": {
        "prettier": true
      }
    }

    If you don't want to load .editorconfig, disable it in the third argument.

    {
      "extends": ["tslint-plugin-prettier"],
      "rules": {
        "prettier": [true, null, { "editorconfig": false }]
      }
    }
  • If you'd like to specify which config file to use, just put its path (relative to process.cwd()) in the second argument, the following example shows how to load the config file from <cwd>/configs/.prettierrc:

    {
      "extends": ["tslint-plugin-prettier"],
      "rules": {
        "prettier": [true, "configs/.prettierrc"]
      }
    }
  • If you'd like to specify options manually, just put Prettier Options in the second argument, for example:

    {
      "extends": ["tslint-plugin-prettier"],
      "rules": {
        "prettier": [true, { "singleQuote": true }]
      }
    }

Ignoring files

  • It will respect your .prettierignore file in your project root ( process.cwd() ) but if you would like to use a different file you can provide it in the third argument, for example:

    {
      "extends": ["tslint-plugin-prettier"],
      "rules": {
        "prettier": [true, null, { "ignorePath": "otherDirectory/.prettierignore" }]
      }
    }
    

Development

# lint
yarn run lint

# build
yarn run build

# test
yarn run test

Related

License

MIT © Ika

tslint-plugin-prettier's People

Contributors

ammarcodes avatar azz avatar dependabot-preview[bot] avatar dependabot[bot] avatar ikatyang avatar renovate-bot avatar renovate[bot] avatar tobli avatar tonyhallett avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tslint-plugin-prettier's Issues

"tslint.configFile" absolute file path required with VSCode

I'm not sure if this issue is more related to VSCode, tslint or this plugin, but considering that tslint works as expected except for the prettier part I'm starting by posting an issue here.

My project folder structure looks similar to this:

<root>
├── backend
└── frontend
    ├── tslint.js
    └── .prettierrc

In VSCode, I open the project root. Now prettier won't do anything unless I specify an absolute path to the tslint.js file for "tslint.configFile" config option in VSCode.

OS: macOS 10.14.6
VSCode 1.37
[email protected]
[email protected]

tslint.js:

module.exports = {
    extends: [
        'tslint-plugin-prettier',
        'tslint-config-prettier',
    ],
    rules: {
        'prettier': true
    }
}

Update Dependencies (Renovate Bot)

This master issue contains a list of Renovate updates and their statuses.

Pending Approval

These branches will be created by Renovate only once you click their checkbox below.

  • chore(deps): update dependency @types/node to v4.9.4
  • chore(deps): update dependency har-validator to v5.1.3
  • chore(deps): update dependency tslint-plugin-prettier to v2.3.0
  • chore(deps): update dependency @types/node to v13
  • chore(deps): update dependency nyc to v15
  • chore(deps): update dependency standard-version to v7

Closed/Ignored

These updates were closed unmerged and will not be recreated unless you click a checkbox below.


Advanced
  • Check this box to trigger a request for Renovate to run again on this repository

when using space-before-function-paren , getting conflicted rules

Description:

When using space-before-function-paren , get conflicting rules.

My react component:
image
Got error:
image

After i remove the space between function name ShopShow and parenthesis (),it got :
image

My configuration files:

{
    "defaultSeverity": "error",
    "extends": [
      "tslint:recommended",
      "tslint-eslint-rules",
      "tslint-react",
      "tslint-plugin-prettier",
      "tslint-config-prettier"
    ],
    "jsRules": {},
    "rules": {
      "trailing-comma": [true, {"multiline": "never", "singleline": "never"}],
      "arrow-parens": false,
      "space-before-function-paren": [true, "always"],
			"quotemark": false,
      "no-console": [false],
      "ter-indent": [true, 2, {"SwitchCase": true}],
      "prettier": [true, {"semi": false, "singleQuote":true}],
      },
      // ...
}

I read the documentation, and using tslint-config-prettier at the last line in extends.

Expected

It could using space-before-function-paren rules.

Thank you ,guys.

Not support .editorconfig file

This plugin does not apply .editorconfig config if I set different rules from prettier default.

Consider to support .editorconfig file.

readme.md seems wrong about configuration

The readme shows following configuration. It's in fact the opposite. Shall I open a pull request to fix that?

for tslint@^5.2.0

{
  "rulesDirectory": ["tslint-plugin-prettier"],
  "rules": {
    "prettier": true
  }
}

for tslint@^5.0.0

{
  "extends": ["tslint-plugin-prettier"],
  "rules": {
    "prettier": true
  }
}

Try to resolve config path relative to tslint.json if config file not found.

Say I have a project that contains both beckend service and web files and is organized like this:

├── backend
│   ├── api
│   ├── common
│   ├── hubpkg
│   ├── idgen
│   ├── jsonrpc
│   └── ws
└── web
    ├── build
    ├── config
    ├── images.d.ts
    ├── node_modules
    ├── package.json
    ├── src
    ├── tslint.json
    └── .prettierrc

And here is my tslint.json:

{
    ...
    "rules": {
        "prettier": [true, ".prettierrc"],
        ...
    }
}

And I use vscode to write both backend and web codes so my favorite working directory is the project root directory and then tslint will report error for failure to find the .prettierrc file (because my cwd is project root directory and tslint can't find file /path_to_project/.prettierrc). Also I can't change the config path to web/.prettierrc because some team members(frontend developers) and ci for web part both prefer to work under web directory.

So how about supporting to resolve the config file relative to tslint.json if fail to find the config file?

support prettier v2

I get these warnings with [email protected] and [email protected] installed.

npm WARN [email protected] requires a peer of prettier@^1.9.0 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of tslint@^5.0.0 but none is installed. You must install peer dependencies yourself.

I'm also getting these on every line in VSCode after updating prettier

image

Not sure if it's this plugin that's causing it.

Not report the error when prettier fails.

The code below will be an error in VSCode editor by prettier semi: false rule:

import foo from 'src/demo.ts';
             ~~ [tslint] Delete `;` (prettier)

But the command tslint -c tslint.yaml src/**/*.ts not report the prettier error, and done successfully.

tslint.yaml

extends:
  - 'tslint:latest'
  - tslint-eslint-rules
  - tslint-config-prettier
defaultSeverity: error
rulesDirectory: tslint-plugin-prettier
rules:
  prettier: true
  ....

.prettierrc.yml

printWidth: 100
useTabs: false
tabWidth: 2
semi: false
singleQuote: true
trailingComma: 'all'
bracketSpacing: true
arrowParens: 'avoid'

Respect prettierignore

I added a file to prettierignore and then manually had to disable the prettier tslint rule. Is this expected?

Resolve configuration using current directory

In prettierRule.ts:38, tslint-plugin-prettier tries to resolve a prettier configuration file based on the examined file's path.

Some editor integrations do fancy things such as running tslint on temp files (in order to catch lint errors before file save), so this breaks that. Specifically I've hit that with Vim+ALE: dense-analysis/ale#956.

My suggestion would be to try to fall back to the system's current directory (process.cwd()) after trying to resolve based on file path.
This would allow the editor integration to fix this by cding into the project directory.

Use prettier-linter-helpers

eslint-plugin-prettier v3 removed the generateDifferences and showInvisibles functions as they are now provided by the prettier-linter-helpers package. Depend upon that package instead of eslint-plugin-prettier.

Here's the PR in stylelint-prettier that changed from depending on eslint-plugin-prettier to prettier-linter-helpers to demonstrate the changes required.

Prettier Parsing Error While Typing

I wanted to preface this by saying I am unsure if the bug is happening here in this package or in Microsoft/vscode-tslint. I filed issue #405 there as well.

Steps to Reproduce

  1. Download this sample project that I put together to reproduce the issue.
  2. Run npm install
  3. Open src/index.ts and start typing anything.
  4. Result: See error printed to tslint output, ⚠️ the bottom status bar next to TSLint. It then never recovers and the ⚠️ stays there forever

Console Output

[Warn  - 2:54:44 PM] The 'prettier' rule threw an error in '/tslint-prettier-vscode-bug/src/index.ts':
SyntaxError: Expression expected. (3:28)
  1 | console.log('👍');
  2 | 
> 3 | const x = "does this work"?
    |                            ^
  4 | 
    at e (/tslint-prettier-vscode-bug/node_modules/prettier/parser-typescript.js:1:422)
    at Object.parse (/tslint-prettier-vscode-bug/node_modules/prettier/parser-typescript.js:1:2010094)
    at Object.parse$2 [as parse] /tslint-prettier-vscode-bug/node_modules/prettier/index.js:7138:19)
    at coreFormat (/Users/morgism/Developer/nodejs/tslint-prettier-vscode-bug/node_modules/prettier/index.js:10398:23)
    at format (/tslint-prettier-vscode-bug/node_modules/prettier/index.js:10570:16)
    at formatWithCursor (/tslint-prettier-vscode-bug/node_modules/prettier/index.js:10582:12)
    at /Users/morgism/Developer/nodejs/tslint-prettier-vscode-bug/node_modules/prettier/index.js:34924:15
    at Object.format (/tslint-prettier-vscode-bug/node_modules/prettier/index.js:34943:12)
    at Walker.walk /tslint-prettier-vscode-bug/node_modules/tslint-plugin-prettier/rules/prettierRule.js:54:34)
    at Rule.AbstractRule.applyWithWalker (/tslint-prettier-vscode-bug/node_modules/tslint/lib/language/rule/abstractRule.js:31:16)

Video

A .gif demoing this can be found here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.