Skip to content

[Request] Verification method to assure that code is formatted properly #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Paden opened this issue Jun 22, 2015 · 16 comments · Fixed by #24
Closed

[Request] Verification method to assure that code is formatted properly #15

Paden opened this issue Jun 22, 2015 · 16 comments · Fixed by #24
Assignees

Comments

@Paden
Copy link

Paden commented Jun 22, 2015

Something like:

$ tsfmt -verify <other options> <files>

The files would not be changed, only verified according to the specs.

@vyk2rr
Copy link

vyk2rr commented Jun 22, 2015

+1

5 similar comments
@gpfarris
Copy link

+1

@cjlpowers
Copy link

+1

@dvalys
Copy link

dvalys commented Jun 22, 2015

+1

@poncho1984
Copy link

+1

@slavik925
Copy link

+1

@vvakame
Copy link
Owner

vvakame commented Jun 23, 2015

when do you want to use that option?

@Paden
Copy link
Author

Paden commented Jun 23, 2015

Formatting helps our team check-in code that looks as if it were written by one person, which helps readability.

Being able to verify that these files have been formatted correctly on tools like Travis CI or TeamCity would help our team make sure that code being checked in is in fact formatted.

@vvakame
Copy link
Owner

vvakame commented Jun 28, 2015

@Paden release typescript-fromatter 0.4.0. try it out.

@myitcv
Copy link
Contributor

myitcv commented Aug 3, 2015

I'm seeing a problem with --verify in v0.4.2 (I've substituted \t where appropriate below to remove any ambiguity):

$ cat js/services/banana.ts
export class Banana {
\tconstructor() {
\t\tconsole.log("Test");
\t}
}
$ tsc --version
message TS6029: Version 1.6.0-dev.20150803
$ tsfmt --version
0.4.2
$ tsfmt --verify js/services/banana.ts
js/services/banana.ts is not formatted
$ tsfmt js/services/banana.ts | diff js/services/banana.ts -
<no output>
$ tsfmt js/services/banana.ts | tsfmt --stdin --verify
temp.ts is not formatted

I'm using a fairly standard tslint.json

Any ideas?

@vvakame vvakame self-assigned this Aug 3, 2015
@vvakame
Copy link
Owner

vvakame commented Aug 3, 2015

@myitcv I can't reproduce your issue. I got below result.

$ tsfmt banana.ts | diff banana.ts -
--- banana.ts   2015-08-03 21:11:00.000000000 +0900
+++ -   2015-08-03 21:11:25.000000000 +0900
@@ -2,4 +2,5 @@
    constructor() {
        console.log("Test");
    }
-}
\ No newline at end of file
+}
+

If you do not solve the problem, please make a new Issue.

@myitcv
Copy link
Contributor

myitcv commented Aug 3, 2015

Thanks for checking. Can I just ask, what platform are you working on? Linux/Mac/Windows?

@vvakame
Copy link
Owner

vvakame commented Aug 3, 2015

I'm using Mac OS X.

@myitcv
Copy link
Contributor

myitcv commented Aug 3, 2015

That's my fault, because I didn't accurately copy my failing example. In any case I think I've worked out what's going on.

Let me try again, showing input.ts:

export class Banana {
    constructor() {
        console.log("Test");
    }
}

Notice the blank line on the end (GitHub shows it in a strange way). Checking via hexdump:

$ hexdump -C input.ts
00000000  65 78 70 6f 72 74 20 63  6c 61 73 73 20 42 61 6e  |export class Ban|
00000010  61 6e 61 20 7b 0a 20 20  20 20 63 6f 6e 73 74 72  |ana {.    constr|
00000020  75 63 74 6f 72 28 29 20  7b 0a 20 20 20 20 20 20  |uctor() {.      |
00000030  20 20 63 6f 6e 73 6f 6c  65 2e 6c 6f 67 28 22 54  |  console.log("T|
00000040  65 73 74 22 29 3b 0a 20  20 20 20 7d 0a 7d 0a 0a  |est");.    }.}..|

This means that input.ts ends in two line feeds.

This code then gobbles both line feeds, and adds back only one. If the intention here is to ensure we only have one line feed at the end of the file, I'm good with that.

However, the problem arises when used in the mode tsfmt input.ts (i.e. where the formatted output is printed to stdout). In this situation another line feed is being added by console.log

(Indeed this is confirmed by the fact that if you run tsfmt -r input.ts the result does check correctly)

So I think all of our problems here are solved by replacing console.log with a process.stdout.write call (plus a couple of tweaks to add \n to messages)

Thoughts?

@myitcv
Copy link
Contributor

myitcv commented Aug 3, 2015

Worth noting that this console.log problem will cause issues for any editor that uses a tsfmt --stdin approach (then reading the results from stdout)

@myitcv
Copy link
Contributor

myitcv commented Aug 3, 2015

I've just raised #19 to flesh out a fix for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants