tslint-eslint-rules icon indicating copy to clipboard operation
tslint-eslint-rules copied to clipboard

Add unicode-bom

Open jez9999 opened this issue 7 years ago • 10 comments

ESLint has a unicode-bom rule. It should exist for tslint too.

jez9999 avatar Sep 16 '17 09:09 jez9999

I've been investigating and it looks like each rule is passed a SourceFile:

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[]

Unfortunately, SourceFile's .text property has already had the BOM removed by the time it gets to the rule. Can anyone suggest a way I can get at the actual raw file? Does tslint need modifying to pass the raw file contents into the rule as well?

jez9999 avatar Oct 18 '17 23:10 jez9999

If the source file has the name of the file perhaps you can read it yourself instead?

jmlopez-rod avatar Oct 19 '17 01:10 jmlopez-rod

Very inefficient, isn't it? It means reading the file twice.

jez9999 avatar Oct 19 '17 09:10 jez9999

Isn't there a way to just read a certain amount of bytes from a file?

https://stackoverflow.com/questions/30096691/read-a-file-one-character-at-a-time-in-node-js

In this case I think you only care about the first byte which was already removed. Another possible way would be to see if typescript can add a flag to the source file that tells us if it has a BOM or not... similar to the parser of eslint:

sourceCode.hasBOM

https://github.com/eslint/eslint/blob/master/lib/rules/unicode-bom.js

jmlopez-rod avatar Oct 19 '17 15:10 jmlopez-rod

I think you'd have to modify the code of Typescript or TSLint itself to add such a flag. And I don't like my chances of getting a pull request accepted for that. :-(

jez9999 avatar Oct 19 '17 16:10 jez9999

Perhaps you could open up an issue and see if they would add this flag for you, in the past they had issues with this it seems: https://github.com/Microsoft/TypeScript/issues/8087

If this is a no go and this rule is very important to you you can try to read that first byte to determine if a warning should be thrown or not. Good luck.

jmlopez-rod avatar Oct 19 '17 16:10 jmlopez-rod

I've submitted an issue, for what it's worth:

https://github.com/Microsoft/TypeScript/issues/19344

jez9999 avatar Oct 19 '17 16:10 jez9999

Well, seems like you'll have to do https://github.com/palantir/tslint/blob/master/src/rules/encodingRule.ts, but on second though, doesn't that rule do what you need to the eslint unicode-bom? Maybe we'll just need to document it and point it out. Let me know if you agree and feel free to make a PR.

jmlopez-rod avatar Oct 19 '17 23:10 jmlopez-rod

Interesting, that rule is rather misnamed. It actually enforces what it calls "UTF-8 encoding", which is UTF-8 encoding without the BOM. That happens to do what I want but it's not quote the same as the unicode-bom rule which could be configured to either require or disallow the BOM.

jez9999 avatar Oct 19 '17 23:10 jez9999

FWIW, there's a duplicate issue for this in the TSLint repo: https://github.com/palantir/tslint/issues/3331

reduckted avatar Nov 03 '17 07:11 reduckted