@domql/globusa 3.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@domql/globusa",
3
+ "version": "3.7.6",
4
+ "description": "Handle global scope in domql components",
5
+ "scripts": {
6
+ "build": "node ./esbuild-globusa.js",
7
+ "test": "cd tests && bash run-tests.sh"
8
+ },
9
+ "files": [
10
+ "dist",
11
+ "src"
12
+ ],
13
+ "main": "dist/index.cjs",
14
+ "source": "src/index.js",
15
+ "exports": {
16
+ ".": {
17
+ "default": "./dist/index.cjs"
18
+ }
19
+ },
20
+ "license": "MIT",
21
+ "dependencies": {
22
+ "@babel/core": "^7.22.8",
23
+ "@babel/parser": "^7.22.7",
24
+ "@babel/traverse": "^7.22.8",
25
+ "@babel/types": "^7.22.5",
26
+ "stringify-object-extract-functions": "^2.4.1"
27
+ },
28
+ "devDependencies": {
29
+ "esbuild": "^0.18.11"
30
+ }
31
+ }
package/src/index.js ADDED
@@ -0,0 +1,117 @@
1
+ import stringify from './stringify'
2
+ import { parse as babelParse } from '@babel/parser'
3
+ import babelTraverse from '@babel/traverse'
4
+ import * as types from '@babel/types'
5
+
6
+ function handleImportDeclaration(code, node) {
7
+ const importObj = {
8
+ imports: {},
9
+ path: node.source.value,
10
+ }
11
+
12
+ for (const s of node.specifiers) {
13
+ let importName, varName;
14
+ if (types.isImportDefaultSpecifier(s)) {
15
+ importName = 'default'
16
+ varName = s.local.name
17
+ } else if (types.isImportNamespaceSpecifier(s)) {
18
+ importName = '*'
19
+ varName = s.local.name
20
+ } else {
21
+ importName = s.imported.name
22
+ varName = s.local.name
23
+ }
24
+ importObj.imports[varName] = importName
25
+ }
26
+
27
+ return importObj
28
+ }
29
+
30
+ export function parse(code) {
31
+ const globusaObj = {
32
+ // Array of mitosis-style import structures
33
+ imports: [],
34
+
35
+ // Array of lines of code (strings) which are part of export statements
36
+ exports: [],
37
+
38
+ // Any line of code that's not an import or an export. (Array of strings).
39
+ declarations: [],
40
+
41
+ // Any line of code that's not part of an 'import' statement
42
+ linesExcludingImports: [],
43
+
44
+ // Array of strings which are parts of a domql component
45
+ domqlComponents: [],
46
+
47
+ // Array of strings of identifiers declared in the code
48
+ identifiers: []
49
+ }
50
+
51
+ const codeAST = babelParse(code, { sourceType: "module", plugins: ["jsx"] })
52
+
53
+ babelTraverse(codeAST, {
54
+ enter(path) {
55
+ if (path.isImportDeclaration()) {
56
+ const mitosisImportDecl = handleImportDeclaration(code, path.node)
57
+ globusaObj.imports.push(mitosisImportDecl)
58
+ path.skip()
59
+ } else if (path.isExportDeclaration()) {
60
+ const codeStr = code.substring(path.node.start, path.node.end)
61
+ globusaObj.exports.push(codeStr)
62
+ globusaObj.linesExcludingImports.push(codeStr)
63
+
64
+ // Extract the domql component
65
+ const decl = path.node.declaration
66
+ if (decl && decl.declarations) {
67
+ for (const d of decl.declarations) {
68
+ const compo = { name: null, code: "" }
69
+
70
+ // Extract component name
71
+ if (d.id && types.isIdentifier(d.id) && d.id.name) {
72
+ compo.name = d.id.name
73
+
74
+ // Also save the variable name in identifiers
75
+ if (types.isProgram(path.parent))
76
+ globusaObj.identifiers.push(d.id.name)
77
+ }
78
+
79
+ // Extract object expression
80
+ if (d.init && types.isObjectExpression(d.init)) {
81
+ compo.code = code.substring(d.init.start, d.init.end)
82
+ }
83
+
84
+ globusaObj.domqlComponents.push(compo)
85
+ }
86
+ }
87
+
88
+ path.skip()
89
+ } else if (path.isVariableDeclaration() && types.isProgram(path.parent)) {
90
+ for (const decl of path.node.declarations) {
91
+ if (decl && decl.id && decl.id.name)
92
+ globusaObj.identifiers.push(decl.id.name)
93
+ }
94
+ } else if (path.isFunctionDeclaration() && types.isProgram(path.parent)) {
95
+ const decl = path.node
96
+ if (decl.id && decl.id.name)
97
+ globusaObj.identifiers.push(decl.id.name)
98
+ }
99
+
100
+ if (!path.isProgram() && !path.isImportDeclaration() &&
101
+ !path.isExportDeclaration()) {
102
+ // Skip directives like 'use strict'
103
+ if (path.isDirective()) {
104
+ path.skip()
105
+ return;
106
+ }
107
+
108
+ const codeStr = code.substring(path.node.start, path.node.end)
109
+ globusaObj.declarations.push(codeStr)
110
+ globusaObj.linesExcludingImports.push(codeStr)
111
+ path.skip()
112
+ }
113
+ }
114
+ })
115
+
116
+ return globusaObj
117
+ }
@@ -0,0 +1,27 @@
1
+ import stringifyObject from 'stringify-object-extract-functions'
2
+
3
+ function genSpaces(num) {
4
+ if (!num || num < 1) return ''
5
+ let str = ''
6
+ for (let i = 0; i < num; i++)
7
+ str = str + ' '
8
+ return str
9
+ }
10
+
11
+ // Equivalent to 'JSON.stringify', but also stringifies functions
12
+ export default function stringify(a, indentWidth) {
13
+ if (typeof a === 'function' || typeof a === 'number')
14
+ return a.toString()
15
+
16
+ const str = stringifyObject(a, {
17
+ indent: genSpaces(indentWidth),
18
+ inlineCharacterLimit: 24
19
+ })
20
+
21
+ if (str[str.length - 2] === '\n') {
22
+ // Correctly indent trailing char (either '}' or ']')
23
+ return str.slice(0, str.length - 1) + genSpaces(indentWidth - 2) + str.slice(-1)
24
+ }
25
+
26
+ return str
27
+ }
@@ -0,0 +1,21 @@
1
+ export const testcode = `
2
+ import { downcase } from 'downcase';
3
+ import { upcase } from 'upcase';
4
+
5
+ import Preact from 'preact';
6
+ import * as MathFunctions from 'math';
7
+ import React, { utils as reactUtils } from 'react';
8
+
9
+ import emotion, {
10
+ setClassName as scn,
11
+ doTheThing as dtt,
12
+ evaluate } from '@emotion/css';
13
+
14
+ const privateFunction = (x) => x * 2
15
+
16
+ export const RandomComponentName = {
17
+ tag: 'div',
18
+ p: () => upcase('hello, friends!'),
19
+ on: { click: (ev, el) => console.log(privateFunction(el.props.x)) },
20
+ props: { x: 12 }
21
+ }`