Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postcss-tape: more improvements #1236

Merged
merged 1 commit into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/postcss-tape/dist/index.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/* node:coverage disable */
"use strict";var e=require("node:assert/strict"),s=require("node:fs/promises"),t=require("node:fs"),n=require("node:path"),o=require("postcss"),r=require("postcss-8.4"),i=require("node:test"),a=require("node:url");const noopPlugin=()=>({postcssPlugin:"noop-plugin",Rule(){}});async function fileContentsOrEmptyString(e){try{return await s.readFile(e,"utf8")}catch(e){return""}}function reduceInformationInCssSyntaxError(e){process.env.DEBUG||(delete e.source,e.input&&delete e.input.source,delete e.postcssNode)}noopPlugin.postcss=!0;const c={postcssPlugin:"declaration-cloner",Declaration(e){"to-clone"===e.prop&&e.cloneBefore({prop:"cloned"})}},p={postcssPlugin:"rule-cloner",prepare(){const e=new WeakSet;return{RuleExit(s){e.has(s)||"to-clone"===s.selector&&(e.add(s),s.cloneBefore({selector:"cloned"}))}}}},l={postcssPlugin:"at-rule-cloner",prepare(){const e=new WeakSet;return{AtRuleExit(s){if(!e.has(s))return"to-clone"===s.params?(e.add(s),void s.cloneBefore({params:"cloned"})):"to-clone"===s.name?(e.add(s),void s.cloneBefore({name:"cloned"})):void 0}}}};class PackageDescriptionError extends Error{constructor(e,s){super(e),this.name="PackageDescriptionError",this.stack=`${this.name}: ${this.message}\n at "${s}" (${a.pathToFileURL(n.resolve("package.json")).pathname})`}}exports.atRuleClonerPlugin=l,exports.declarationClonerPlugin=c,exports.postcssTape=function postcssTape(a,c){c=c??{},i("`postcss` flag is set on exported plugin creator",(()=>{e.strictEqual(a.postcss,!0)})),i("exported plugin creator is a function",(()=>{e.strictEqual(typeof a,"function")})),i("`postcssPlugin` is set on a plugin instance",(()=>{const s=a();e.ok(s.postcssPlugin),e.strictEqual(typeof s.postcssPlugin,"string")})),i("package.json",(async t=>{const n=await s.readFile("./package.json","utf-8"),o=JSON.parse(n);await t.test('includes "postcss-plugin" keyword',(()=>{e.ok(o.keywords?.includes("postcss-plugin"),new PackageDescriptionError('Missing "postcss-plugin" keyword in package.json',"keywords"))})),await t.test('name starts with "postcss-"',{skip:c?.skipPackageNameCheck},(()=>{let s=o.name;if(s.startsWith("@")){s=o.name.split("/").slice(1).join("/")}e.ok(s.startsWith("postcss-"),new PackageDescriptionError(`package name "${s}" does not start with "postcss-"`,"name"))})),await t.test("`postcss` is a peer dependency and not a direct dependency",{skip:"postcssTapeSelfTest"in a},(()=>{e.ok(Object.keys(Object(o.peerDependencies)).includes("postcss"),new PackageDescriptionError('"postcss" is not listed in "peerDependencies"',"peerDependencies")),e.ok(!Object.keys(Object(o.dependencies)).includes("postcss"),new PackageDescriptionError('"postcss" must not be listed in "dependencies"',"dependencies"))}))}));const p=a().postcssPlugin;return async c=>{await i(p,(async i=>{for(const p in c)await i.test(p,(async i=>{const l=c[p];l.before&&await l.before();const u=n.join(".","test",...p.split(":")[0].split(n.posix.sep)),d=n.join(".","test",...p.replace(/:/g,".").split(n.posix.sep)),g="css";let w=`${u}.${g}`,f=`${d}.expect.${g}`,m=`${d}.result.${g}`;l.source&&(w=n.join(".","test",l.source)),l.expect&&(f=n.join(".","test",l.expect)),l.result&&(m=n.join(".","test",l.result));const k=l.plugins??[a(l.options)],y=await fileContentsOrEmptyString(w),E=await fileContentsOrEmptyString(f);let h,x=!1;try{h=await o(k).process(y,{from:w,to:m,map:{inline:!1,annotation:!1}})}catch(s){if(reduceInformationInCssSyntaxError(s),x=!0,l.exception&&l.exception.test(s.message))return;e.ifError(s)}e.notEqual(!x,l.exception,"expected an exception but got none");const S=h.css.toString();{const e=[s.writeFile(m,S,"utf8")];process.env.REWRITE_EXPECTS&&e.push(s.writeFile(f,S,"utf8")),await Promise.all(e)}E||e.ok(t.existsSync(f),`Missing expect file: "${f}"`),await i.test("has expected output",(()=>{e.strictEqual(S,E),e.deepStrictEqual(h.warnings().length,l.warnings??0,"Unexpected number warnings")})),await i.test("sourcemaps",(async()=>{e.ok(!h.map.toJSON().sources.includes("<no source>"),'Sourcemap is broken. This is most likely a newly created PostCSS AST Node without a value for "source". See: https://github.com/postcss/postcss/blob/main/docs/guidelines/plugin.md#24-set-nodesource-for-new-nodes')})),l.after&&await l.after(),await i.test("output is parsable with PostCSS",(async()=>{const s=await fileContentsOrEmptyString(m),t=await o([noopPlugin()]).process(s,{from:m,to:m,map:{inline:!1,annotation:!1}});e.deepStrictEqual(t.warnings(),[],"Unexpected warnings on second pass")})),await i.test("The oldest and current PostCSS version produce the same result",{skip:o([noopPlugin()]).version===r([noopPlugin()]).version},(async()=>{const s=await r(k).process(y,{from:w,to:m,map:{inline:!1,annotation:!1}});e.strictEqual(s.css.toString(),S)}))}))}))}},exports.ruleClonerPlugin=p;
"use strict";var e=require("node:assert/strict"),s=require("node:fs/promises"),t=require("node:fs"),n=require("node:path"),o=require("postcss"),r=require("postcss-8.4"),i=require("node:test"),a=require("node:url");const noopPlugin=()=>({postcssPlugin:"noop-plugin",Rule(){}});async function fileContentsOrEmptyString(e){try{return await s.readFile(e,"utf8")}catch(e){return""}}function reduceInformationInCssSyntaxError(e){process.env.DEBUG||(delete e.source,e.input&&delete e.input.source,delete e.postcssNode)}noopPlugin.postcss=!0;const c={postcssPlugin:"declaration-cloner",Declaration(e){"to-clone"===e.prop&&e.cloneBefore({prop:"cloned"})}},p={postcssPlugin:"rule-cloner",prepare(){const e=new WeakSet;return{RuleExit(s){e.has(s)||"to-clone"===s.selector&&(e.add(s),s.cloneBefore({selector:"cloned"}))}}}},l={postcssPlugin:"at-rule-cloner",prepare(){const e=new WeakSet;return{AtRuleExit(s){if(!e.has(s))return"to-clone"===s.params?(e.add(s),void s.cloneBefore({params:"cloned"})):"to-clone"===s.name?(e.add(s),void s.cloneBefore({name:"cloned"})):void 0}}}};class PackageDescriptionError extends Error{constructor(e,s){super(e),this.name="PackageDescriptionError",this.stack=`${this.name}: ${this.message}\n at "${s}" (${a.pathToFileURL(n.resolve("package.json"))}:1:1)`}}class OutcomeError extends Error{constructor(e,s){super(e),this.name="OutcomeError",this.stack=`${this.name}: ${this.message}\n at ${a.pathToFileURL(n.resolve(s))}:1:1`}}exports.atRuleClonerPlugin=l,exports.declarationClonerPlugin=c,exports.postcssTape=function postcssTape(a,c){c=c??{},i("`postcss` flag is set on exported plugin creator",(()=>{e.equal(a.postcss,!0)})),i("exported plugin creator is a function",(()=>{e.equal(typeof a,"function")})),i("`postcssPlugin` is set on a plugin instance",(()=>{const s=a();e.ok(s.postcssPlugin),e.equal(typeof s.postcssPlugin,"string")})),i("package.json",(async t=>{const n=await s.readFile("./package.json","utf-8"),o=JSON.parse(n);await t.test('includes "postcss-plugin" keyword',(()=>{e.ok(o.keywords?.includes("postcss-plugin"),new PackageDescriptionError('Missing "postcss-plugin" keyword in package.json',"keywords"))})),await t.test('name starts with "postcss-"',{skip:c?.skipPackageNameCheck},(()=>{let s=o.name;if(s.startsWith("@")){s=o.name.split("/").slice(1).join("/")}e.ok(s.startsWith("postcss-"),new PackageDescriptionError(`package name "${s}" does not start with "postcss-"`,"name"))})),await t.test("`postcss` is a peer dependency and not a direct dependency",{skip:"postcssTapeSelfTest"in a},(()=>{e.ok(Object.keys(Object(o.peerDependencies)).includes("postcss"),new PackageDescriptionError('"postcss" must be listed in "peerDependencies"',"peerDependencies")),e.ok(!Object.keys(Object(o.dependencies)).includes("postcss"),new PackageDescriptionError('"postcss" must not be listed in "dependencies"',"dependencies"))}))}));const p=a().postcssPlugin;return async c=>{await i(p,(async i=>{for(const p in c)await i.test(p,(async i=>{const l=c[p];l.before&&await l.before();const u=n.join(".","test",...p.split(":")[0].split(n.posix.sep)),d=n.join(".","test",...p.replace(/:/g,".").split(n.posix.sep)),g="css";let m=`${u}.${g}`,w=`${d}.expect.${g}`,f=`${d}.result.${g}`;l.source&&(m=n.join(".","test",l.source)),l.expect&&(w=n.join(".","test",l.expect)),l.result&&(f=n.join(".","test",l.result));const k=l.plugins??[a(l.options)],h=await fileContentsOrEmptyString(m),y=await fileContentsOrEmptyString(w);let E;try{E=await o(k).process(h,{from:m,to:f,map:{inline:!1,annotation:!1}})}catch(e){if(reduceInformationInCssSyntaxError(e),l.exception&&l.exception.test(e.message))return;throw e}e.ok(!l.exception,new OutcomeError(`expected an exception matching "${l.exception}"`,m));const x=E.css.toString();{const e=[s.writeFile(f,x,"utf8")];process.env.REWRITE_EXPECTS&&e.push(s.writeFile(w,x,"utf8")),await Promise.all(e)}y||e.ok(t.existsSync(w),new OutcomeError(`Missing expect file: "${w}"`,m)),await i.test("has expected output",(()=>{e.deepEqual(x,y),e.deepEqual(E.warnings().length,l.warnings??0,"Unexpected number warnings")})),await i.test("sourcemaps",(async()=>{e.ok(!E.map.toJSON().sources.includes("<no source>"),'Sourcemap is broken. This is most likely a newly created PostCSS AST Node without a value for "source". See: https://github.com/postcss/postcss/blob/main/docs/guidelines/plugin.md#24-set-nodesource-for-new-nodes')})),l.after&&await l.after(),await i.test("output is parsable with PostCSS",(async()=>{const s=await fileContentsOrEmptyString(f),t=await o([noopPlugin()]).process(s,{from:f,to:f,map:{inline:!1,annotation:!1}});e.deepEqual(t.warnings(),[],"Unexpected warnings on second pass")})),await i.test("The oldest and current PostCSS version produce the same result",{skip:o([noopPlugin()]).version===r([noopPlugin()]).version},(async()=>{const s=await r(k).process(h,{from:m,to:f,map:{inline:!1,annotation:!1}});e.deepEqual(s.css.toString(),x)}))}))}))}},exports.ruleClonerPlugin=p;
/* node:coverage enable */
2 changes: 1 addition & 1 deletion packages/postcss-tape/dist/index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/* node:coverage disable */
import e from"node:assert/strict";import t from"node:fs/promises";import s from"node:fs";import o from"node:path";import n from"postcss";import i from"postcss-8.4";import r from"node:test";import a from"node:url";const noopPlugin=()=>({postcssPlugin:"noop-plugin",Rule(){}});async function fileContentsOrEmptyString(e){try{return await t.readFile(e,"utf8")}catch(e){return""}}function reduceInformationInCssSyntaxError(e){process.env.DEBUG||(delete e.source,e.input&&delete e.input.source,delete e.postcssNode)}function postcssTape(a,c){c=c??{},r("`postcss` flag is set on exported plugin creator",(()=>{e.strictEqual(a.postcss,!0)})),r("exported plugin creator is a function",(()=>{e.strictEqual(typeof a,"function")})),r("`postcssPlugin` is set on a plugin instance",(()=>{const t=a();e.ok(t.postcssPlugin),e.strictEqual(typeof t.postcssPlugin,"string")})),r("package.json",(async s=>{const o=await t.readFile("./package.json","utf-8"),n=JSON.parse(o);await s.test('includes "postcss-plugin" keyword',(()=>{e.ok(n.keywords?.includes("postcss-plugin"),new PackageDescriptionError('Missing "postcss-plugin" keyword in package.json',"keywords"))})),await s.test('name starts with "postcss-"',{skip:c?.skipPackageNameCheck},(()=>{let t=n.name;if(t.startsWith("@")){t=n.name.split("/").slice(1).join("/")}e.ok(t.startsWith("postcss-"),new PackageDescriptionError(`package name "${t}" does not start with "postcss-"`,"name"))})),await s.test("`postcss` is a peer dependency and not a direct dependency",{skip:"postcssTapeSelfTest"in a},(()=>{e.ok(Object.keys(Object(n.peerDependencies)).includes("postcss"),new PackageDescriptionError('"postcss" is not listed in "peerDependencies"',"peerDependencies")),e.ok(!Object.keys(Object(n.dependencies)).includes("postcss"),new PackageDescriptionError('"postcss" must not be listed in "dependencies"',"dependencies"))}))}));const p=a().postcssPlugin;return async c=>{await r(p,(async r=>{for(const p in c)await r.test(p,(async r=>{const l=c[p];l.before&&await l.before();const u=o.join(".","test",...p.split(":")[0].split(o.posix.sep)),d=o.join(".","test",...p.replace(/:/g,".").split(o.posix.sep)),m="css";let g=`${u}.${m}`,f=`${d}.expect.${m}`,w=`${d}.result.${m}`;l.source&&(g=o.join(".","test",l.source)),l.expect&&(f=o.join(".","test",l.expect)),l.result&&(w=o.join(".","test",l.result));const k=l.plugins??[a(l.options)],y=await fileContentsOrEmptyString(g),E=await fileContentsOrEmptyString(f);let h,S=!1;try{h=await n(k).process(y,{from:g,to:w,map:{inline:!1,annotation:!1}})}catch(t){if(reduceInformationInCssSyntaxError(t),S=!0,l.exception&&l.exception.test(t.message))return;e.ifError(t)}e.notEqual(!S,l.exception,"expected an exception but got none");const x=h.css.toString();{const e=[t.writeFile(w,x,"utf8")];process.env.REWRITE_EXPECTS&&e.push(t.writeFile(f,x,"utf8")),await Promise.all(e)}E||e.ok(s.existsSync(f),`Missing expect file: "${f}"`),await r.test("has expected output",(()=>{e.strictEqual(x,E),e.deepStrictEqual(h.warnings().length,l.warnings??0,"Unexpected number warnings")})),await r.test("sourcemaps",(async()=>{e.ok(!h.map.toJSON().sources.includes("<no source>"),'Sourcemap is broken. This is most likely a newly created PostCSS AST Node without a value for "source". See: https://github.com/postcss/postcss/blob/main/docs/guidelines/plugin.md#24-set-nodesource-for-new-nodes')})),l.after&&await l.after(),await r.test("output is parsable with PostCSS",(async()=>{const t=await fileContentsOrEmptyString(w),s=await n([noopPlugin()]).process(t,{from:w,to:w,map:{inline:!1,annotation:!1}});e.deepStrictEqual(s.warnings(),[],"Unexpected warnings on second pass")})),await r.test("The oldest and current PostCSS version produce the same result",{skip:n([noopPlugin()]).version===i([noopPlugin()]).version},(async()=>{const t=await i(k).process(y,{from:g,to:w,map:{inline:!1,annotation:!1}});e.strictEqual(t.css.toString(),x)}))}))}))}}noopPlugin.postcss=!0;const c={postcssPlugin:"declaration-cloner",Declaration(e){"to-clone"===e.prop&&e.cloneBefore({prop:"cloned"})}},p={postcssPlugin:"rule-cloner",prepare(){const e=new WeakSet;return{RuleExit(t){e.has(t)||"to-clone"===t.selector&&(e.add(t),t.cloneBefore({selector:"cloned"}))}}}},l={postcssPlugin:"at-rule-cloner",prepare(){const e=new WeakSet;return{AtRuleExit(t){if(!e.has(t))return"to-clone"===t.params?(e.add(t),void t.cloneBefore({params:"cloned"})):"to-clone"===t.name?(e.add(t),void t.cloneBefore({name:"cloned"})):void 0}}}};class PackageDescriptionError extends Error{constructor(e,t){super(e),this.name="PackageDescriptionError",this.stack=`${this.name}: ${this.message}\n at "${t}" (${a.pathToFileURL(o.resolve("package.json")).pathname})`}}export{l as atRuleClonerPlugin,c as declarationClonerPlugin,postcssTape,p as ruleClonerPlugin};
import e from"node:assert/strict";import s from"node:fs/promises";import t from"node:fs";import o from"node:path";import n from"postcss";import r from"postcss-8.4";import i from"node:test";import a from"node:url";const noopPlugin=()=>({postcssPlugin:"noop-plugin",Rule(){}});async function fileContentsOrEmptyString(e){try{return await s.readFile(e,"utf8")}catch(e){return""}}function reduceInformationInCssSyntaxError(e){process.env.DEBUG||(delete e.source,e.input&&delete e.input.source,delete e.postcssNode)}function postcssTape(a,c){c=c??{},i("`postcss` flag is set on exported plugin creator",(()=>{e.equal(a.postcss,!0)})),i("exported plugin creator is a function",(()=>{e.equal(typeof a,"function")})),i("`postcssPlugin` is set on a plugin instance",(()=>{const s=a();e.ok(s.postcssPlugin),e.equal(typeof s.postcssPlugin,"string")})),i("package.json",(async t=>{const o=await s.readFile("./package.json","utf-8"),n=JSON.parse(o);await t.test('includes "postcss-plugin" keyword',(()=>{e.ok(n.keywords?.includes("postcss-plugin"),new PackageDescriptionError('Missing "postcss-plugin" keyword in package.json',"keywords"))})),await t.test('name starts with "postcss-"',{skip:c?.skipPackageNameCheck},(()=>{let s=n.name;if(s.startsWith("@")){s=n.name.split("/").slice(1).join("/")}e.ok(s.startsWith("postcss-"),new PackageDescriptionError(`package name "${s}" does not start with "postcss-"`,"name"))})),await t.test("`postcss` is a peer dependency and not a direct dependency",{skip:"postcssTapeSelfTest"in a},(()=>{e.ok(Object.keys(Object(n.peerDependencies)).includes("postcss"),new PackageDescriptionError('"postcss" must be listed in "peerDependencies"',"peerDependencies")),e.ok(!Object.keys(Object(n.dependencies)).includes("postcss"),new PackageDescriptionError('"postcss" must not be listed in "dependencies"',"dependencies"))}))}));const p=a().postcssPlugin;return async c=>{await i(p,(async i=>{for(const p in c)await i.test(p,(async i=>{const l=c[p];l.before&&await l.before();const u=o.join(".","test",...p.split(":")[0].split(o.posix.sep)),d=o.join(".","test",...p.replace(/:/g,".").split(o.posix.sep)),m="css";let g=`${u}.${m}`,f=`${d}.expect.${m}`,w=`${d}.result.${m}`;l.source&&(g=o.join(".","test",l.source)),l.expect&&(f=o.join(".","test",l.expect)),l.result&&(w=o.join(".","test",l.result));const k=l.plugins??[a(l.options)],h=await fileContentsOrEmptyString(g),y=await fileContentsOrEmptyString(f);let E;try{E=await n(k).process(h,{from:g,to:w,map:{inline:!1,annotation:!1}})}catch(e){if(reduceInformationInCssSyntaxError(e),l.exception&&l.exception.test(e.message))return;throw e}e.ok(!l.exception,new OutcomeError(`expected an exception matching "${l.exception}"`,g));const x=E.css.toString();{const e=[s.writeFile(w,x,"utf8")];process.env.REWRITE_EXPECTS&&e.push(s.writeFile(f,x,"utf8")),await Promise.all(e)}y||e.ok(t.existsSync(f),new OutcomeError(`Missing expect file: "${f}"`,g)),await i.test("has expected output",(()=>{e.deepEqual(x,y),e.deepEqual(E.warnings().length,l.warnings??0,"Unexpected number warnings")})),await i.test("sourcemaps",(async()=>{e.ok(!E.map.toJSON().sources.includes("<no source>"),'Sourcemap is broken. This is most likely a newly created PostCSS AST Node without a value for "source". See: https://github.com/postcss/postcss/blob/main/docs/guidelines/plugin.md#24-set-nodesource-for-new-nodes')})),l.after&&await l.after(),await i.test("output is parsable with PostCSS",(async()=>{const s=await fileContentsOrEmptyString(w),t=await n([noopPlugin()]).process(s,{from:w,to:w,map:{inline:!1,annotation:!1}});e.deepEqual(t.warnings(),[],"Unexpected warnings on second pass")})),await i.test("The oldest and current PostCSS version produce the same result",{skip:n([noopPlugin()]).version===r([noopPlugin()]).version},(async()=>{const s=await r(k).process(h,{from:g,to:w,map:{inline:!1,annotation:!1}});e.deepEqual(s.css.toString(),x)}))}))}))}}noopPlugin.postcss=!0;const c={postcssPlugin:"declaration-cloner",Declaration(e){"to-clone"===e.prop&&e.cloneBefore({prop:"cloned"})}},p={postcssPlugin:"rule-cloner",prepare(){const e=new WeakSet;return{RuleExit(s){e.has(s)||"to-clone"===s.selector&&(e.add(s),s.cloneBefore({selector:"cloned"}))}}}},l={postcssPlugin:"at-rule-cloner",prepare(){const e=new WeakSet;return{AtRuleExit(s){if(!e.has(s))return"to-clone"===s.params?(e.add(s),void s.cloneBefore({params:"cloned"})):"to-clone"===s.name?(e.add(s),void s.cloneBefore({name:"cloned"})):void 0}}}};class PackageDescriptionError extends Error{constructor(e,s){super(e),this.name="PackageDescriptionError",this.stack=`${this.name}: ${this.message}\n at "${s}" (${a.pathToFileURL(o.resolve("package.json"))}:1:1)`}}class OutcomeError extends Error{constructor(e,s){super(e),this.name="OutcomeError",this.stack=`${this.name}: ${this.message}\n at ${a.pathToFileURL(o.resolve(s))}:1:1`}}export{l as atRuleClonerPlugin,c as declarationClonerPlugin,postcssTape,p as ruleClonerPlugin};
/* node:coverage enable */
Loading