zebzhao / indent.js

Pure code indentation for jsx, tsx, ts, js, html, css, less, scss.
https://zebzhao.github.io/indent.js/
MIT License
76 stars 8 forks source link

Incorrect TypeScript conversion #23

Open charlesr1971 opened 8 months ago

charlesr1971 commented 8 months ago
import { createSelector } from '@ngrx/store';
export const selectShoppingCartItems = createSelector(
(state) => state.shoppingCart,
(shoppingCart) => shoppingCart.items
);
export const selectTotalItems = createSelector{
selectShoppingCartItems,
(items) => items.length
};

This is converted to:

import { createSelector } from '@ngrx/store';
export const selectShoppingCartItems = createSelector(
    (state) => state.shoppingCart,
        (shoppingCart) => shoppingCart.items
);
export const selectTotalItems = createSelector{
    selectShoppingCartItems,
    (items) => items.length
};

And here is the solution:

{
      $languages: "js",
      $name: "=",
      $excludeIf: [HTML_TAG_RULES],
      $startPatterns: [/=/],
      $endPatterns: [/[,;\)\]}]/, NEW_LINE_REGEX]
},

To:

{
      $languages: "js",
      $name: "=",
      $excludeIf: [HTML_TAG_RULES],
      $startPatterns: [/=(?!>)/],
      $endPatterns: [/[,;\)\]}]/, NEW_LINE_REGEX]
},
charlesr1971 commented 8 months ago

And you need to add a new rule for typescript generics:

{
      $languages: "js",
      $name: "typescript-generics",
      $startPatterns: [/(?<!\w<)<[\w\s,]+(?=>)/i],
      $endPatterns: [/>/],
      $indent: true,
      $consumeEndMatch: true
},

This ensures that it will match the TypeScript Generic, below:

import { Subject } from 'rxjs';
const subject = new Subject()<string>;
const subscription = subject.subscribe(data => {
    console.log(data);
});
subject.next('Hello');
subject.next('World');
subscription.unsubscribe();

But won't match the less than operator, below:

var c,i,l,quitchars;
quitchars=['q','Q'];
charloop:while(c=getc()){
  for (i=0; i < quitchars.length; i++){
    if (c == quitchars[i]) break charloop;
  }
}