Socket
Socket
Sign inDemoInstall

@react-json-templates/compiler

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-json-templates/compiler - npm Package Compare versions

Comparing version 0.2.0-RC.5 to 0.2.0-RC.6

367

dist/__test__/analyser.test.js

@@ -64,48 +64,2 @@ "use strict";

});
// 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()
// })
});

@@ -130,6 +84,8 @@ describe(Analyser.analyzeExports, () => {

it('should detect default Serializable exports', () => {
assert(`
import {Serializable as _Serializable} from '@react-json-templates/core'
export default _Serializable("s1", () => null);
assert(`
export default () => {
"serializable s1"
return null;
}
`, {

@@ -143,6 +99,8 @@ type: 'Exports',

it('should detect default variables export type', () => {
assert(`
import {Serializable} from '@react-json-templates/core'
const variable = Serializable("s1", () => null)
assert(`
const variable = () => {
"serializable s1"
return null;
}

@@ -156,8 +114,10 @@ export default variable

});
assert(`
import {Serializable} from '@react-json-templates/core'
assert(`
let variable = () => null
variable = Serializable("s1", () => null)
variable = () => {
"serializable s1"
return null;
}

@@ -174,8 +134,15 @@ export default variable

assert(`
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;
}
}

@@ -190,9 +157,15 @@

});
assert(`
import {Template, Serializable} from '@react-json-templates/core'
let variable = Serializable("s1", () => null)
assert(`
let variable = () => {
"serializable s1"
return null;
}
if(cond) {
variable = Serializable("s2", () => null)
variable = () => {
"serializable s2"
return null;
}
}

@@ -205,9 +178,11 @@

});
assert(`
import {Serializable} from '@react-json-templates/core'
assert(`
let variable = () => null
if(cond) {
variable = Serializable("s1", () => null)
variable = () => {
"serializable s1"
return null;
}
}

@@ -221,6 +196,8 @@

assert(`
import {Serializable} from '@react-json-templates/core'
let variable = cond
? Serializable("s1", () => null)
? () => {
"serializable s1"
return null;
}
: () => null

@@ -260,5 +237,7 @@

assert(`
import {Serializable} from '@react-json-templates/core'
export const s1 = Serializable("s1", () => null)
export const s1 = () => {
"serializable s1"
return null;
}
`, {

@@ -270,6 +249,8 @@ type: 'Exports',

});
assert(`
import {Serializable} from '@react-json-templates/core'
const s1 = Serializable("s1", () => null)
assert(`
const s1 = () => {
"serializable s1"
return null;
}

@@ -283,6 +264,8 @@ export { s1 }

});
assert(`
import {Serializable} from '@react-json-templates/core'
const _s1 = Serializable("s1", () => null)
assert(`
const _s1 = () => {
"serializable s1"
return null;
}

@@ -298,6 +281,8 @@ export { _s1 as s1 }

it('should detect named variables export type', () => {
assert(`
import {Serializable} from '@react-json-templates/core'
const variable = Serializable("s1", () => null)
assert(`
const variable = () => {
"serializable s1"
return null;
}

@@ -311,8 +296,14 @@ export const s1 = variable

});
assert(`
import {Serializable} from '@react-json-templates/core'
let variable = Serializable("_s1", () => null)
assert(`
let variable = () => {
"serializable _s1"
return null;
}
variable = Serializable("s1", () => null)
variable = () => {
"serializable s1"
return null;
}

@@ -326,8 +317,10 @@ export const s1 = variable

});
assert(`
import {Serializable} from '@react-json-templates/core'
assert(`
let variable = () => null
variable = Serializable("s1", () => null)
variable = () => {
"serializable s1"
return null;
}

@@ -341,8 +334,10 @@ export const s1 = variable

});
assert(`
import {Serializable} from '@react-json-templates/core'
assert(`
let variable = () => null
variable = Serializable("s1", () => null)
variable = () => {
"serializable s1"
return null;
}

@@ -360,10 +355,16 @@ const s1 = variable

it('should not detect named variables export with conditional type', () => {
assert(`
import {Serializable} from '@react-json-templates/core'
const variable = Serializable("s1", () => null)
assert(`
const variable = () => {
"serializable s1"
return null;
}
if(cond) {
variable = Serializable("s1", () => null)
variable = () => {
"serializable s1"
return null;
}
}

@@ -377,9 +378,15 @@ export { variable as s1 }

});
assert(`
import {Serializable} from '@react-json-templates/core'
const variable = Serializable("s1", () => null)
assert(`
const variable = () => {
"serializable s1"
return null;
}
if(cond) {
variable = Serializable("s2", () => null)
variable = () => {
"serializable s2"
return null;
}
}

@@ -392,9 +399,11 @@

});
assert(`
import {Serializable} from '@react-json-templates/core'
assert(`
let variable = () => null
if(cond) {
variable = Serializable("s1", () => null)
variable = () => {
"serializable s1"
return null;
}
}

@@ -407,9 +416,11 @@

});
assert(`
import {Serializable} from '@react-json-templates/core'
assert(`
let variable = () => null
variable = cond
? Serializable("s1", () => null)
? () => {
"serializable s1"
return null;
}
: () => null

@@ -422,7 +433,9 @@

});
assert(`
import {Template, Serializable} from '@react-json-templates/core'
assert(`
export const n1 = cond
? Serializable("s1", () => null)
? () => {
"serializable s1"
return null;
}
: () => null

@@ -433,9 +446,15 @@ `, {

});
assert(`
import {Template, Serializable} from '@react-json-templates/core'
export let variable = Serializable("s1", () => null)
assert(`
export let variable = () => {
"serializable s1"
return null;
}
variable = cond
? Serializable("s2", () => null)
? () => {
"serializable s2"
return null;
}
: () => null

@@ -448,7 +467,9 @@ `, {

it('should detect multiple export types', () => {
assert(`
import {Serializable} from '@react-json-templates/core'
assert(`
export const s1 = () => {
"serializable s1"
return null;
}
export const s1 = Serializable("s1", () => null)
const n1 = 5

@@ -458,7 +479,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;
}
`, {

@@ -474,73 +503,3 @@ type: 'Exports',

});
// 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")
// })
// })
// })
});
//# sourceMappingURL=analyser.test.js.map

@@ -33,7 +33,11 @@ "use strict";

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
}
`;

@@ -40,0 +44,0 @@ const compilerConfig = {

import { type NodePath } from '@babel/traverse';
import type * as types from '@babel/types';
import * as types from '@babel/types';
import { type RJTComponentType } from './types';
export declare const getIdentifierPossibleTypes: (path: NodePath<types.Identifier | types.JSXIdentifier>) => Array<RJTComponentType | null>;
export declare const getRJTTypeFromPath: (path: NodePath<any>) => RJTComponentType | null;
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRJTTypeFromPath = exports.getIdentifierPossibleTypes = void 0;
const types = __importStar(require("@babel/types"));
const getIdentifierPossibleTypes = (path) => {

@@ -44,32 +68,29 @@ const binding = path.scope.getBinding(path.node.name);

const getRJTTypeFromPath = (path) => {
if (path.isCallExpression()) {
return getRJTTypeFromCallExpression(path);
}
if (path.isIdentifier()) {
return getRJTTypeFromIdentifier(path);
}
return null;
return parseSerializable(path);
};
exports.getRJTTypeFromPath = getRJTTypeFromPath;
const getRJTTypeFromCallExpression = (path) => {
const callee = path.get('callee');
if (!callee.isIdentifier()) {
const parseSerializable = (path) => {
var _a;
if (!path.isFunctionDeclaration() && !path.isArrowFunctionExpression()) {
return null;
}
const name = callee.node.name;
const binding = callee.scope.getBinding(name);
if (binding == null || !(binding === null || binding === void 0 ? void 0 : binding.path.isImportSpecifier())) {
const body = path.node.body;
if (!types.isBlockStatement(body)) {
return null;
}
const type = getRJTTypeFromImportSpecifier(binding === null || binding === void 0 ? void 0 : binding.path);
if (type === 'Serializable') {
const name = path.get('arguments')[0];
if (name === null || name === void 0 ? void 0 : name.isStringLiteral()) {
return {
type,
name: name.node.value
};
}
const serializableDirective = (_a = body.directives.find(item => item.value.value.match(/serializable .+/))) === null || _a === void 0 ? void 0 : _a.value.value;
if (serializableDirective == null) {
return null;
}
return null;
const name = serializableDirective.replace('serializable ', '').trim();
if (name === '') {
return null;
}
return {
type: 'Serializable',
name
};
};

@@ -82,16 +103,2 @@ const getRJTTypeFromIdentifier = (path) => {

};
const getRJTTypeFromImportSpecifier = (path) => {
const parent = path.parent;
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
: null;
};
const isConditional = (path) => {

@@ -98,0 +105,0 @@ if (path == null) {

{
"name": "@react-json-templates/compiler",
"version": "0.2.0-RC.5",
"version": "0.2.0-RC.6",
"main": "./dist/index.js",

@@ -23,3 +23,3 @@ "types": "./dist/index.d.ts",

},
"gitHead": "84be2a0a583db78edf8f8c72580a32cd625fa7ff"
"gitHead": "8c7c35d2054b775f309873c3fe55167bad98fbb1"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc