Having enabled eslint in my project, it's now reporting lots of warnings Unexpected Unicode BOM (Byte Order Mark) unicode-bom in my typescript and javascript files. Why does VS include BOMs in my files and is it actually a problem?
When I open these files in the VS binary editor, I see that there is indeed a BOM at the start of the file, for example
00000000 EF BB BF 69 6D 70 6F 72 74 20 52 65 61 63 74 20 ...import React
Looking at some other file types (e.g. C# code) in the binary editor shows that they start with a BOM too. I'm using VS 2022 however Is there any case in which removing a BOM from a Js file created in Visual Studio 2008 will cause problems? suggests that VS has been doing this for many years and the answer on that question suggests that it's probably best to remove them.
I understand that byte order marks are used in text encodings which use more than one byte per character, to indicate whether the bytes in each character are presented most significant byte first or least significant byte first (big endian or little endian). However my files all use only one byte per character, so the BOM seems surplus to requirements. So I guess I have two questions
I know I could just disable this rule in eslint configuration but I don't like disabling code analysis rules without understanding why it's OK to do so.
A BOM in source code files is helpful if the source code file contains unicode characters, e.g. if you have non-english text or characters somewhere in them. Otherwise it is irrelevant. As most modern tools (text editors, tools, compilers, version control, etc.) correctly handle the BOM or at least ignore it, it should be fine to leave it there. You can use your .editorconfig file to specify which files should have a BOM and which don't, in case you want to make a distinction:
[*.{js}]
charset = utf-8
indent_style = tab
tab_width = 4
[*.{cs}]
charset = utf-8-bom
indent_style = space
tab_width = 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With