-
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.
fix: finally real obj parsing in the prebuilder codebase
- Loading branch information
1 parent
42d4c1c
commit 352ffc6
Showing
16 changed files
with
162 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
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
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
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
87 changes: 83 additions & 4 deletions
87
packages/prebuilder/src/lib/__tests__/prebuild.etc.test.ts
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 |
---|---|---|
@@ -1,14 +1,93 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import getRawDataFromBracesDeclaration from '../getRawDataFromBracesDeclaration'; | ||
import { objInnerToObj } from '../etc'; | ||
|
||
describe('objInnerToObj', () => { | ||
it('should return an obj, given a valid obj inner', () => { | ||
const objInner = ` | ||
const initialObj = { foo: 'bar', bar: 'foo' }; | ||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers | ||
const objInner = getRawDataFromBracesDeclaration(JSON.stringify(initialObj), 0); | ||
expect(objInner).not.toBe(null); | ||
expect(objInnerToObj(objInner as string)).toStrictEqual(initialObj); | ||
}); | ||
|
||
it('should return an obj, given a valid obj inner (nested)', () => { | ||
const initialObj = { | ||
baz: { | ||
baz: { | ||
bar: 'foo' | ||
}, | ||
foo: 'bar', | ||
bar: 'foo' | ||
}, | ||
foo: 'bar', | ||
bar: 'foo', | ||
`; | ||
expect(objInnerToObj(objInner)).toStrictEqual({ foo: 'bar', bar: 'foo' }); | ||
bar: 'foo' | ||
}; | ||
|
||
const objInner = getRawDataFromBracesDeclaration( | ||
JSON.stringify(initialObj), | ||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers | ||
0 | ||
); | ||
expect(objInner).not.toBe(null); | ||
expect(objInnerToObj(objInner as string)).toStrictEqual(initialObj); | ||
}); | ||
|
||
it('should return an obj, given a valid obj inner (literals)', () => { | ||
const objInner = "foo: 'bar', bar: 'foo'"; | ||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers | ||
expect(objInnerToObj(objInner as string)).toStrictEqual({ foo: 'bar', bar: 'foo' }); | ||
}); | ||
|
||
it('should return an obj, given a valid obj inner (string literals)', () => { | ||
const objInner = "'foo': 'bar', 'bar': 'foo'"; | ||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers | ||
expect(objInnerToObj(objInner as string)).toStrictEqual({ foo: 'bar', bar: 'foo' }); | ||
}); | ||
|
||
it('should throw, given an invalid obj inner (numeric literals)', () => { | ||
const objInner = "2172183: 'bar', 211838173: 'foo'"; | ||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers | ||
expect(() => objInnerToObj(objInner as string)).toThrow(); | ||
}); | ||
|
||
it('should throw, given an invalid obj inner (int values)', () => { | ||
const objInner = 'foo: 45, bar: 12'; | ||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers | ||
expect(() => objInnerToObj(objInner as string)).toThrow(); | ||
}); | ||
|
||
it('should throw when encountering unsupported value type', () => { | ||
const objInner = 'foo: /regex/'; | ||
expect(() => objInnerToObj(objInner as string)).toThrow(); | ||
}); | ||
|
||
it('should throw, given stupid input (random number)', () => { | ||
const objInner = '88909988799'; | ||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers | ||
expect(() => objInnerToObj(objInner as string)).toThrow(); | ||
}); | ||
|
||
it('should return empty object, given empty string input', () => { | ||
const objInner = ''; | ||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers | ||
expect(objInnerToObj(objInner as string)).toStrictEqual({}); | ||
}); | ||
|
||
it('should throw when JSON parsing fails', () => { | ||
const objInner = 'invalid json'; | ||
expect(() => objInnerToObj(objInner as string)).toThrow(); | ||
}); | ||
|
||
it('should throw when Babel parsing fails', () => { | ||
const objInner = 'foo: bar'; | ||
expect(() => objInnerToObj(objInner as string)).toThrow(); | ||
}); | ||
|
||
it('should return an obj when parsing succeeds', () => { | ||
const objInner = '"foo": "bar"'; | ||
expect(objInnerToObj(objInner as string)).toStrictEqual({ foo: 'bar' }); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,2 +1,49 @@ | ||
// eslint-disable-next-line | ||
export const objInnerToObj = (objInner: string): any => eval('({\n' + objInner + '\n})'); | ||
import type { I18nJSONPart } from 'src/types/Metadatas'; | ||
|
||
import { parse } from '@babel/parser'; | ||
|
||
export function objInnerToObj(objInner: string): I18nJSONPart { | ||
let res = {}; | ||
try { | ||
const obj = JSON.parse('{\n' + objInner + '\n}'); | ||
return obj; | ||
} catch (jsonError) { | ||
try { | ||
const parsedObject = parse(`({${objInner}})`, { | ||
sourceType: 'module', | ||
plugins: [] | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers | ||
if (parsedObject.program.body[0]?.type === 'ExpressionStatement' && parsedObject.program.body[0].expression.type === 'ObjectExpression') { | ||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers | ||
const objExpression = parsedObject.program.body[0].expression; | ||
const obj: I18nJSONPart = objExpression.properties.reduce((accumulator: I18nJSONPart, prop) => { | ||
if (prop.type === 'ObjectProperty') { | ||
let key: string; | ||
if (prop.key.type === 'Identifier') { | ||
key = prop.key.name; | ||
} else if (prop.key.type === 'StringLiteral') { | ||
key = prop.key.value; | ||
} else { | ||
throw new Error(`Unsupported key type: ${prop.key.type}`); | ||
} | ||
|
||
let value: string | null = null; | ||
if (prop.value.type === 'StringLiteral') { | ||
value = prop.value.value; | ||
} else { | ||
throw new Error(`Unsupported value type: ${prop.value.type}`); | ||
} | ||
accumulator[key] = value; | ||
} | ||
return accumulator; | ||
}, {}); | ||
res = obj; | ||
} | ||
} catch (babelError) { | ||
throw babelError; | ||
} | ||
} | ||
return res; | ||
} |
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
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