summaryrefslogtreecommitdiff
path: root/@linaria/packages/babel/src/utils/getLinariaComment.ts
diff options
context:
space:
mode:
Diffstat (limited to '@linaria/packages/babel/src/utils/getLinariaComment.ts')
-rw-r--r--@linaria/packages/babel/src/utils/getLinariaComment.ts31
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))];
+}