Skip to content

Commit

Permalink
validate output
Browse files Browse the repository at this point in the history
  • Loading branch information
carmelc committed Aug 28, 2024
1 parent 75fef8d commit 3160098
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 14 deletions.
59 changes: 45 additions & 14 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions __tests__/unit/validate-output.spec.ts
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);
});
});
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import fs from 'fs-extra';
import { CdnType } from './consts';
import { CDN } from './cdn-impl/cdn-base';
import { cdnFactory } from './cdn-factory';
import { isCodeValid } from './validate-output';

export { CdnType, CDN };

Expand Down Expand Up @@ -64,6 +65,11 @@ export async function bundleOnce(packageName: string, packageVersion: string, ou
const result = await bundle.write({file: outputFilePath});

const outputCode = result.output[0].code;
const { isValid, match } = isCodeValid(outputCode);
if (!isValid) {
console.error(`Invalid output code for ${packageName}@${packageVersion}`, match?.[0]);
throw new Error(`Invalid output code for ${packageName}@${packageVersion}`);
}
resultCode = outputCode;
});

Expand Down
9 changes: 9 additions & 0 deletions src/validate-output.ts
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,
}
}

0 comments on commit 3160098

Please sign in to comment.