@jsenv/ast 6.2.14 → 6.2.16

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/ast",
3
- "version": "6.2.14",
3
+ "version": "6.2.16",
4
4
  "description": "Parse and transform HTML, CSS and JS",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -32,7 +32,7 @@
32
32
  "test": "node --conditions=development ./scripts/test.mjs"
33
33
  },
34
34
  "dependencies": {
35
- "@babel/parser": "7.24.8",
35
+ "@babel/parser": "7.25.3",
36
36
  "@jsenv/humanize": "1.2.8",
37
37
  "@jsenv/urls": "2.5.2",
38
38
  "acorn": "8.12.1",
@@ -1,6 +1,5 @@
1
1
  import { fileURLToPath } from "node:url";
2
-
3
- import { createCssParseError } from "./css_parse_error.js";
2
+ import { createParseError } from "../parse_error.js";
4
3
 
5
4
  export const applyPostCss = async ({
6
5
  sourcemaps = "comment",
@@ -34,8 +33,7 @@ export const applyPostCss = async ({
34
33
  };
35
34
  } catch (error) {
36
35
  if (error.name === "CssSyntaxError") {
37
- throw createCssParseError({
38
- message: error.message,
36
+ throw createParseError(error.message, {
39
37
  reasonCode: error.reason,
40
38
  url,
41
39
  line: error.line,
@@ -1,6 +1,6 @@
1
1
  import { generateContentFrame } from "@jsenv/humanize";
2
2
  import { parse, parseFragment, serialize } from "parse5";
3
-
3
+ import { createParseError } from "../parse_error.js";
4
4
  import { insertHtmlNodeAfter, insertHtmlNodeBefore } from "./html_node.js";
5
5
  import {
6
6
  getHtmlNodeAttribute,
@@ -27,19 +27,20 @@ export const parseHtml = ({ html, url, storeOriginalPositions = true }) => {
27
27
  ) {
28
28
  return;
29
29
  }
30
- const htmlParseError = new Error(
30
+ const htmlParseError = createParseError(
31
31
  `Unable to parse HTML; ${parse5Error.code}`,
32
+ {
33
+ reasonCode: parse5Error.code,
34
+ url,
35
+ line: parse5Error.startLine,
36
+ column: parse5Error.startCol,
37
+ contentFrame: generateContentFrame({
38
+ content: html,
39
+ line: parse5Error.startLine,
40
+ column: parse5Error.startCol,
41
+ }),
42
+ },
32
43
  );
33
- htmlParseError.reasonCode = parse5Error.code;
34
- htmlParseError.code = "PARSE_ERROR";
35
- htmlParseError.url = url;
36
- htmlParseError.line = parse5Error.startLine;
37
- htmlParseError.column = parse5Error.startCol;
38
- htmlParseError.contentFrame = generateContentFrame({
39
- content: html,
40
- line: parse5Error.startLine,
41
- column: parse5Error.startCol,
42
- });
43
44
  throw htmlParseError;
44
45
  },
45
46
  });
@@ -4,8 +4,7 @@
4
4
  * - https://bvaughn.github.io/babel-repl
5
5
  */
6
6
  import { fileURLToPath, pathToFileURL } from "node:url";
7
-
8
- import { createJsParseError } from "./js_parse_error.js";
7
+ import { createParseError } from "../parse_error.js";
9
8
 
10
9
  export const applyBabelPlugins = async ({
11
10
  babelPlugins,
@@ -70,8 +69,7 @@ export const applyBabelPlugins = async ({
70
69
  return normalizeResult(result);
71
70
  } catch (error) {
72
71
  if (error && error.code === "BABEL_PARSE_ERROR") {
73
- throw createJsParseError({
74
- message: error.message,
72
+ throw createParseError(error.message, {
75
73
  reasonCode: error.reasonCode,
76
74
  content: input,
77
75
  url: inputUrl,
@@ -1,7 +1,6 @@
1
1
  import { Parser, getLineInfo } from "acorn";
2
2
  import { importAttributes } from "acorn-import-attributes";
3
-
4
- import { createJsParseError } from "./js_parse_error.js";
3
+ import { createParseError } from "../parse_error.js";
5
4
 
6
5
  export const parseJsWithAcorn = ({ js, url, isJsModule }) => {
7
6
  const AcornParser = Parser.extend(importAttributes);
@@ -28,8 +27,7 @@ export const parseJsWithAcorn = ({ js, url, isJsModule }) => {
28
27
  } catch (e) {
29
28
  if (e && e.name === "SyntaxError") {
30
29
  const { line, column } = getLineInfo(js, e.raisedAt);
31
- throw createJsParseError({
32
- message: e.message,
30
+ throw createParseError(e.message, {
33
31
  reasonCode: e.message,
34
32
  content: js,
35
33
  url,
@@ -0,0 +1,18 @@
1
+ export const createParseError = (message, props) => {
2
+ const parseError = new Error(message);
3
+ defineNonEnumerableProperties(parseError, {
4
+ code: "PARSE_ERROR",
5
+ ...props,
6
+ });
7
+ return parseError;
8
+ };
9
+
10
+ const defineNonEnumerableProperties = (error, properties) => {
11
+ for (const key of Object.keys(properties)) {
12
+ Object.defineProperty(error, key, {
13
+ configurable: true,
14
+ writable: true,
15
+ value: properties[key],
16
+ });
17
+ }
18
+ };
@@ -1,15 +0,0 @@
1
- export const createCssParseError = ({
2
- message,
3
- reasonCode,
4
- url,
5
- line,
6
- column,
7
- }) => {
8
- const parseError = new Error(message);
9
- parseError.reasonCode = reasonCode;
10
- parseError.code = "PARSE_ERROR";
11
- parseError.url = url;
12
- parseError.line = line;
13
- parseError.column = column;
14
- return parseError;
15
- };
@@ -1,15 +0,0 @@
1
- export const createJsParseError = ({
2
- message,
3
- reasonCode,
4
- url,
5
- line,
6
- column,
7
- }) => {
8
- const parseError = new Error(message);
9
- parseError.reasonCode = reasonCode;
10
- parseError.code = "PARSE_ERROR";
11
- parseError.url = url;
12
- parseError.line = line;
13
- parseError.column = column;
14
- return parseError;
15
- };