foliojs / fontkit

An advanced font engine for Node and the browser
1.45k stars 213 forks source link

ReferenceError: Cannot access 'c3x' before initialization #282

Closed fstrube closed 2 years ago

fstrube commented 2 years ago

Version 1.9.0-2.0.2 introduced a regression in the parse() function. I'm getting the following error and stack trace with the attached OTF font. It works fine in 1.8.1.

/test-fontkit/2.0.2/node_modules/fontkit/dist/main.cjs:11520
                                    c3x = c2x + stack.shift();
                                        ^

ReferenceError: Cannot access 'c3x' before initialization
    at parse (/test-fontkit/2.0.2/node_modules/fontkit/dist/main.cjs:11520:41)
    at $7ee0705195f3b047$export$2e2bcd8739ae039._getPath (/test-fontkit/2.0.2/node_modules/fontkit/dist/main.cjs:11570:9)
    at $7ee0705195f3b047$export$2e2bcd8739ae039.get path (/test-fontkit/2.0.2/node_modules/fontkit/dist/main.cjs:10750:21)
    at $7ee0705195f3b047$export$2e2bcd8739ae039.descriptor.get (/test-fontkit/2.0.2/node_modules/fontkit/dist/main.cjs:92:29)
    at $7ee0705195f3b047$export$2e2bcd8739ae039._getCBox (/test-fontkit/2.0.2/node_modules/fontkit/dist/main.cjs:10685:21)
    at $7ee0705195f3b047$export$2e2bcd8739ae039.get cbox (/test-fontkit/2.0.2/node_modules/fontkit/dist/main.cjs:10735:21)
    at $7ee0705195f3b047$export$2e2bcd8739ae039.descriptor.get (/test-fontkit/2.0.2/node_modules/fontkit/dist/main.cjs:92:29)
    at $7ee0705195f3b047$export$2e2bcd8739ae039._getMetrics (/test-fontkit/2.0.2/node_modules/fontkit/dist/main.cjs:10706:72)
    at $7ee0705195f3b047$export$2e2bcd8739ae039.get advanceWidth (/test-fontkit/2.0.2/node_modules/fontkit/dist/main.cjs:10764:21)
    at $7ee0705195f3b047$export$2e2bcd8739ae039.descriptor.get (/test-fontkit/2.0.2/node_modules/fontkit/dist/main.cjs:92:29)

KeplerStd-Bold.otf.zip

Use the following repo to reproduce: https://github.com/fstrube/fontkit-test

fstrube commented 2 years ago

@devongovett

After a bit of digging, the breaking change occurred in 1.9.0 because the build was switched from Babel to Parcel. The ReferenceError is due to a situation that arises when using let declarations inside a switch statement (see the MDN reference for let declarations).

In 1.8.1 and earlier, Babel transpiled let declarations to var declarations, so a ReferenceError was not thrown. But now, with Parcel building the code, the let declarations are left intact, thus the introduction of the ReferenceError.

I've submitted a fix in PR #286