Currently, on CSS and JavaScript pages, templates, wikilinks, and other wiki markup are parsed, with subst: and ~~~~ markup being replaced in their entirety (T43155), causing oddities like T307691 (which was fixed by a bodge) and other similar oddities.
The MediaWiki parser appears blind to the content model that it is parsing; specific content models such as plaintext and JSON are explicitly excluded from parsing in config-schema.php because of this. (Neither plaintext nor JSON support comments anyways, but that is irrelevant.)
There should be a way for the parser to render only CSS/JS comments and the like on CSS/JS files and the like. This should be expandable to other content models provided by extensions like Scribunto and TemplateStyles as well.