@react-json-templates/compiler
Advanced tools
Comparing version 0.2.0-RC.4 to 0.2.0-RC.5
{ | ||
"name": "@react-json-templates/compiler", | ||
"version": "0.2.0-RC.4", | ||
"version": "0.2.0-RC.5", | ||
"main": "./dist/index.js", | ||
@@ -23,3 +23,3 @@ "types": "./dist/index.d.ts", | ||
}, | ||
"gitHead": "ca4384628278886d4b27d5cc32127033cb2ca81a" | ||
"gitHead": "84be2a0a583db78edf8f8c72580a32cd625fa7ff" | ||
} |
@@ -52,67 +52,2 @@ import * as Analyser from '../analyser' | ||
}) | ||
// it("should call analyzeTemplate for tsx templates files", () => { | ||
// jest.spyOn(Utils, "getHash").mockReturnValue("hash") | ||
// const result: RJTAnalyserResult = { | ||
// type: "Template", | ||
// exports: null | ||
// } | ||
// const cache: RJTCompilerCache = {} | ||
// const spy = jest.spyOn(TemplateAnalyser, 'analyzeTemplate').mockReturnValue(result) | ||
// expect(Analyser.analyze({ filePath: "filePath.rjt.tsx", code: "", ast: null as unknown as File, cache })).toEqual(result) | ||
// expect(spy).toBeCalled() | ||
// expect(cache.hash).toBe(result) | ||
// }) | ||
// it("should call analyzeTemplate for jsx templates files", () => { | ||
// jest.spyOn(Utils, "getHash").mockReturnValue("hash") | ||
// const result: RJTAnalyserResult = { | ||
// type: "Template", | ||
// exports: null | ||
// } | ||
// const cache: RJTCompilerCache = {} | ||
// const spy = jest.spyOn(TemplateAnalyser, 'analyzeTemplate').mockReturnValue(result) | ||
// expect(Analyser.analyze({ filePath: "filePath.rjt.jsx", code: "", ast: null as unknown as File, cache })).toEqual(result) | ||
// expect(spy).toBeCalled() | ||
// expect(cache.hash).toBe(result) | ||
// }) | ||
// it('should use cache for tsx templates files', () => { | ||
// jest.spyOn(Utils, "getHash").mockReturnValue("hash") | ||
// const result: RJTAnalyserResult = { | ||
// type: "Template", | ||
// exports: null | ||
// } | ||
// const cache: RJTCompilerCache = { hash: result } | ||
// const spy = jest.spyOn(TemplateAnalyser, 'analyzeTemplate') | ||
// expect(Analyser.analyze({ filePath: "filePath.rjt.tsx", code: "", ast: null as unknown as File, cache })).toEqual(result) | ||
// expect(spy).not.toBeCalled() | ||
// }) | ||
// it('should use cache for jsx templates files', () => { | ||
// jest.spyOn(Utils, "getHash").mockReturnValue("hash") | ||
// const result: RJTAnalyserResult = { | ||
// type: "Template", | ||
// exports: null | ||
// } | ||
// const cache: RJTCompilerCache = { hash: result } | ||
// const spy = jest.spyOn(TemplateAnalyser, 'analyzeTemplate') | ||
// expect(Analyser.analyze({ filePath: "filePath.rjt.jsx", code: "", ast: null as unknown as File, cache })).toEqual(result) | ||
// expect(spy).not.toBeCalled() | ||
// }) | ||
}) | ||
@@ -146,6 +81,8 @@ | ||
assert( | ||
` | ||
import {Serializable as _Serializable} from '@react-json-templates/core' | ||
export default _Serializable("s1", () => null); | ||
` | ||
export default () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
`, | ||
@@ -163,6 +100,8 @@ { | ||
assert( | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
const variable = Serializable("s1", () => null) | ||
` | ||
const variable = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
@@ -180,8 +119,10 @@ export default variable | ||
assert( | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
` | ||
let variable = () => null | ||
variable = Serializable("s1", () => null) | ||
variable = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
@@ -202,8 +143,15 @@ export default variable | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
let variable = Serializable("s1", () => null) | ||
let variable = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
if(cond) { | ||
variable = Serializable("s1", () => null) | ||
variable = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
} | ||
@@ -222,9 +170,15 @@ | ||
assert( | ||
` | ||
import {Template, Serializable} from '@react-json-templates/core' | ||
let variable = Serializable("s1", () => null) | ||
` | ||
let variable = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
if(cond) { | ||
variable = Serializable("s2", () => null) | ||
variable = () => { | ||
"serializable s2" | ||
return null; | ||
} | ||
} | ||
@@ -241,9 +195,11 @@ | ||
assert( | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
` | ||
let variable = () => null | ||
if(cond) { | ||
variable = Serializable("s1", () => null) | ||
variable = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
} | ||
@@ -261,6 +217,8 @@ | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
let variable = cond | ||
? Serializable("s1", () => null) | ||
? () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
: () => null | ||
@@ -316,5 +274,7 @@ | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
export const s1 = Serializable("s1", () => null) | ||
export const s1 = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
`, | ||
@@ -330,6 +290,8 @@ { | ||
assert( | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
const s1 = Serializable("s1", () => null) | ||
` | ||
const s1 = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
@@ -347,6 +309,8 @@ export { s1 } | ||
assert( | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
const _s1 = Serializable("s1", () => null) | ||
` | ||
const _s1 = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
@@ -366,6 +330,8 @@ export { _s1 as s1 } | ||
assert( | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
const variable = Serializable("s1", () => null) | ||
` | ||
const variable = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
@@ -383,8 +349,14 @@ export const s1 = variable | ||
assert( | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
let variable = Serializable("_s1", () => null) | ||
` | ||
let variable = () => { | ||
"serializable _s1" | ||
return null; | ||
} | ||
variable = Serializable("s1", () => null) | ||
variable = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
@@ -402,8 +374,10 @@ export const s1 = variable | ||
assert( | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
` | ||
let variable = () => null | ||
variable = Serializable("s1", () => null) | ||
variable = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
@@ -421,8 +395,10 @@ export const s1 = variable | ||
assert( | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
` | ||
let variable = () => null | ||
variable = Serializable("s1", () => null) | ||
variable = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
@@ -444,10 +420,16 @@ const s1 = variable | ||
assert( | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
const variable = Serializable("s1", () => null) | ||
` | ||
const variable = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
if(cond) { | ||
variable = Serializable("s1", () => null) | ||
variable = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
} | ||
@@ -465,9 +447,15 @@ export { variable as s1 } | ||
assert( | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
const variable = Serializable("s1", () => null) | ||
` | ||
const variable = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
if(cond) { | ||
variable = Serializable("s2", () => null) | ||
variable = () => { | ||
"serializable s2" | ||
return null; | ||
} | ||
} | ||
@@ -484,9 +472,11 @@ | ||
assert( | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
` | ||
let variable = () => null | ||
if(cond) { | ||
variable = Serializable("s1", () => null) | ||
variable = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
} | ||
@@ -503,9 +493,11 @@ | ||
assert( | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
` | ||
let variable = () => null | ||
variable = cond | ||
? Serializable("s1", () => null) | ||
? () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
: () => null | ||
@@ -522,7 +514,9 @@ | ||
assert( | ||
` | ||
import {Template, Serializable} from '@react-json-templates/core' | ||
` | ||
export const n1 = cond | ||
? Serializable("s1", () => null) | ||
? () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
: () => null | ||
@@ -537,9 +531,15 @@ `, | ||
assert( | ||
` | ||
import {Template, Serializable} from '@react-json-templates/core' | ||
export let variable = Serializable("s1", () => null) | ||
` | ||
export let variable = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
variable = cond | ||
? Serializable("s2", () => null) | ||
? () => { | ||
"serializable s2" | ||
return null; | ||
} | ||
: () => null | ||
@@ -556,7 +556,9 @@ `, | ||
assert( | ||
` | ||
import {Serializable} from '@react-json-templates/core' | ||
` | ||
export const s1 = () => { | ||
"serializable s1" | ||
return null; | ||
} | ||
export const s1 = Serializable("s1", () => null) | ||
const n1 = 5 | ||
@@ -566,7 +568,15 @@ | ||
export let n2 = Serializable("_s2", () => null) | ||
export let n2 = () => { | ||
"serializable _s2" | ||
return null; | ||
} | ||
n2 = () => null | ||
export default Serializable("s3", () => null) | ||
export default () => { | ||
"serializable s3" | ||
return null; | ||
} | ||
`, | ||
@@ -584,90 +594,2 @@ { | ||
}) | ||
// describe(Analyser.analyzeTemplate, () => { | ||
// it("should detect valid templates", () => { | ||
// const code = ` | ||
// import {S1, S2} from "../serializable" | ||
// const a = Math.random(); | ||
// <S1 value={a}> | ||
// <S2 /> | ||
// </S1> | ||
// ` | ||
// const ast = Utils.parseString(code, config) | ||
// const result = Analyser.analyzeTemplate({ | ||
// code, | ||
// ast, | ||
// filePath: "filePath", | ||
// cache: {} | ||
// }) | ||
// expect(result).toEqual({ type: 'Template', exports: null }) | ||
// }) | ||
// it("should throw Syntax error if invalid template", () => { | ||
// const invalidCodes = [ | ||
// ` | ||
// import {S1, S2} from "../serializable" | ||
// const a = Math.random(); | ||
// `, | ||
// ` | ||
// import {S1, S2} from "../serializable" | ||
// const a = Math.random(); | ||
// <S1 value={a}> | ||
// <S2 /> | ||
// </S1> | ||
// const x = 5 | ||
// `, | ||
// ` | ||
// import {S1, S2} from "../serializable" | ||
// export const a = Math.random(); | ||
// <S1 value={a}> | ||
// <S2 /> | ||
// </S1> | ||
// `, | ||
// ` | ||
// import {S1, S2} from "../serializable" | ||
// const a = Math.random(); | ||
// export default a; | ||
// <S1 value={a}> | ||
// <S2 /> | ||
// </S1> | ||
// `, | ||
// ` | ||
// import {S1, S2} from "../serializable" | ||
// const a = Math.random(); | ||
// export * from "../test"; | ||
// <S1 value={a}> | ||
// <S2 /> | ||
// </S1> | ||
// ` | ||
// ] | ||
// invalidCodes.forEach((code) => { | ||
// const ast = Utils.parseString(code, config) | ||
// expect(() => { | ||
// Analyser.analyzeTemplate({ | ||
// filePath: 'filPath', | ||
// code, | ||
// ast, | ||
// cache: {} | ||
// }) | ||
// }) | ||
// .toThrow("invalid syntax") | ||
// }) | ||
// }) | ||
// }) | ||
}) |
@@ -7,7 +7,11 @@ import fs from 'fs' | ||
const S = ` | ||
import {Serializable} from "@react-json-templates/core" | ||
export const S2 = () => { | ||
"serializable S2" | ||
return null | ||
} | ||
export const S2 = Serializable("S2", () => null) | ||
export default Serializable("S1", () => null) | ||
export default () => { | ||
"serializable S1" | ||
return null | ||
} | ||
` | ||
@@ -14,0 +18,0 @@ |
import { type NodePath } from '@babel/traverse' | ||
import type * as types from '@babel/types' | ||
import { type RJTComponentType, type RJTType } from './types' | ||
import * as types from '@babel/types' | ||
import { type RJTComponentType } from './types' | ||
@@ -54,6 +54,2 @@ export const getIdentifierPossibleTypes = ( | ||
export const getRJTTypeFromPath = (path: NodePath<any>): RJTComponentType | null => { | ||
if (path.isCallExpression()) { | ||
return getRJTTypeFromCallExpression(path) | ||
} | ||
if (path.isIdentifier()) { | ||
@@ -63,33 +59,31 @@ return getRJTTypeFromIdentifier(path) | ||
return null | ||
return parseSerializable(path) | ||
} | ||
const getRJTTypeFromCallExpression = (path: NodePath<types.CallExpression>): RJTComponentType | null => { | ||
const callee = path.get('callee') | ||
const parseSerializable = (path: NodePath): RJTComponentType | null => { | ||
if (!path.isFunctionDeclaration() && !path.isArrowFunctionExpression()) { | ||
return null | ||
} | ||
const body = path.node.body | ||
if (!callee.isIdentifier()) { | ||
if (!types.isBlockStatement(body)) { | ||
return null | ||
} | ||
const name = callee.node.name | ||
const binding = callee.scope.getBinding(name) | ||
const serializableDirective = body.directives.find(item => item.value.value.match(/serializable .+/))?.value.value | ||
if (binding == null || !binding?.path.isImportSpecifier()) { | ||
if (serializableDirective == null) { | ||
return null | ||
} | ||
const type = getRJTTypeFromImportSpecifier(binding?.path) | ||
const name = serializableDirective.replace('serializable ', '').trim() | ||
if (type === 'Serializable') { | ||
const name = path.get('arguments')[0] | ||
if (name === '') { | ||
return null | ||
} | ||
if (name?.isStringLiteral()) { | ||
return { | ||
type, | ||
name: name.node.value | ||
} | ||
} | ||
return { | ||
type: 'Serializable', | ||
name | ||
} | ||
return null | ||
} | ||
@@ -105,21 +99,2 @@ | ||
const getRJTTypeFromImportSpecifier = (path: NodePath<types.ImportSpecifier>): RJTType | null => { | ||
const parent = path.parent as types.ImportDeclaration | ||
const source = parent.source.value | ||
if (source !== '@react-json-templates/core') { | ||
return null | ||
} | ||
const imported = path.get('imported') | ||
if (!imported.isIdentifier()) { | ||
return null | ||
} | ||
return ['Serializable'].includes(imported.node.name) | ||
? imported.node.name as RJTType | ||
: null | ||
} | ||
const isConditional = (path: NodePath<any> | null): boolean => { | ||
@@ -126,0 +101,0 @@ if (path == null) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
114673
2598