Skip to content

Commit

Permalink
import attributes syntax parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Apr 12, 2024
1 parent ef5bb27 commit 4d85d8b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
18 changes: 16 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
5 changes: 3 additions & 2 deletions src/jsx-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)$/;

Expand All @@ -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,
Expand Down Expand Up @@ -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'
});
Expand Down
9 changes: 5 additions & 4 deletions src/wcc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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'
}), {
Expand Down Expand Up @@ -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'
}), {
Expand Down

0 comments on commit 4d85d8b

Please sign in to comment.