Giter Club home page Giter Club logo

prettier-plugin-curly's Introduction

prettier-plugin-curly

Prettier plugin to enforce consistent brace style for all control statements. ๐ŸฅŒ

All Contributors: 5 ๐Ÿ‘ช Codecov Test Coverage Contributor Covenant License: MIT Sponsor: On GitHub Style: Prettier TypeScript: Strict npm package version Contributor Covenant

Usage

First install this package as a dev dependency in your package manager of choice:

npm i prettier-plugin-curly -D

You'll then be able to list it as a Prettier plugin in your Prettier config:

{
	"plugins": ["prettier-plugin-curly"]
}

As a result, Prettier will add {} curly brackets to control flow statements such as for, if, and while:

- if (abc) def;
+ if (abc) {
+   def;
+ }

But Why?

Prettier generally does not modify the structure of code: which includes not enforcing curly brackets to match ESLint's curly rule. However, enforcing curly generally does not modify code runtime behavior, and is often desirable for code consistency and to avoid accidental bugs. This plugin enforces the equivalent of curly's all option at the Prettier level.

See The Blurry Line Between Formatting and Style for more details.

Development

See .github/CONTRIBUTING.md, then .github/DEVELOPMENT.md. Thanks! ๐Ÿ’–

Contributors

Dan Vanderkam
Dan Vanderkam

๐Ÿ›
Holger Jeromin
Holger Jeromin

๐Ÿ›
Josh Goldberg
Josh Goldberg

๐Ÿ”ง ๐Ÿšง ๐Ÿ’ป ๐Ÿš‡ ๐Ÿค” ๐Ÿ›
Navin Moorthy
Navin Moorthy

๐Ÿ›
ส€แด€ส
ส€แด€ส

๐Ÿ“– ๐Ÿ’ป ๐Ÿ›

๐Ÿ’™ This package is based on @JoshuaKGoldberg's create-typescript-app.

prettier-plugin-curly's People

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

Watchers

 avatar  avatar  avatar

prettier-plugin-curly's Issues

๐Ÿ› Bug: cannot parse code includes syntax errors

Bug Report Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have pulled the latest main branch of the repository.
  • I have searched for related issues and found none that matched my issue.

Expected

Should parse and format

const a =1
const a=1

Actual

Cannot parse and breaks formatting

Additional Info

We should use prettier's parser. import from prettier/parser-babel

PR later

๐Ÿ› Bug: Replaces ; (empty statement) with {} (block statement)

Bug Report Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have pulled the latest main branch of the repository.
  • I have searched for related issues and found none that matched my issue.

Expected

Given code like:

for (; i < lines.length && lines[i] !== until; i++);

I'd expect no changes, as the ; indicates the for's body is an intentionally empty statement

Actual

It gets changed to:

for (; i < lines.length && lines[i] !== until; i++) {}

Additional Info

Seen when verifying #309 works on @danvk's https://github.com/danvk/literate-ts/blob/5d04101f895285d838cb4cdccadc8941c86b0771/src/ts-checker.ts#L85 as referenced by #284.

Thanks again @danvk for using the plugin & filing the issue! ๐Ÿ˜„

๐Ÿ› Bug: prettier-plugin-curly crashes on nested if statements

Bug Report Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have pulled the latest main branch of the repository.
  • I have searched for related issues and found none that matched my issue.

Expected

I expected to be able to run pnpm format with prettier-plugin-curly on literate-ts.

Actual

$ npx prettier src/ts-checker.ts
src/ts-checker.ts
[error] src/ts-checker.ts: SyntaxError: ',' expected. (85:5)
[error]   83 |       const start = m.index + 1;
[error]   84 |       const end = start + m[1].length;
[error] > 85 |       errors.push({line: lastCodeLine, start, end, message});
[error]      |     ^
[error]   86 |     }
[error]   87 |   });
[error]   88 |   return errors;

Additional Info

Here's the file in question, no syntax errors in sight:
https://github.com/danvk/literate-ts/blob/5d04101f895285d838cb4cdccadc8941c86b0771/src/ts-checker.ts#L85

This was quite confusing because there's nothing in the error message indicating that the error is coming from prettier-plugin-curly, rather than prettier itself.

๐Ÿ› Bug: comments are removed on prettier run

Bug Report Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have pulled the latest main branch of the repository.
  • I have searched for related issues and found none that matched my issue.

Expected

"prettier-plugin-curly": "0.1.3":

This code...

if(document.body)
  //Important comment which will be lost!
  document.body.append('')

should be transformed to

if(document.body) {
  //Important comment which will be lost!
  document.body.append('')
}

Actual

I get

if(document.body) {
  document.body.append('')
}

Additional Info

Same with block comments: /* comment */

๐Ÿ›  Tooling: Migrate to create-typescript-app@1

Bug Report Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have pulled the latest main branch of the repository.
  • I have searched for related issues and found none that matched my issue.

Overview

Tracking JoshuaKGoldberg/create-typescript-app#734 in this repository.

Additional Info

npx create-typescript-app --mode migrate --author JoshuaKGoldberg --description "Prettier plugin to enforce consistent brace style for all control statements. ๐Ÿงน" --email [email protected] --funding JoshuaKGoldberg --owner JoshuaKGoldberg --repository prettier-plugin-curly --title prettier-plugin-curly

๐Ÿ› Bug: else branch with function call is not curlified

Bug Report Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have pulled the latest main branch of the repository.
  • I have searched for related issues and found none that matched my issue.

Expected

Input:

let foo = new Set<string>();
if ('bar') {
} else
    foo.add('I am at the wrong position');

expected output

let foo = new Set<string>();
if ('bar') {
} else {
   foo.add('I am at the wrong position');
}

Actual

let foo = new Set<string>();
if ('bar') {
} else foo.add('I am at the wrong position');

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.