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

feat: Add stylis for css #598

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"solidity-parser-diligence": "^0.4.18",
"source-map": "^0.6.1",
"sqlite-parser": "^1.0.0-rc3",
"stylis": "^4.0.10",
"svelte": "^3.4.1",
"tenko": "^1.0.6",
"tern": "^0.24.3",
Expand Down
61 changes: 61 additions & 0 deletions website/src/parsers/css/stylis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import defaultParserInterface from '../utils/defaultParserInterface';
import pkg from 'stylis/package.json';

const ID = 'stylis';

function removeLength(node, lineLengths) {
Copy link
Owner

@fkling fkling Aug 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this function? Why is it necessary to remove length?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a workaround - if length:0 is present astexplorer will hide all children

if (!node || typeof node !== 'object') return;
if (Array.isArray(node.children)) {
node.children.forEach((child) => removeLength(child, lineLengths));
}
if ('line' in node && 'column' in node) {
delete(node.length);
if (Array.isArray(node.children)) {
node.length = node.children.length;
}
}
}

export default {
...defaultParserInterface,

id: ID,
displayName: ID,
version: pkg.version,
homepage: pkg.homepage || 'https://stylis.js.org/',
locationProps: new Set(['line', 'column']),

loadParser(callback) {
require(['stylis'], callback);
},

parse(stylis, code) {
const ast = stylis.compile(code);
const rootNode = {
type: 'Stylesheet',
line:0,
column:0,
length: ast.length,
children: ast,
};
removeLength(rootNode);
return rootNode;
},

nodeToRange() {
// Return void until the stylis provides the correct position
// @see https://github.com/thysultan/stylis.js/issues/269
},

_ignoredProperties: new Set(['parent', 'root']),

opensByDefault(node, key) {
return key === 'children' && typeof node[key] !== 'string';
},

getDefaultOptions() {
return {
};
},

};
10 changes: 10 additions & 0 deletions website/src/parsers/css/transformers/stylis/codeExample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { prefixer } from "stylis";

export default [
(element, index, children, callback) => {
if (element.type === "decl" && element.props === "border") {
element.return = element.value.replace(/^border/, "outline");
}
},
prefixer,
];
35 changes: 35 additions & 0 deletions website/src/parsers/css/transformers/stylis/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import compileModule from '../../../utils/compileModule';
import pkg from 'stylis/package.json';

const ID = 'stylis';

export default {
id: ID,
displayName: ID,
version: pkg.version,
homepage: pkg.homepage,

defaultParserID: 'stylis',

loadTransformer(callback) {
require(['../../../transpilers/babel', 'stylis'], (transpile, stylis) => {
callback({ transpile: transpile.default, stylis });
});
},

transform({ transpile, stylis }, transformCode, code) {
transformCode = transpile( transformCode);
let transform = compileModule( // eslint-disable-line no-shadow
transformCode,
{
require(name) {
switch (name) {
case 'stylis': return stylis;
default: throw new Error(`Cannot find module '${name}'`);
}
},
},
);
return stylis.serialize(stylis.compile(code), stylis.middleware([].concat(transform.default || transform).concat(stylis.stringify)));
},
};
5 changes: 5 additions & 0 deletions website/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10550,6 +10550,11 @@ style-to-object@^0.2.1:
dependencies:
inline-style-parser "0.1.1"

stylis@^4.0.10:
version "4.0.10"
resolved "https://registry.yarnpkg.com/stylis/-/stylis-4.0.10.tgz#446512d1097197ab3f02fb3c258358c3f7a14240"
integrity sha512-m3k+dk7QeJw660eIKRRn3xPF6uuvHs/FFzjX3HQ5ove0qYsiygoAhwn5a3IYKaZPo5LrYD0rfVmtv1gNY1uYwg==

[email protected], supports-color@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-6.1.0.tgz#0764abc69c63d5ac842dd4867e8d025e880df8f3"
Expand Down