blackducksoftware / ohcount

The Ohloh source code line counter
https://github.com/blackducksoftware/ohcount
GNU General Public License v2.0
261 stars 69 forks source link

Add support for TypeScript language #56

Closed dlech closed 6 years ago

dlech commented 6 years ago

http://www.typescriptlang.org/

TypeScript is basically a derivitave of JavaScript, so using the existing JavaScript parser to parse TypeScript files.

dlech commented 6 years ago

Related: https://www.openhub.net/topics/14028

notalex commented 6 years ago

Thanks @dlech for your contribution. There are a few more changes required for this patch to be complete.

.js files can also contain typescript, see here. This was done to help existing javascript projects migrate easily and also to fix certain Visual Studio use cases. See here and here for more details. The popular project https://github.com/ant-design/ant-design uses typescript within .js files extensively.

Hence ohcount should be able to detect typescript code within .js files. If a .js file contains javascript and typescript both, we can count it entirely as typescript. If a .js file contains no typescript elements, we can count it entirely as javascript.

To do this one needs to add some disambiguate code to /src/detector.c. The disambiguate logic will iterate over each line and check for typescript constructs. For example see this and this. See this PR for how to add disambiguate functions to gperf files.

If you need any help in completing this patch, please do let me know.

dlech commented 6 years ago

I read all the links you gave, but the conclusion I came to is that you can pass javascript files to the typescript compiler. There is no mixing of typescript and javascript in the same file and there is no typescript in a file with a .js extension (I don't see it anywhere in the ant-design project).

notalex commented 6 years ago

I checked the ant-design project again. You are right, there is no instance of typescript in .js files.

The ant-design project has a lot of files with .tsx extension. If .tsx is commonly used, it would be helpful for us to detect it as typescript.

dlech commented 6 years ago

I don't know how common JSX is (I've never used it). I can add another commit that treats .jsx files as JavaScript and .tsx files as TypeScript. If you want to add a new JSX language that is the "XML-like syntax extension to ECMAScript" found inline in these files, then I think we should save that for another PR.

Personally, I would be OK with just calling lines of JSX JavaScript (or TypeScript) instead of calling it a new language since it is an "extension" of the language.

notalex commented 6 years ago

Ohcount with this patch detects the ant-design project as primarily javascript, which is incorrect. We just need a small change in this PR to detect .tsx as Typescript. No need to add anything to the parser. I will add JSX support in another PR since that is not the scope of work here.

dlech commented 6 years ago

I've added a commit for the .tsx file extension.

notalex commented 6 years ago

Thank you @dlech for your contribution.