@@ -6,7 +6,7 @@ import type { SourceFiles } from '../../moduleTypes'
66import parseProgramsAndConstructImportGraph from '../linker'
77
88import * as resolver from '../resolver'
9- import { assertTrue } from '../../../utils/testing/misc'
9+ import { assertNodeType , assertTrue } from '../../../utils/testing/misc'
1010jest . spyOn ( resolver , 'default' )
1111
1212beforeEach ( ( ) => {
@@ -136,3 +136,129 @@ test('Linker does tree-shaking', async () => {
136136 expect ( resolver . default ) . not . toHaveBeenCalledWith ( './b.js' )
137137 expect ( Object . keys ( result . programs ) ) . not . toContain ( '/b.js' )
138138} )
139+
140+ test ( 'Linker updates the source paths of Import Declaration nodes' , async ( ) => {
141+ const [ , result ] = await testCode (
142+ {
143+ '/dir0/a.js' : 'export const x = 0;' ,
144+ '/b.js' : `import { x } from "./dir0/a.js";
145+ export { x };
146+ ` ,
147+ '/dir1/c.js' : 'import { x } from "../b.js";' ,
148+ } ,
149+ '/dir1/c.js'
150+ )
151+
152+ assertTrue ( result . ok )
153+ const [ bNode ] = result . programs [ '/b.js' ] . body
154+ assertNodeType ( 'ImportDeclaration' , bNode )
155+ expect ( bNode . source . value ) . toEqual ( '/dir0/a.js' )
156+
157+ const [ cNode ] = result . programs [ '/dir1/c.js' ] . body
158+ assertNodeType ( 'ImportDeclaration' , cNode )
159+ expect ( cNode . source . value ) . toEqual ( '/b.js' )
160+ } )
161+
162+ describe ( 'Test determining verbose errors' , ( ) => {
163+ test ( 'Verbose errors is normally false' , async ( ) => {
164+ const [ , result ] = await testCode ( {
165+ '/a.js' : "const a = 0;"
166+ } , '/a.js' )
167+
168+ assertTrue ( result . ok )
169+ assertTrue ( ! result . verboseErrors )
170+ } )
171+
172+ test ( 'Verbose errors enables normally' , async ( ) => {
173+ const [ , result ] = await testCode ( {
174+ '/a.js' : "'enable verbose';"
175+ } , '/a.js' )
176+
177+ assertTrue ( result . ok )
178+ assertTrue ( result . verboseErrors )
179+ } )
180+
181+ test ( 'Verbose errors does not enable when it is not the entrypoint' , async ( ) => {
182+ const [ , result ] = await testCode ( {
183+ '/a.js' : `
184+ 'enable verbose';
185+ export const a = "a";
186+ ` ,
187+ '/b.js' : "import { a } from './a.js';"
188+ } , '/b.js' )
189+
190+ assertTrue ( result . ok )
191+ assertTrue ( ! result . verboseErrors )
192+ } )
193+
194+ test ( 'Verbose errors does not enable when it is not the first statement' , async ( ) => {
195+ const [ , result ] = await testCode ( {
196+ '/a.js' : `
197+ export const a = "a";
198+ 'enable verbose';
199+ ` ,
200+ } , '/a.js' )
201+
202+ assertTrue ( result . ok )
203+ assertTrue ( ! result . verboseErrors )
204+ } )
205+
206+ test ( 'Verbose errors does not enable when it is an unknown entrypoint' , async ( ) => {
207+ const [ , result ] = await testCode ( {
208+ '/a.js' : `
209+ export const a = "a";
210+ 'enable verbose';
211+ ` ,
212+ } , '/d.js' as any )
213+
214+ assertTrue ( ! result . ok )
215+ assertTrue ( ! result . verboseErrors )
216+ } )
217+
218+ test ( 'Verbose errors enables even if other files have parse errors' , async ( ) => {
219+ const [ { errors } , result ] = await testCode ( {
220+ '/a.js' : `
221+ 'enable verbose';
222+ import { b } from "./b.js";
223+ ` ,
224+ '/b.js' : `export const b = "b"`
225+ } , '/a.js' )
226+
227+ assertTrue ( ! result . ok )
228+ assertTrue ( result . verboseErrors )
229+ expect ( errors . length ) . toEqual ( 1 )
230+ expect ( errors [ 0 ] ) . toBeInstanceOf ( MissingSemicolonError )
231+ } )
232+
233+ test ( 'Verbose errors enables even if the entrypoint has parse errors' , async ( ) => {
234+ const [ { errors } , result ] = await testCode ( {
235+ '/a.js' : `
236+ 'enable verbose';
237+ const x = 0
238+ ` ,
239+ } , '/a.js' )
240+
241+ assertTrue ( ! result . ok )
242+ assertTrue ( result . verboseErrors )
243+ expect ( errors . length ) . toEqual ( 1 )
244+ expect ( errors [ 0 ] ) . toBeInstanceOf ( MissingSemicolonError )
245+ } )
246+
247+ test ( 'Verbose errors enables even if there is a module resolution problem' , async ( ) => {
248+ const [ { errors } , result ] = await testCode ( {
249+ '/a.js' : `
250+ 'enable verbose';
251+ import { b } from "./b.js";
252+ ` ,
253+ '/b.js' : `
254+ import { c } from "./c.js";
255+ export { c as b };
256+ `
257+ } , '/a.js' )
258+
259+ assertTrue ( ! result . ok )
260+ assertTrue ( result . verboseErrors )
261+ expect ( errors . length ) . toEqual ( 1 )
262+ expect ( errors [ 0 ] ) . toBeInstanceOf ( ModuleNotFoundError )
263+ } )
264+ } )
0 commit comments