-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
128 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,68 @@ | ||
import { isCodeValid } from '../../src/validate-output'; | ||
|
||
describe('isCodeValid', () => { | ||
it('should return isValid: true when no import/export statements are present', () => { | ||
const code = ` | ||
// Some comment | ||
const x = 5; | ||
function myFunc() { /* ... */ } | ||
`; | ||
|
||
const result = isCodeValid(code); | ||
expect(result.isValid).toBe(true); | ||
}); | ||
|
||
it('should return isValid: false and the match when an import statement is present', () => { | ||
const code = ` | ||
import something from 'moduleA'; | ||
// Other code | ||
`; | ||
|
||
const result = isCodeValid(code); | ||
expect(result.isValid).toBe(false); | ||
expect(result.match).toEqual(expect.arrayContaining([ | ||
expect.stringMatching(/import something from 'moduleA'/) | ||
])); | ||
}); | ||
|
||
it('should return isValid: false and the match when an export statement is present', () => { | ||
const code = ` | ||
// Comment | ||
export { default as MyComponent } from 'moduleB'; | ||
`; | ||
|
||
const result = isCodeValid(code); | ||
expect(result.isValid).toBe(false); | ||
expect(result.match).toEqual(expect.arrayContaining([ | ||
expect.stringMatching(/export { default as MyComponent } from 'moduleB'/) | ||
])); | ||
}); | ||
|
||
it('should handle multiple import/export statements', () => { | ||
const code = ` | ||
import foo from 'moduleX'; | ||
// Some comment | ||
export const bar = 10; | ||
export * from 'moduleY'; | ||
`; | ||
|
||
const result = isCodeValid(code); | ||
expect(result.isValid).toBe(false); | ||
expect(result.match).toEqual(expect.arrayContaining([ | ||
expect.stringMatching(/import foo from 'moduleX'/), | ||
])); | ||
}); | ||
|
||
it('should allow import/export statements within comments', () => { | ||
const code = ` | ||
// This is a comment | ||
/* Multiline comment with an export: | ||
export const something = 5; | ||
*/ | ||
const x = 1; | ||
`; | ||
|
||
const result = isCodeValid(code); | ||
expect(result.isValid).toBe(true); | ||
}); | ||
}); |
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 @@ | ||
const importsRegex = /^(?:\s*(?:\/\/.*\s*)*)*(import .* from '.*'| export .* from '.*')/; | ||
|
||
export const isCodeValid = (code: string) => { | ||
const match = code.match(importsRegex); | ||
return { | ||
isValid: !match?.length, | ||
match, | ||
} | ||
} |