@jsenv/ast 3.0.7 → 4.0.0

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.
@@ -1,9 +1,9 @@
1
- import { isStringLiteralNode } from "./helpers.js"
1
+ import { isStringLiteralNode } from "./helpers.js";
2
2
 
3
3
  export const isImportScriptsCall = (node) => {
4
- const callee = node.callee
4
+ const callee = node.callee;
5
5
  if (callee.type === "Identifier" && callee.name === "importScripts") {
6
- return true
6
+ return true;
7
7
  }
8
8
  return (
9
9
  callee.type === "MemberExpression" &&
@@ -11,12 +11,12 @@ export const isImportScriptsCall = (node) => {
11
11
  callee.object.name === "self" &&
12
12
  callee.property.type === "Identifier" &&
13
13
  callee.property.name === "importScripts"
14
- )
15
- }
14
+ );
15
+ };
16
16
  export const analyzeImportScriptCalls = (node, { onUrl }) => {
17
17
  node.arguments.forEach((arg) => {
18
18
  if (isStringLiteralNode(arg)) {
19
- const specifierNode = arg
19
+ const specifierNode = arg;
20
20
  onUrl({
21
21
  type: "js_url",
22
22
  subtype: "self_import_scripts_arg",
@@ -26,7 +26,7 @@ export const analyzeImportScriptCalls = (node, { onUrl }) => {
26
26
  end: specifierNode.end,
27
27
  line: specifierNode.loc.start.line,
28
28
  column: specifierNode.loc.start.column,
29
- })
29
+ });
30
30
  }
31
- })
32
- }
31
+ });
32
+ };
@@ -1,87 +1,87 @@
1
- import { getTypePropertyNode, isStringLiteralNode } from "./helpers.js"
2
- import { isNewUrlCall, analyzeNewUrlCall } from "./new_url.js"
1
+ import { getTypePropertyNode, isStringLiteralNode } from "./helpers.js";
2
+ import { isNewUrlCall, analyzeNewUrlCall } from "./new_url.js";
3
3
  import {
4
4
  isImportMetaResolveCall,
5
5
  analyzeImportMetaResolveCall,
6
- } from "./import_meta_resolve.js"
6
+ } from "./import_meta_resolve.js";
7
7
 
8
8
  export const isNewWorkerCall = (node) => {
9
9
  return (
10
10
  node.type === "NewExpression" &&
11
11
  node.callee.type === "Identifier" &&
12
12
  node.callee.name === "Worker"
13
- )
14
- }
13
+ );
14
+ };
15
15
  export const analyzeNewWorkerCall = (node, { isJsModule, onUrl }) => {
16
16
  analyzeWorkerCallArguments(node, {
17
17
  isJsModule,
18
18
  onUrl,
19
19
  referenceSubtype: "new_worker_first_arg",
20
20
  expectedSubtype: "worker",
21
- })
22
- }
21
+ });
22
+ };
23
23
 
24
24
  export const isNewSharedWorkerCall = (node) => {
25
25
  return (
26
26
  node.type === "NewExpression" &&
27
27
  node.callee.type === "Identifier" &&
28
28
  node.callee.name === "SharedWorker"
29
- )
30
- }
29
+ );
30
+ };
31
31
  export const analyzeNewSharedWorkerCall = (node, { isJsModule, onUrl }) => {
32
32
  analyzeWorkerCallArguments(node, {
33
33
  isJsModule,
34
34
  onUrl,
35
35
  referenceSubtype: "new_shared_worker_first_arg",
36
36
  expectedSubtype: "shared_worker",
37
- })
38
- }
37
+ });
38
+ };
39
39
 
40
40
  export const isServiceWorkerRegisterCall = (node) => {
41
41
  if (node.type !== "CallExpression") {
42
- return false
42
+ return false;
43
43
  }
44
- const callee = node.callee
44
+ const callee = node.callee;
45
45
  if (
46
46
  callee.type === "MemberExpression" &&
47
47
  callee.property.type === "Identifier" &&
48
48
  callee.property.name === "register"
49
49
  ) {
50
- const parentObject = callee.object
50
+ const parentObject = callee.object;
51
51
  if (parentObject.type === "MemberExpression") {
52
- const parentProperty = parentObject.property
52
+ const parentProperty = parentObject.property;
53
53
  if (
54
54
  parentProperty.type === "Identifier" &&
55
55
  parentProperty.name === "serviceWorker"
56
56
  ) {
57
- const grandParentObject = parentObject.object
57
+ const grandParentObject = parentObject.object;
58
58
  if (grandParentObject.type === "MemberExpression") {
59
59
  // window.navigator.serviceWorker.register
60
- const grandParentProperty = grandParentObject.property
60
+ const grandParentProperty = grandParentObject.property;
61
61
  if (
62
62
  grandParentProperty.type === "Identifier" &&
63
63
  grandParentProperty.name === "navigator"
64
64
  ) {
65
- const ancestorObject = grandParentObject.object
65
+ const ancestorObject = grandParentObject.object;
66
66
  if (
67
67
  ancestorObject.type === "Identifier" &&
68
68
  ancestorObject.name === "window"
69
69
  ) {
70
- return true
70
+ return true;
71
71
  }
72
72
  }
73
73
  }
74
74
  if (grandParentObject.type === "Identifier") {
75
75
  // navigator.serviceWorker.register
76
76
  if (grandParentObject.name === "navigator") {
77
- return true
77
+ return true;
78
78
  }
79
79
  }
80
80
  }
81
81
  }
82
82
  }
83
- return false
84
- }
83
+ return false;
84
+ };
85
85
  export const analyzeServiceWorkerRegisterCall = (
86
86
  node,
87
87
  { isJsModule, onUrl },
@@ -91,32 +91,32 @@ export const analyzeServiceWorkerRegisterCall = (
91
91
  onUrl,
92
92
  referenceSubtype: "service_worker_register_first_arg",
93
93
  expectedSubtype: "service_worker",
94
- })
95
- }
94
+ });
95
+ };
96
96
 
97
97
  const analyzeWorkerCallArguments = (
98
98
  node,
99
99
  { isJsModule, onUrl, referenceSubtype, expectedSubtype },
100
100
  ) => {
101
- let expectedType = "js_classic"
102
- let typePropertyNode
103
- const secondArgNode = node.arguments[1]
101
+ let expectedType = "js_classic";
102
+ let typePropertyNode;
103
+ const secondArgNode = node.arguments[1];
104
104
  if (secondArgNode) {
105
- typePropertyNode = getTypePropertyNode(secondArgNode)
105
+ typePropertyNode = getTypePropertyNode(secondArgNode);
106
106
  if (typePropertyNode) {
107
- const typePropertyValueNode = typePropertyNode.value
107
+ const typePropertyValueNode = typePropertyNode.value;
108
108
  if (isStringLiteralNode(typePropertyValueNode)) {
109
- const typePropertyValue = typePropertyValueNode.value
109
+ const typePropertyValue = typePropertyValueNode.value;
110
110
  if (typePropertyValue === "module") {
111
- expectedType = "js_module"
111
+ expectedType = "js_module";
112
112
  }
113
113
  }
114
114
  }
115
115
  }
116
116
 
117
- const firstArgNode = node.arguments[0]
117
+ const firstArgNode = node.arguments[0];
118
118
  if (isStringLiteralNode(firstArgNode)) {
119
- const specifierNode = firstArgNode
119
+ const specifierNode = firstArgNode;
120
120
  onUrl({
121
121
  type: "js_url",
122
122
  subtype: referenceSubtype,
@@ -128,8 +128,8 @@ const analyzeWorkerCallArguments = (
128
128
  end: specifierNode.end,
129
129
  line: specifierNode.loc.start.line,
130
130
  column: specifierNode.loc.start.column,
131
- })
132
- return
131
+ });
132
+ return;
133
133
  }
134
134
  if (isNewUrlCall(firstArgNode)) {
135
135
  analyzeNewUrlCall(firstArgNode, {
@@ -139,11 +139,11 @@ const analyzeWorkerCallArguments = (
139
139
  expectedType,
140
140
  expectedSubtype,
141
141
  typePropertyNode,
142
- })
143
- onUrl(mention)
142
+ });
143
+ onUrl(mention);
144
144
  },
145
- })
146
- return
145
+ });
146
+ return;
147
147
  }
148
148
  if (isJsModule && isImportMetaResolveCall(firstArgNode)) {
149
149
  analyzeImportMetaResolveCall(firstArgNode, {
@@ -152,10 +152,10 @@ const analyzeWorkerCallArguments = (
152
152
  expectedType,
153
153
  expectedSubtype,
154
154
  typePropertyNode,
155
- })
156
- onUrl(mention)
155
+ });
156
+ onUrl(mention);
157
157
  },
158
- })
159
- return
158
+ });
159
+ return;
160
160
  }
161
- }
161
+ };
@@ -1,16 +1,16 @@
1
- import { ancestor } from "acorn-walk"
1
+ import { ancestor } from "acorn-walk";
2
2
 
3
- import { parseJsWithAcorn } from "./parse_js_with_acorn.js"
3
+ import { parseJsWithAcorn } from "./parse_js_with_acorn.js";
4
4
  import {
5
5
  analyzeImportExpression,
6
6
  analyzeImportDeclaration,
7
7
  analyzeExportNamedDeclaration,
8
8
  analyzeExportAllDeclaration,
9
- } from "./js_static_analysis/import_export.js"
9
+ } from "./js_static_analysis/import_export.js";
10
10
  import {
11
11
  isImportMetaResolveCall,
12
12
  analyzeImportMetaResolveCall,
13
- } from "./js_static_analysis/import_meta_resolve.js"
13
+ } from "./js_static_analysis/import_meta_resolve.js";
14
14
  import {
15
15
  isNewWorkerCall,
16
16
  analyzeNewWorkerCall,
@@ -18,15 +18,15 @@ import {
18
18
  analyzeNewSharedWorkerCall,
19
19
  isServiceWorkerRegisterCall,
20
20
  analyzeServiceWorkerRegisterCall,
21
- } from "./js_static_analysis/web_worker_entry_point.js"
21
+ } from "./js_static_analysis/web_worker_entry_point.js";
22
22
  import {
23
23
  isImportScriptsCall,
24
24
  analyzeImportScriptCalls,
25
- } from "./js_static_analysis/web_worker.js"
25
+ } from "./js_static_analysis/web_worker.js";
26
26
  import {
27
27
  isNewUrlCall,
28
28
  analyzeNewUrlCall,
29
- } from "./js_static_analysis/new_url.js"
29
+ } from "./js_static_analysis/new_url.js";
30
30
  import {
31
31
  isSystemRegisterCall,
32
32
  analyzeSystemRegisterCall,
@@ -34,71 +34,96 @@ import {
34
34
  analyzeSystemImportCall,
35
35
  isSystemResolveCall,
36
36
  analyzeSystemResolveCall,
37
- } from "./js_static_analysis/system.js"
37
+ } from "./js_static_analysis/system.js";
38
+ import {
39
+ isNewBlobCall,
40
+ analyzeNewBlobCall,
41
+ } from "./js_static_analysis/new_blob.js";
42
+ import {
43
+ isNewInlineContentCall,
44
+ analyzeNewInlineContentCall,
45
+ } from "./js_static_analysis/new_inline_content.js";
46
+ import {
47
+ isJSONParseCall,
48
+ analyzeJSONParseCall,
49
+ } from "./js_static_analysis/json_parse.js";
38
50
 
39
51
  export const parseJsUrls = async ({
40
52
  js,
41
53
  url,
42
54
  isJsModule = false,
43
55
  isWebWorker = false,
56
+ inlineContent = true,
44
57
  } = {}) => {
45
- const jsUrls = []
58
+ const jsUrls = [];
46
59
  const jsAst = await parseJsWithAcorn({
47
60
  js,
48
61
  url,
49
62
  isJsModule,
50
- })
63
+ });
51
64
  const onUrl = (jsUrl) => {
52
- jsUrls.push(jsUrl)
53
- }
65
+ jsUrls.push(jsUrl);
66
+ };
67
+ const onInlineContent = (inlineContentInfo) => {
68
+ jsUrls.push({
69
+ isInline: true,
70
+ ...inlineContentInfo,
71
+ });
72
+ };
54
73
  ancestor(jsAst, {
55
74
  ImportDeclaration: (node) => {
56
- analyzeImportDeclaration(node, { onUrl })
75
+ analyzeImportDeclaration(node, { onUrl });
57
76
  },
58
77
  ImportExpression: (node) => {
59
- analyzeImportExpression(node, { onUrl })
78
+ analyzeImportExpression(node, { onUrl });
60
79
  },
61
80
  ExportNamedDeclaration: (node) => {
62
- analyzeExportNamedDeclaration(node, { onUrl })
81
+ analyzeExportNamedDeclaration(node, { onUrl });
63
82
  },
64
83
  ExportAllDeclaration: (node) => {
65
- analyzeExportAllDeclaration(node, { onUrl })
84
+ analyzeExportAllDeclaration(node, { onUrl });
66
85
  },
67
86
  CallExpression: (node) => {
68
87
  if (isJsModule && isImportMetaResolveCall(node)) {
69
- analyzeImportMetaResolveCall(node, { onUrl })
70
- return
88
+ analyzeImportMetaResolveCall(node, { onUrl });
89
+ return;
71
90
  }
72
91
  if (isServiceWorkerRegisterCall(node)) {
73
92
  analyzeServiceWorkerRegisterCall(node, {
74
93
  isJsModule,
75
94
  onUrl,
76
- })
77
- return
95
+ });
96
+ return;
78
97
  }
79
98
  if (isWebWorker && isImportScriptsCall(node)) {
80
99
  analyzeImportScriptCalls(node, {
81
100
  onUrl,
82
- })
83
- return
101
+ });
102
+ return;
84
103
  }
85
104
  if (!isJsModule && isSystemRegisterCall(node)) {
86
105
  analyzeSystemRegisterCall(node, {
87
106
  onUrl,
88
- })
89
- return
107
+ });
108
+ return;
90
109
  }
91
110
  if (!isJsModule && isSystemImportCall(node)) {
92
111
  analyzeSystemImportCall(node, {
93
112
  onUrl,
94
- })
95
- return
113
+ });
114
+ return;
96
115
  }
97
116
  if (!isJsModule && isSystemResolveCall(node)) {
98
117
  analyzeSystemResolveCall(node, {
99
118
  onUrl,
100
- })
101
- return
119
+ });
120
+ return;
121
+ }
122
+ if (inlineContent && isJSONParseCall(node)) {
123
+ analyzeJSONParseCall(node, {
124
+ onInlineContent,
125
+ });
126
+ return;
102
127
  }
103
128
  },
104
129
  NewExpression: (node, ancestors) => {
@@ -106,33 +131,45 @@ export const parseJsUrls = async ({
106
131
  analyzeNewWorkerCall(node, {
107
132
  isJsModule,
108
133
  onUrl,
109
- })
110
- return
134
+ });
135
+ return;
111
136
  }
112
137
  if (isNewSharedWorkerCall(node)) {
113
138
  analyzeNewSharedWorkerCall(node, {
114
139
  isJsModule,
115
140
  onUrl,
116
- })
117
- return
141
+ });
142
+ return;
118
143
  }
119
144
  if (isNewUrlCall(node)) {
120
- const parent = ancestors[ancestors.length - 2]
145
+ const parent = ancestors[ancestors.length - 2];
121
146
  if (
122
147
  parent &&
123
148
  (isNewWorkerCall(parent) ||
124
149
  isNewSharedWorkerCall(parent) ||
125
150
  isServiceWorkerRegisterCall(parent))
126
151
  ) {
127
- return
152
+ return;
128
153
  }
129
154
  analyzeNewUrlCall(node, {
130
155
  isJsModule,
131
156
  onUrl,
132
- })
133
- return
157
+ });
158
+ return;
159
+ }
160
+ if (inlineContent && isNewInlineContentCall(node)) {
161
+ analyzeNewInlineContentCall(node, {
162
+ onInlineContent,
163
+ });
164
+ return;
165
+ }
166
+ if (inlineContent && isNewBlobCall(node)) {
167
+ analyzeNewBlobCall(node, {
168
+ onInlineContent,
169
+ });
170
+ return;
134
171
  }
135
172
  },
136
- })
137
- return jsUrls
138
- }
173
+ });
174
+ return jsUrls;
175
+ };
@@ -1,10 +1,10 @@
1
- import { createJsParseError } from "./js_parse_error.js"
1
+ import { createJsParseError } from "./js_parse_error.js";
2
2
 
3
- let AcornParser
4
- let _getLineInfo
3
+ let AcornParser;
4
+ let _getLineInfo;
5
5
 
6
6
  export const parseJsWithAcorn = async ({ js, url, isJsModule }) => {
7
- await initAcornParser()
7
+ await initAcornParser();
8
8
  try {
9
9
  // https://github.com/acornjs/acorn/tree/master/acorn#interface
10
10
  const jsAst = AcornParser.parse(js, {
@@ -12,30 +12,30 @@ export const parseJsWithAcorn = async ({ js, url, isJsModule }) => {
12
12
  allowAwaitOutsideFunction: true,
13
13
  sourceType: isJsModule ? "module" : "script",
14
14
  ecmaVersion: 2022,
15
- })
16
- return jsAst
15
+ });
16
+ return jsAst;
17
17
  } catch (e) {
18
18
  if (e && e.name === "SyntaxError") {
19
- const { line, column } = _getLineInfo(js, e.raisedAt)
19
+ const { line, column } = _getLineInfo(js, e.raisedAt);
20
20
  throw createJsParseError({
21
21
  message: e.message,
22
22
  reasonCode: e.message,
23
23
  url,
24
24
  line,
25
25
  column,
26
- })
26
+ });
27
27
  }
28
- throw e
28
+ throw e;
29
29
  }
30
- }
30
+ };
31
31
 
32
32
  const initAcornParser = async () => {
33
33
  if (AcornParser) {
34
- return
34
+ return;
35
35
  }
36
- const { Parser, getLineInfo } = await import("acorn")
37
- const { importAssertions } = await import("acorn-import-assertions")
36
+ const { Parser, getLineInfo } = await import("acorn");
37
+ const { importAssertions } = await import("acorn-import-assertions");
38
38
 
39
- AcornParser = Parser.extend(importAssertions)
40
- _getLineInfo = getLineInfo
41
- }
39
+ AcornParser = Parser.extend(importAssertions);
40
+ _getLineInfo = getLineInfo;
41
+ };
package/src/main.js CHANGED
@@ -4,41 +4,41 @@ export {
4
4
  stringifyHtmlAst,
5
5
  parseSvgString,
6
6
  stringifySvgAst,
7
- } from "./html/html_parse.js"
8
- export { visitHtmlNodes, findHtmlNode } from "./html/html_search.js"
9
- export { analyzeScriptNode, analyzeLinkNode } from "./html/html_analysis.js"
7
+ } from "./html/html_parse.js";
8
+ export { visitHtmlNodes, findHtmlNode } from "./html/html_search.js";
9
+ export { analyzeScriptNode, analyzeLinkNode } from "./html/html_analysis.js";
10
10
  export {
11
11
  getHtmlNodeText,
12
12
  removeHtmlNodeText,
13
13
  setHtmlNodeText,
14
- } from "./html/html_node_text.js"
15
- export { parseSrcSet, stringifySrcSet } from "./html/html_src_set.js"
14
+ } from "./html/html_node_text.js";
15
+ export { parseSrcSet, stringifySrcSet } from "./html/html_src_set.js";
16
16
  export {
17
17
  getHtmlNodeAttribute,
18
18
  setHtmlNodeAttributes,
19
- } from "./html/html_node_attributes.js"
19
+ } from "./html/html_node_attributes.js";
20
20
  export {
21
21
  removeHtmlNode,
22
22
  createHtmlNode,
23
23
  injectHtmlNode,
24
24
  injectHtmlNodeAsEarlyAsPossible,
25
25
  insertHtmlNodeAfter,
26
- } from "./html/html_node.js"
26
+ } from "./html/html_node.js";
27
27
  export {
28
28
  inlineScriptNode,
29
29
  inlineLinkStylesheetNode,
30
30
  inlineImgNode,
31
- } from "./html/html_inlining.js"
31
+ } from "./html/html_inlining.js";
32
32
  export {
33
33
  getHtmlNodePosition,
34
34
  getHtmlNodeAttributePosition,
35
- } from "./html/html_node_position.js"
35
+ } from "./html/html_node_position.js";
36
36
 
37
37
  // css
38
- export { parseCssUrls } from "./css/parse_css_urls.js"
38
+ export { parseCssUrls } from "./css/parse_css_urls.js";
39
39
 
40
40
  // js
41
- export { applyBabelPlugins } from "./js/apply_babel_plugins.js"
42
- export { injectJsImport } from "./js/babel_utils.js"
43
- export { parseJsWithAcorn } from "./js/parse_js_with_acorn.js"
44
- export { parseJsUrls } from "./js/parse_js_urls.js"
41
+ export { applyBabelPlugins } from "./js/apply_babel_plugins.js";
42
+ export { injectJsImport } from "./js/babel_utils.js";
43
+ export { parseJsWithAcorn } from "./js/parse_js_with_acorn.js";
44
+ export { parseJsUrls } from "./js/parse_js_urls.js";
package/src/require.js CHANGED
@@ -1,3 +1,3 @@
1
- import { createRequire } from "node:module"
1
+ import { createRequire } from "node:module";
2
2
 
3
- export const require = createRequire(import.meta.url)
3
+ export const require = createRequire(import.meta.url);