Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(shaker): avoid collision between function name and its param (#1081)
* fix(shaker): avoid collision between function name and its param (fixes #1055) * fix(shaker): incorrect identifier type detection for void expressions
- Loading branch information
Showing
15 changed files
with
177 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@linaria/shaker': patch | ||
'@linaria/utils': patch | ||
--- | ||
|
||
Fix an incorrect dead-code detection when a function has a parameter with the same name as the function itself. Fixes #1055 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@linaria/shaker': patch | ||
'@linaria/utils': patch | ||
--- | ||
|
||
Fix rare use case when `void`-expression causes too aggressive tree-shaking. Fixes #1055. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`preeval should keep getGlobal but remove window-related code 1`] = ` | ||
"function getGlobal() { | ||
if (typeof globalThis !== \\"undefined\\") { | ||
return globalThis; | ||
} | ||
if (typeof window !== \\"undefined\\") {} | ||
if (typeof global !== \\"undefined\\") { | ||
return global; | ||
} | ||
if (typeof self !== \\"undefined\\") { | ||
return self; | ||
} | ||
return mockGlobal; | ||
}" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { join } from 'path'; | ||
|
||
import { transformSync } from '@babel/core'; | ||
import dedent from 'dedent'; | ||
|
||
import { preeval } from '@linaria/babel-preset'; | ||
|
||
const run = (code: TemplateStringsArray) => { | ||
const filename = join(__dirname, 'source.js'); | ||
const formattedCode = dedent(code); | ||
|
||
const transformed = transformSync(formattedCode, { | ||
babelrc: false, | ||
configFile: false, | ||
filename, | ||
plugins: [ | ||
[ | ||
preeval, | ||
{ | ||
evaluate: true, | ||
}, | ||
], | ||
], | ||
}); | ||
|
||
if (!transformed) { | ||
throw new Error(`Something went wrong with ${filename}`); | ||
} | ||
|
||
return { | ||
code: transformed.code, | ||
// metadata: transformed.metadata.__linariaShaker, | ||
}; | ||
}; | ||
|
||
describe('preeval', () => { | ||
it('should keep getGlobal but remove window-related code', () => { | ||
const { code } = run` | ||
function getGlobal() { | ||
if (typeof globalThis !== "undefined") { | ||
return globalThis; | ||
} | ||
if (typeof window !== "undefined") { | ||
return window; | ||
} | ||
if (typeof global !== "undefined") { | ||
return global; | ||
} | ||
if (typeof self !== "undefined") { | ||
return self; | ||
} | ||
return mockGlobal; | ||
} | ||
`; | ||
|
||
expect(code).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { NodePath } from '@babel/traverse'; | ||
|
||
export function getScope(path: NodePath) { | ||
// In some nodes (like FunctionDeclaration) `scope` for `id` returns | ||
// local function scope instead of a scope where function is declared. | ||
return path.key === 'id' && path.parent === path.scope.block | ||
? path.scope.parent | ||
: path.scope; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters