Skip to content

Commit

Permalink
πŸŒ€ Enable building kdjs with kdjs
Browse files Browse the repository at this point in the history
  • Loading branch information
KimlikDAO-bot committed Jan 2, 2025
1 parent 234b50f commit d343661
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ jobs:
bun run test --filter did crypto
compile_kdjs_with_kdjs:
if: false # Temporarily disabled
name: Compile `kdjs` with `kdjs`
runs-on: ubuntu-latest
steps:
Expand Down
60 changes: 59 additions & 1 deletion kdjs/externs/css.d.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,66 @@
/** @const */
const css = {};

/**
* @interface
* @struct
*/
css.BaseNode = function () { }

/** @const {string} */
css.BaseNode.prototype.type;

/**
* @const {{
* content: string
* }}
*/
css.BaseNode.prototype.position;

/**
* @interface
* @extends {css.BaseNode}
*/
css.Rule = function () { }

/** @const {!Array<string>} */
css.Rule.prototype.selectors;

/**
* @interface
* @extends {css.BaseNode}
*/
css.Comment = function () { }

/** @const {string} */
css.Comment.prototype.comment;

/**
* @interface
* @extends {css.BaseNode}
*/
css.Stylesheet = function () { }

/**
* @const {{
* rules: !Array<!css.Rule|!css.Comment|!css.Media>
* }}
*/
css.Stylesheet.prototype.stylesheet;

/**
* @interface
* @extends {css.BaseNode}
*/
css.Media = function () { }

/** @const {!Array<!css.Rule|!css.Comment|!css.Media>} */
css.Media.prototype.rules;

/**
* @param {string} content
* @return {!Object<string, string>}
* @return {!css.Stylesheet}
*/
css.parse = (content) => { };

export default css;
21 changes: 13 additions & 8 deletions kdjs/stylesheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,33 @@ import css from "css";
* @return {string} js code that exports the enum
*/
const processCss = (content) => {
/** @const {!css.Stylesheet} */
const parsedCss = css.parse(content);
/** @const {!Object<string, string>} */
const exports = {};

const rulesStack = [parsedCss.stylesheet.rules];
for (let rules; rules = rulesStack.pop();) {
/** @const {!Array<!Array<!css.Rule|!css.Comment|!css.Media>>} */
const nodesStack = [parsedCss.stylesheet.rules];
for (let nodes; nodes = nodesStack.pop();) {
/** @type {?string} */
let maybeExport;
for (const rule of rules) {
if (rule.type == "comment") {
const matches = rule.comment.match(/@export\s*{(.*)}/);
for (const node of nodes) {
if (node.type == "comment") {
const matches = /** @type {!css.Comment} */(node).comment.match(/@export\s*{(.*)}/);
if (matches) maybeExport = matches[1].trim();
} else if (rule.type == "rule") {
} else if (node.type == "rule") {
/** @const {!css.Rule} */
const rule = /** @type {!css.Rule} */(node);
if (maybeExport) {
if (rule.selectors.length != 1 || rule.selectors[0].includes(" "))
throw `Named or exported selectors must be singletons. Violating rule: ${rule.position.content}`;
exports[maybeExport] = rule.selectors[0].slice(1);
}
maybeExport = null;
} else if (rule.type == "media" || rule.type == "supports") {
} else if (node.type == "media") {
if (maybeExport)
throw "Only singleton selectors may be named.";
rulesStack.push(rule.rules);
nodesStack.push(/** @type {!css.Media} */(node).rules);
}
}
}
Expand Down

0 comments on commit d343661

Please sign in to comment.