diff --git a/src/__tests__/sync-configured-options.mjs b/src/__tests__/sync-configured-options.mjs new file mode 100644 index 0000000..8bf2032 --- /dev/null +++ b/src/__tests__/sync-configured-options.mjs @@ -0,0 +1,25 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import parser from "../../dist/index.js"; + +for (const method of ["astSync", "transformSync", "processSync"]) { + test(`${method} respects configured updates and per-call overrides`, () => { + const processor = parser( + (root) => { + root.first.first.value = "new"; + }, + { updateSelector: true }, + ); + const rule = { selector: ".old" }; + processor[method](rule); + assert.equal(rule.selector, ".new"); + const preserved = { selector: ".old" }; + processor[method](preserved, { updateSelector: false }); + assert.equal(preserved.selector, ".old"); + const defaultRule = { selector: ".old" }; + parser((root) => { + root.first.first.value = "new"; + })[method](defaultRule); + assert.equal(defaultRule.selector, ".old"); + }); +} diff --git a/src/processor.js b/src/processor.js index 85f8f13..57a5853 100644 --- a/src/processor.js +++ b/src/processor.js @@ -83,7 +83,8 @@ export default class Processor { throw new Error("Selector processor returned a promise to a synchronous call."); } let string = undefined; - if (options.updateSelector && typeof rule !== "string") { + const merged = Object.assign({}, this.options, options); + if (merged.updateSelector && typeof rule !== "string") { string = root.toString(this._stringifyOptions(options)); rule.selector = string; }