fix: return original source when no inline styles are found#7
Open
arkthur wants to merge 6 commits intochristophbuehler:mainfrom
Open
fix: return original source when no inline styles are found#7arkthur wants to merge 6 commits intochristophbuehler:mainfrom
arkthur wants to merge 6 commits intochristophbuehler:mainfrom
Conversation
When a .ts file has no inline `styles`, `node.root().nodes` is empty and the forEach loop never calls `builder`, producing an empty string. Stylelint then writes that empty string back to disk, erasing the file. The VS Code stylelint extension is particularly affected because it replaces the full document content unconditionally, whereas the CLI accidentally skips the write when `result.code` is falsy. Fix: when there are no nodes, call `builder` with the original source so the file is preserved unchanged. Closes christophbuehler#6
The Root document is created by parsing an empty string, so root.source.input.css is always "". The original TypeScript source was never stored on the PostCSS tree, making it impossible for the stringifier to return it unchanged when no inline styles are present. Store it in document.raws.angularSource so the stringifier can use it.
Previously the fallback used node.source?.input?.css which is always "" because the Root is created by parsing an empty string in the parser. Now reads root.raws.angularSource (set by the parser) to return the original TypeScript file content unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When stylelint runs with
--fixon an Angular.tsfile that usesstyleUrl(external stylesheet) — or any plain.tsfile with no inline styles — the stringifier produces an empty string. Stylelint writes that empty string back to disk, erasing the entire file.Reported in issue #6.
Root cause
Parser: the PostCSS document root is created by
postcss.parse("", { from })— an empty string. The original TypeScript source is never stored anywhere on the PostCSS tree.Stringifier: iterates over
node.root().nodes. When there are no inline styles,nodesis an empty array,forEachnever executes,builderis never called, and the output is"". The attempted fallbacknode.source?.input?.cssalso returns""because the root was parsed from an empty string, not the original source.Stylelint then writes
""back to disk.Fix
Parser — store the original TypeScript source in
document.raws.angularSourcebefore returning:+document.raws.angularSource = sourceString;Stringifier — when
nodesis empty, readroot.raws.angularSourceand return the original source unchanged: