@linaria/utils
Version:
Blazing fast zero-runtime CSS in JS library
145 lines (135 loc) • 3.99 kB
JavaScript
"use strict";
var _core = require("@babel/core");
var _generator = _interopRequireDefault(require("@babel/generator"));
var _traverse = _interopRequireDefault(require("@babel/traverse"));
var _dedent = _interopRequireDefault(require("dedent"));
var _removeDangerousCode = require("../removeDangerousCode");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const run = code => {
const ast = (0, _core.parseSync)((0, _dedent.default)(code), {
filename: 'test.tsx',
presets: ['@babel/preset-typescript', '@babel/preset-react']
});
if (!ast) {
throw new Error('Failed to parse');
}
(0, _traverse.default)(ast, {
Program(path) {
(0, _removeDangerousCode.removeDangerousCode)(path);
}
});
return (0, _generator.default)(ast).code;
};
describe('removeDangerousCode', () => {
it('should be defined', () => {
expect(_removeDangerousCode.removeDangerousCode).toBeDefined();
});
it('should remove `window` but keep `setVersion` function untouched', () => {
const result = run`
let _win = undefined;
try {
_win = window;
} catch (e) {
/* no-op */
}
export function setVersion(packageName, packageVersion) {
if (typeof _win !== 'undefined') {
return null;
}
}
`;
expect(result).toMatchSnapshot();
});
it('should replace body of react component with null', () => {
const result = run`
export var Popup = /*#__PURE__*/function () {
var Popup = function Popup() {
var name = Popup.displayName;
return <div>{name}</div>;
};
Popup.displayName = 'Popup';
return Popup;
}();
`;
expect(result).toMatchSnapshot();
});
it('should simplify ternary operator', () => {
expect(run`
export function getHostname(opts) {
return typeof location !== "undefined" ? location.hostname : "localhost";
}
`).toMatchSnapshot();
expect(run`
export function getHostname(opts) {
return location.hostname ? "location.hostname" : "localhost";
}
`).toMatchSnapshot();
expect(run`
export function getHostname(opts) {
return opts ? "localhost" : location.hostname;
}
`).toMatchSnapshot();
expect(run`
export function getHostname(opts) {
return opts ? location.hostname : "localhost";
}
`).toMatchSnapshot();
});
it('should simplify OR expression', () => {
const result = run`
export function getHostname(opts) {
return opts.hostname || location.hostname;
}
`;
expect(result).toMatchSnapshot();
});
it('should remove code under is-it-browser checks', () => {
const result = run`
let method;
if (typeof window !== 'undefined') {
console.log('it is browser');
method = window.fetch;
}
if (typeof window === 'undefined') {
console.log('it is not browser');
method = fetch;
}
export function testFn(arg) {
method(arg);
}
`;
expect(result).toMatchSnapshot();
});
it('should simplify if statement', () => {
const result = run`
if (typeof window !== 'undefined') {
console.log('it is browser');
}
if (typeof window === 'undefined') {
console.log('it is not browser');
}
if (typeof window === 'undefined') {
console.log('it is not browser');
} else {
console.log('it is browser');
}
if (typeof window !== 'undefined') {
console.log('it is browser');
} else {
console.log('it is not browser');
}
`;
expect(result).toMatchSnapshot();
});
it('should not remove class', () => {
const result = run`
class Test {
constructor() {
window.fetch();
}
}
`;
expect(result).toMatchSnapshot();
});
});
//# sourceMappingURL=removeDangerousCode.test.js.map