Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Avoid sharing list of previously added imports. #355

Merged
merged 3 commits into from
Mar 12, 2021

Conversation

rwjblue
Copy link
Member

@rwjblue rwjblue commented Mar 12, 2021

The test added here shows the underlying problem, when transpiling multiple files with a shared babel configuration (which is very normal), you would end up with failures if any transformation fails (all future transformations would continue to fail):

TypeError: /Users/rjackson/src/ember-cli/babel-plugin-htmlbars-inline-precompile/foo-bar.j s: Cannot read property 'reference' of undefined

  14 |   for (let ref of refPaths) {
  15 |     let binding = ref.scope.getBinding(ref.node.name);
> 16 |     binding.reference(ref);
     |             ^
  17 |   }
  18 | };
  19 |

  at reference (src/util.js:16:13)
  at registerRefs (index.js:143:7)
  at PluginPass.replacePath (index.js:383:7)
  at newFn (node_modules/@babel/traverse/lib/visitors.js:175:21)

The fundamental issue here is that we shared the list of previously imported modules so that we can make sure their import bindings have been rewritten to use the babel-plugin-ember-modules-api-polyfill transformation. Unfortunately, we tracked that state in the closure state of the babel plugin. That babel plugin closure state is only evaluated / created once not once per-file.

This changes things to track the module imports that have been created on the local state (which is reset properly per "transpilation attempt").

rwjblue added 2 commits March 11, 2021 20:07
…le files

```
    TypeError: /Users/rjackson/src/ember-cli/babel-plugin-htmlbars-inline-precompile/foo-bar.js: C
annot read property 'reference' of undefined

      14 |   for (let ref of refPaths) {
      15 |     let binding = ref.scope.getBinding(ref.node.name);
    > 16 |     binding.reference(ref);
         |             ^
      17 |   }
      18 | };
      19 |

      at reference (src/util.js:16:13)
      at registerRefs (index.js:143:7)
      at PluginPass.replacePath (index.js:383:7)
      at newFn (node_modules/@babel/traverse/lib/visitors.js:175:21)

```

Simply guarding for `ref.scope.getBinding(...)` returning `undefined` is
not "good enough".
The types allow for returning `undefined`, but we always assumed it
would return a reference binding.

Note: this does not fix the underlying tests, they now fail to add the
module imports for `createTemplateFactory`:

```
 FAIL  __tests__/tests.js
  ● htmlbars-inline-precompile › does not error when transpiling multiple modules with a single pl
ugin config

    expect(received).toMatchInlineSnapshot(snapshot)

    Snapshot name: `htmlbars-inline-precompile does not error when transpiling multiple modules wi
th a single plugin config 2`

    - Snapshot  - 2
    + Received  + 0

    - import { createTemplateFactory as _createTemplateFactory } from "@ember/template-factory";
    -
      var compiled = _createTemplateFactory(
      /*
        hello
      */
      "precompiled(hello)");

      84 |     );
      85 |
    > 86 |     expect(transpiled).toMatchInlineSnapshot(`
         |                        ^
      87 |       "import { createTemplateFactory as _createTemplateFactory } from \\"@ember/templa
te-factory\\";
      88 |
      89 |       var compiled = _createTemplateFactory(

      at Object.toMatchInlineSnapshot (__tests__/tests.js:86:24)
```
@rwjblue rwjblue added the bug label Mar 12, 2021
@rwjblue rwjblue requested a review from pzuraq March 12, 2021 01:29
Creating `allAddedImports` in plugin scope "bleeds" across repeated
transpilations. This babel plugin function is invoked once for each
configuration of babel plugins, **not** once per transpilation attempt.

This moves `allAddedImports` onto `state` so that it is reset between
transpilation attempts.
@rwjblue rwjblue force-pushed the avoid-shared-mutable-state branch from 8747af0 to 8a5738c Compare March 12, 2021 01:32
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants