From e86fd0907164572ae96a064d0d4e9b3d7fe7f85a Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Tue, 9 Apr 2024 21:29:11 -0400 Subject: [PATCH] import attributes syntax parsing --- package-lock.json | 18 ++++++++++++++++-- package.json | 1 + src/jsx-loader.js | 5 +++-- src/wcc.js | 9 +++++---- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index deee35a..0321637 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,18 @@ { "name": "wc-compiler", - "version": "0.11.0", + "version": "0.12.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "wc-compiler", - "version": "0.11.0", + "version": "0.12.1", "license": "MIT", "dependencies": { "@projectevergreen/acorn-jsx-esm": "~0.1.0", "@projectevergreen/escodegen-esm": "~0.1.0", "acorn": "^8.7.0", + "acorn-import-attributes": "^1.9.5", "acorn-walk": "^8.2.0", "parse5": "^6.0.1" }, @@ -586,6 +587,14 @@ "node": ">=0.4.0" } }, + "node_modules/acorn-import-attributes": { + "version": "1.9.5", + "resolved": "https://registry.npmjs.org/acorn-import-attributes/-/acorn-import-attributes-1.9.5.tgz", + "integrity": "sha512-n02Vykv5uA3eHGM/Z2dQrcD56kL8TyDb2p1+0P83PClMnC/nc+anbQRhIOWnSq4Ke/KvDPrY3C9hDtC/A3eHnQ==", + "peerDependencies": { + "acorn": "^8" + } + }, "node_modules/acorn-jsx": { "version": "5.3.2", "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", @@ -8916,6 +8925,11 @@ } } }, + "acorn-import-attributes": { + "version": "1.9.5", + "resolved": "https://registry.npmjs.org/acorn-import-attributes/-/acorn-import-attributes-1.9.5.tgz", + "integrity": "sha512-n02Vykv5uA3eHGM/Z2dQrcD56kL8TyDb2p1+0P83PClMnC/nc+anbQRhIOWnSq4Ke/KvDPrY3C9hDtC/A3eHnQ==" + }, "acorn-jsx": { "version": "5.3.2", "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", diff --git a/package.json b/package.json index 7452a0d..84474a2 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "@projectevergreen/acorn-jsx-esm": "~0.1.0", "@projectevergreen/escodegen-esm": "~0.1.0", "acorn": "^8.7.0", + "acorn-import-attributes": "^1.9.5", "acorn-walk": "^8.2.0", "parse5": "^6.0.1" }, diff --git a/src/jsx-loader.js b/src/jsx-loader.js index 4012fe9..e1cf2a6 100644 --- a/src/jsx-loader.js +++ b/src/jsx-loader.js @@ -6,6 +6,7 @@ import { generate } from '@projectevergreen/escodegen-esm'; import fs from 'fs'; import jsx from '@projectevergreen/acorn-jsx-esm'; import { parse, parseFragment, serialize } from 'parse5'; +import { importAttributes } from 'acorn-import-attributes'; const jsxRegex = /\.(jsx)$/; @@ -29,7 +30,7 @@ export function getParser(moduleURL) { } return { - parser: acorn.Parser.extend(jsx()), + parser: acorn.Parser.extend(jsx(), importAttributes), config: { // https://github.com/acornjs/acorn/issues/829#issuecomment-1172586171 ...walk.base, @@ -236,7 +237,7 @@ export function parseJsx(moduleURL) { const hasOwnObservedAttributes = undefined; let inferredObservability = false; let observedAttributes = []; - let tree = acorn.Parser.extend(jsx()).parse(moduleContents, { + let tree = acorn.Parser.extend(jsx(), importAttributes).parse(moduleContents, { ecmaVersion: 'latest', sourceType: 'module' }); diff --git a/src/wcc.js b/src/wcc.js index 72088df..b8de6b7 100644 --- a/src/wcc.js +++ b/src/wcc.js @@ -7,6 +7,7 @@ import * as walk from 'acorn-walk'; import { generate } from '@projectevergreen/escodegen-esm'; import { getParser, parseJsx } from './jsx-loader.js'; import { parse, parseFragment, serialize } from 'parse5'; +import { importAttributes } from 'acorn-import-attributes'; import fs from 'fs'; function getParse(html) { @@ -61,12 +62,12 @@ function registerDependencies(moduleURL, definitions, depth = 0) { const moduleContents = fs.readFileSync(moduleURL, 'utf-8'); const nextDepth = depth += 1; const customParser = getParser(moduleURL); - const parser = customParser ? customParser.parser : acorn; + const parser = customParser ? customParser.parser : acorn.Parser; const config = customParser ? customParser.config : { ...walk.base }; - walk.simple(parser.parse(moduleContents, { + walk.simple(parser.extend(importAttributes).parse(moduleContents, { ecmaVersion: 'latest', sourceType: 'module' }), { @@ -106,13 +107,13 @@ function registerDependencies(moduleURL, definitions, depth = 0) { async function getTagName(moduleURL) { const moduleContents = await fs.promises.readFile(moduleURL, 'utf-8'); const customParser = getParser(moduleURL); - const parser = customParser ? customParser.parser : acorn; + const parser = customParser ? customParser.parser : acorn.Parser; const config = customParser ? customParser.config : { ...walk.base }; let tagName; - walk.simple(parser.parse(moduleContents, { + walk.simple(parser.extend(importAttributes).parse(moduleContents, { ecmaVersion: 'latest', sourceType: 'module' }), {