question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Assignment of text to JavaScript asset produces invalid code

See original GitHub issue

Hey,

I’ve got a bit of a weird situation - I use AssetGraph to load JavaScript assets associated with HTML pages. There is a rewriting process that I do on the text of these assets, and then I serialize them back out.

In the process of doing that I think I’ve found a bit of a nasty issue where an assignment to the text property causes broken code to be produced which is then returned on the next read of the asset .text property, and it seems specifically to do with some bad treatment of a comment causing a line break to be added after a return and when combined with ASI… I have included demo code below.

With the following placed in a file called asset.js:

const MyComponent = () => {
    return /*#__PURE__*/React.createElement(Provider, {
      store: services.store
    }, /*#__PURE__*/React.createElement(Router, {
      routes: routes,
      history: createBrowserHistory()
    }, /*#__PURE__*/React.createElement(App, null)));
};

Alongside the above I place the following demo file:

const AssetGraph = require('assetgraph');

const ag = new AssetGraph({root: "."});

(async () => {
    const jsAsset = (await ag.loadAssets("/asset.js"))[0];
    console.log(jsAsset.text); // JS still fine
    jsAsset.text = jsAsset.text; // re-assign the propety
    console.log(jsAsset.text); // JS not fine
})();

When executed, the first console log outputs:

const MyComponent = () => {
    return /*#__PURE__*/React.createElement(Provider, {
      store: services.store
    }, /*#__PURE__*/React.createElement(Router, {
      routes: routes,
      history: createBrowserHistory()
    }, /*#__PURE__*/React.createElement(App, null)));
};

but the second results in:

const MyComponent = () => {
    return /*#__PURE__*/
    React.createElement(Provider, { store: services.store }, /*#__PURE__*/
    React.createElement(Router, {
        routes: routes,
        history: createBrowserHistory()
    }, /*#__PURE__*/
    React.createElement(App, null)));
};

In this case the addition of the newline breaks the code by causing the function which itself is react component to return undefined. It seems like something different is happening when the property is assigned versus when the asset is created, but I’m too unfamiliar with the internals to have a clue about what the cause might be.

Please let me know if there is anything else I can do to help diagnose this.

Thanks!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
alexjeffburkecommented, Jul 31, 2021

I think the weird extra situation was at least partially a result of me calling .markDirty() myself. When I understood what the comment in https://github.com/assetgraph/assetgraph/commit/85b65c15fcc46cad55b4c63e379e1adc896a6a86 was saying it’s that marking dirty clears the cached representations, i.e. _text. I made the associated change my side (https://github.com/alexjeffburke/relivestyle/commit/4d9c0842f6810f02a1a3002de890cf1c66126fbd) and have shipped it so consider this closed.

Thank you again!

0reactions
alexjeffburkecommented, Jul 15, 2021

Thanks so much for doing that, it really helps 😃

There’s a still a little piece of behaviour that makes newly instantiated JS assets and those that have been interacted with different, and the best proposal I’ve got to address it (if only for the purposes of prompting a discussion) here: https://github.com/assetgraph/assetgraph/pull/1182

Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: invalid assignment left-hand side - MDN Web Docs
The JavaScript exception "invalid assignment left-hand side" occurs when there was an unexpected assignment somewhere.
Read more >
Building does not set relative paths to the assets in `static` folder
Ensure that your static resources do not reside within directories "public/js" or "public/css". I chose "public/font" for the fonts in the case per...
Read more >
How to avoid no-param-reassign when setting a property on a ...
If you assign to a parameter and then try and access some of the parameters via the arguments object, it can lead to...
Read more >
Top 10 JavaScript errors from 1000+ projects (and how to ...
To give back to our community of developers, we looked at our database of thousands of projects and found the top 10 errors...
Read more >
Practical Ways to Write Better JavaScript - Stack Overflow Blog
But instead of resulting in valid JavaScript, this code results in invalid ... TypeScript makes team architecture communication easier.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found