Unverified Commit 04b2f0eb authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Use Real Identifier for SSG Transform (#9656)

parent cbf8e0b2
...@@ -12,7 +12,11 @@ const ssgExports = new Set([ ...@@ -12,7 +12,11 @@ const ssgExports = new Set([
EXPORT_NAME_GET_STATIC_PATHS, EXPORT_NAME_GET_STATIC_PATHS,
]) ])
type PluginState = { refs: Set<any>; isPrerender: boolean; done: boolean } type PluginState = {
refs: Set<NodePath<BabelTypes.Identifier>>
isPrerender: boolean
done: boolean
}
function decorateSsgExport( function decorateSsgExport(
t: typeof BabelTypes, t: typeof BabelTypes,
...@@ -106,7 +110,7 @@ export default function nextTransformSsg({ ...@@ -106,7 +110,7 @@ export default function nextTransformSsg({
) { ) {
const ident = getIdentifier(path) const ident = getIdentifier(path)
if (ident && ident.node && isIdentifierReferenced(ident)) { if (ident && ident.node && isIdentifierReferenced(ident)) {
state.refs.add(ident.node.name) state.refs.add(ident)
} }
} }
...@@ -120,7 +124,7 @@ export default function nextTransformSsg({ ...@@ -120,7 +124,7 @@ export default function nextTransformSsg({
) { ) {
const local = path.get('local') const local = path.get('local')
if (isIdentifierReferenced(local)) { if (isIdentifierReferenced(local)) {
state.refs.add(local.node.name) state.refs.add(local)
} }
} }
...@@ -128,7 +132,7 @@ export default function nextTransformSsg({ ...@@ -128,7 +132,7 @@ export default function nextTransformSsg({
visitor: { visitor: {
Program: { Program: {
enter(_, state) { enter(_, state) {
state.refs = new Set<string>() state.refs = new Set<NodePath<BabelTypes.Identifier>>()
state.isPrerender = false state.isPrerender = false
state.done = false state.done = false
}, },
...@@ -147,7 +151,7 @@ export default function nextTransformSsg({ ...@@ -147,7 +151,7 @@ export default function nextTransformSsg({
if ( if (
ident && ident &&
ident.node && ident.node &&
refs.has(ident.node.name) && refs.has(ident) &&
!isIdentifierReferenced(ident) !isIdentifierReferenced(ident)
) { ) {
++count ++count
...@@ -171,7 +175,7 @@ export default function nextTransformSsg({ ...@@ -171,7 +175,7 @@ export default function nextTransformSsg({
> >
) { ) {
const local = path.get('local') const local = path.get('local')
if (refs.has(local.node.name) && !isIdentifierReferenced(local)) { if (refs.has(local) && !isIdentifierReferenced(local)) {
++count ++count
path.remove() path.remove()
if ( if (
...@@ -195,10 +199,7 @@ export default function nextTransformSsg({ ...@@ -195,10 +199,7 @@ export default function nextTransformSsg({
} }
const local = path.get('id') as NodePath<BabelTypes.Identifier> const local = path.get('id') as NodePath<BabelTypes.Identifier>
if ( if (refs.has(local) && !isIdentifierReferenced(local)) {
refs.has(local.node.name) &&
!isIdentifierReferenced(local)
) {
++count ++count
path.remove() path.remove()
} }
...@@ -224,7 +225,7 @@ export default function nextTransformSsg({ ...@@ -224,7 +225,7 @@ export default function nextTransformSsg({
const local = path.get('id') as NodePath<BabelTypes.Identifier> const local = path.get('id') as NodePath<BabelTypes.Identifier>
if (isIdentifierReferenced(local)) { if (isIdentifierReferenced(local)) {
state.refs.add(path.node.id.name) state.refs.add(local)
} }
}, },
FunctionDeclaration: markFunction, FunctionDeclaration: markFunction,
......
...@@ -308,5 +308,26 @@ describe('babel plugin (next-ssg-transform)', () => { ...@@ -308,5 +308,26 @@ describe('babel plugin (next-ssg-transform)', () => {
`"import keep_me from'hello';import{keep_me2}from'hello2';import*as keep_me3 from'hello3';import{but_not_me}from'bar';var leave_me_alone=1;function dont_bug_me_either(){}const __NEXT_COMP=function Test(){return __jsx(\\"div\\",null);};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"` `"import keep_me from'hello';import{keep_me2}from'hello2';import*as keep_me3 from'hello3';import{but_not_me}from'bar';var leave_me_alone=1;function dont_bug_me_either(){}const __NEXT_COMP=function Test(){return __jsx(\\"div\\",null);};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"`
) )
}) })
it('should not mix up bindings', () => {
const output = babel(trim`
function Function1() {
return {
a: function bug(a) {
return 2;
}
};
}
function Function2() {
var bug = 1;
return { bug };
}
`)
expect(output).toMatchInlineSnapshot(
`"function Function1(){return{a:function bug(a){return 2;}};}function Function2(){var bug=1;return{bug};}"`
)
})
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment