Skip to content

Commit

Permalink
fix: evaluation of functions with their own nested identifiers (#326)
Browse files Browse the repository at this point in the history
There is a problem with the evaluation of functions like this
```
    const foo = (k) => {
      const obj = objects[k];
      return obj;
    };
```

It will be transformed to
```
      {
        const foo = k => {
          const obj = objects[k];
          return obj;
        };

        {
          const obj = objects[k];
          {
            module.exports = foo('key');
          }
        }
      }
```

As we can see, `obj` was extracted from the function to the global scope.
  • Loading branch information
Anber authored and satya164 committed Feb 27, 2019
1 parent 768e266 commit 6c054f5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/__tests__/__snapshots__/preval.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,39 @@ Dependencies: ../slugify
`;
exports[`evaluates functions with nested identifiers 1`] = `
"import { styled } from 'linaria/react';
const objects = {
key: {
fontSize: 12
}
};
const foo = k => {
const obj = objects[k];
return obj;
};
export const Title =
/*#__PURE__*/
styled(\\"h1\\")({
name: \\"Title\\",
class: \\"Title_t1xha7dm\\"
});"
`;
exports[`evaluates functions with nested identifiers 2`] = `
CSS:
.Title_t1xha7dm {
font-size: 12px;
}
Dependencies: NA
`;
exports[`evaluates identifier in scope 1`] = `
"import { styled } from 'linaria/react';
const answer = 42;
Expand Down
21 changes: 21 additions & 0 deletions src/__tests__/preval.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ it('evaluates local expressions', async () => {
expect(metadata).toMatchSnapshot();
});

it('evaluates functions with nested identifiers', async () => {
const { code, metadata } = await transpile(
dedent`
import { styled } from 'linaria/react';
const objects = { key: { fontSize: 12 } };
const foo = (k) => {
const obj = objects[k];
return obj;
};
export const Title = styled.h1\`
${"${foo('key')}"}
\`;
`
);

expect(code).toMatchSnapshot();
expect(metadata).toMatchSnapshot();
});

it('evaluates expressions with dependencies', async () => {
const { code, metadata } = await transpile(
dedent`
Expand Down
14 changes: 13 additions & 1 deletion src/babel/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@ const generator = require('@babel/generator').default;
const babel = require('@babel/core');
const Module = require('./module');

const isAdded = (requirements, path) => {
if (requirements.some(req => req.path === path)) {
return true;
}

if (path.parentPath) {
return isAdded(requirements, path.parentPath);
}

return false;
};

const resolve = (path, t, requirements) => {
const binding = path.scope.getBinding(path.node.name);

if (
path.isReferenced() &&
binding &&
binding.kind !== 'param' &&
!requirements.some(req => req.path === binding.path)
!isAdded(requirements, binding.path)
) {
let result;

Expand Down

0 comments on commit 6c054f5

Please sign in to comment.