tslint-eslint-rules
tslint-eslint-rules copied to clipboard
Add unicode-bom
ESLint has a unicode-bom
rule. It should exist for tslint too.
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?
If the source file has the name of the file perhaps you can read it yourself instead?
Very inefficient, isn't it? It means reading the file twice.
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
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. :-(
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.
I've submitted an issue, for what it's worth:
https://github.com/Microsoft/TypeScript/issues/19344
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.
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.
FWIW, there's a duplicate issue for this in the TSLint repo: https://github.com/palantir/tslint/issues/3331