Visitar URL original
Return the processed CSS from NoWorkResult#toString() by giaBaoJS · Pull Request #2155 · postcss/postcss · GitHub
Skip to content

Return the processed CSS from NoWorkResult#toString() - #2155

Open
giaBaoJS wants to merge 1 commit into
postcss:mainfrom
giaBaoJS:fix-no-work-result-to-string
Open

giaBaoJS wants to merge 1 commit into
postcss:mainfrom
giaBaoJS:fix-no-work-result-to-string

Conversation

@giaBaoJS

@giaBaoJS giaBaoJS commented Sep 13, 2026 •

Copy link
Copy Markdown

When Processor#process() gets no plugins it returns a NoWorkResult. Its
constructor runs the map generator and rewrites this.result.css: it appends a
generated sourceMappingURL annotation, or strips a stale one left in the
input. toString() skipped all of that and returned the raw input string, so
String(result) and result.css disagreed whenever the constructor had
changed anything.

lib/no-work-result.d.ts declares NoWorkResult_ implements LazyResult<Root>,
and LazyResult#toString() is documented as "Alias for the LazyResult#css
property. lazy + '' === lazy.css". LazyResult#toString() returns
this.css; NoWorkResult had css and content reading this.result.css
while toString() read the untouched this._css.

Reproduction:

const postcss = require('postcss')

// 1. a generated inline map is dropped
let a = postcss([]).process('.foo { color: red }\n', {
  from: 'foo.css',
  map: true
})
a.css      // '.foo { color: red }\n\n/*# sourceMappingURL=data:application/json;base64,... */'
String(a)  // '.foo { color: red }\n'

// 2. a stale annotation is leaked
let b = postcss([]).process(
  '.foo { color: red }\n\n/*# sourceMappingURL=old.css.map */\n',
  { from: 'foo.css', map: false }
)
b.css      // '.foo { color: red }\n'
String(b)  // the input, annotation and all

With one plugin registered the same calls take the LazyResult path and both
values agree.

The fix makes toString() return this.result.css, matching css, content
and LazyResult#toString(). This is residue from #1909, which converged the
NoWorkResult constructor with LazyResult but left toString() reading the
pre-map string.

The three new tests sit next to the no work result matches lazy result...
tests that #1909 added; those compare .css, these compare toString(). They
cover the two branches of the constructor separately: a generated inline map, a
generated external map (which also sets result.map), and a stale inline map
being stripped. The existing stringifies css test already asserted
`${result}` === result.css, but only on input the constructor never
touches, so it passed throughout.

Reverting the one-line change turns all three red; a patch that fixes only the
map-generating branch, only the annotation-stripping branch, or that trims the
result is caught too. Full pnpm test is green, 704 tests (701 before), c8
line coverage still at 100%.

Summary by CodeRabbit

  • Bug Fixes

    • Stringifying a no-work result now consistently returns the processed CSS, including source map transformations, instead of the original source.
  • Tests

    • Added coverage for string conversion with enabled maps, non-inlined maps, and inline source maps.

The constructor rewrites `result.css` to append a generated source map
annotation or to strip a stale one, but `toString()` returned the
untouched input, so `String(result)` disagreed with `result.css`.
@coderabbitai

coderabbitai Bot commented Sep 13, 2026 •

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 661ce52a-5808-4868-af0c-73909b7518cc

📥 Commits

Reviewing files that changed from the base of the PR and between 1547198 and 0e0e261.

📒 Files selected for processing (2)
  • lib/no-work-result.js
  • test/no-work-result.test.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

NoWorkResult.toString() now returns processed CSS from result.css. Tests verify this behavior with enabled, external, and inline source maps.

Changes

NoWorkResult stringification

Layer / File(s) Summary
Return processed CSS and validate source map cases
lib/no-work-result.js, test/no-work-result.test.ts
toString() returns result.css instead of the original CSS input. Tests verify matching string and css values for multiple map configurations.

Priority: ⬇️ Low

Estimated code review effort: 1 (Trivial) | ~5 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 0e0e2

The change aligns NoWorkResult stringification with its processed CSS output, with source-map cases covered by tests.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: NoWorkResult#toString() now returns processed CSS.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ai ai added this to the 9.0 milestone Sep 15, 2026
@ai

ai commented Sep 15, 2026

Copy link
Copy Markdown
Member

Good idea. But I prefer to keep to 9.0 release soon since it could create some breaking changes.

@giaBaoJS

Copy link
Copy Markdown
Author

Understood, and 9.0 is the right place for it. Returning the processed CSS changes what a lazy result prints once map options are involved, so anyone relying on the current output would see a difference.

The branch still merges cleanly on main and CI is green, so it is ready whenever you pick up 9.0. I did not see a 9.0 branch to retarget at, so I will leave it open against main unless you would rather I moved it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants