diff options
author | Sebastian <sebasjm@gmail.com> | 2021-08-23 16:46:06 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2021-08-23 16:48:30 -0300 |
commit | 38acabfa6089ab8ac469c12b5f55022fb96935e5 (patch) | |
tree | 453dbf70000cc5e338b06201af1eaca8343f8f73 /@linaria/packages/babel/src/utils/getLinariaComment.ts | |
parent | f26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff) | |
download | node-vendor-master.tar.gz node-vendor-master.tar.bz2 node-vendor-master.zip |
Diffstat (limited to '@linaria/packages/babel/src/utils/getLinariaComment.ts')
-rw-r--r-- | @linaria/packages/babel/src/utils/getLinariaComment.ts | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/@linaria/packages/babel/src/utils/getLinariaComment.ts b/@linaria/packages/babel/src/utils/getLinariaComment.ts new file mode 100644 index 0000000..06b3edb --- /dev/null +++ b/@linaria/packages/babel/src/utils/getLinariaComment.ts @@ -0,0 +1,31 @@ +import type { Node } from '@babel/types'; + +const pattern = /^linaria (css|styled) (.+)$/; + +export default function getLinariaComment( + path: { node: Node }, + remove: boolean = true +): ['css' | 'styled' | null, ...(string | null)[]] { + const comments = path.node.leadingComments; + if (!comments) { + return [null, null, null, null]; + } + + const idx = comments.findIndex((comment) => pattern.test(comment.value)); + if (idx === -1) { + return [null, null, null, null]; + } + + const matched = comments[idx].value.match(pattern); + if (!matched) { + return [null, null, null, null]; + } + + if (remove) { + path.node.leadingComments = comments.filter((_, i) => i !== idx); + } + + const type = matched[1] === 'css' ? 'css' : 'styled'; + + return [type, ...matched[2].split(' ').map((i) => (i ? i : null))]; +} |