Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

flow-factor

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flow-factor - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

46

lib/main.js

@@ -8,2 +8,3 @@ 'use strict';

exports.expressionsAt = expressionsAt;
exports.extractVariable = extractVariable;

@@ -32,11 +33,11 @@ var _babylon = require('babylon');

function getExpressions(ast) {
var expressions = [];
function getPaths(ast) {
var paths = [];
(0, _babelTraverse2.default)(ast, {
enter: function enter(path) {
if (t.isExpression(path.node)) expressions.push(path.node);
if (t.isExpression(path.node)) paths.push(path);
}
});
return expressions;
return paths;
}

@@ -48,2 +49,8 @@

function getExpressions(paths) {
return paths.map(function (path) {
return path.node;
});
}
function normalizeSelection(selection) {

@@ -58,3 +65,3 @@ return {

var normalizedSelection = normalizeSelection(selection);
var allExpressions = getExpressions(ast);
var allExpressions = getExpressions(getPaths(ast));

@@ -83,2 +90,31 @@ function isOneLineExpression(expression) {

});
}
function pathAt(ast, selection) {
var normalizedSelection = normalizeSelection(selection);
var allPaths = getPaths(ast);
function isSearchedPath(path) {
var expressionPosition = path.node.loc;
return expressionPosition.start.column === normalizedSelection.start.column && expressionPosition.start.line === normalizedSelection.start.line && expressionPosition.end.column === normalizedSelection.end.column && expressionPosition.end.line === normalizedSelection.end.line;
}
return (0, _lodash.filter)(allPaths, isSearchedPath)[0];
}
function extractVariable(ast, selection, code) {
var path = pathAt(ast, selection);
if (path) {
var extractedCode = code.slice(path.node.start, path.node.end);
var closestStatement = path.parentPath;
while (!closestStatement.node.body) {
closestStatement = closestStatement.parentPath;
}
var statementPositon = closestStatement.node.loc.start;
var identifier = '_ref';
var insertedCode = '\nvar ' + identifier + ' = ' + extractedCode + '\n';
var operations = [{ type: 'replace', selection: selection, code: identifier }, { type: 'add', statementPositon: statementPositon, code: insertedCode }];
return operations;
}
}

2

package.json
{
"name": "flow-factor",
"version": "0.0.2",
"version": "0.0.3",
"description": "Refactoring tool based on the flow ast.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -11,9 +11,9 @@ // @flow

function getExpressions (ast: Object) {
let expressions = []
function getPaths (ast: Object) {
let paths = []
traverse(ast, {
enter (path) { if (t.isExpression(path.node)) expressions.push(path.node) }
enter (path) { if (t.isExpression(path.node)) paths.push(path) }
})
return expressions
return paths
}

@@ -28,2 +28,6 @@

function getExpressions (paths) {
return paths.map((path) => path.node)
}
function normalizeSelection (selection: selection) {

@@ -38,3 +42,3 @@ return {

const normalizedSelection = normalizeSelection(selection)
const allExpressions = getExpressions(ast)
const allExpressions = getExpressions(getPaths(ast))

@@ -72,1 +76,38 @@ function isOneLineExpression (expression) {

}
function pathAt (ast: Object, selection: selection) {
const normalizedSelection = normalizeSelection(selection)
const allPaths = getPaths(ast)
function isSearchedPath (path) {
const expressionPosition = path.node.loc
return (
expressionPosition.start.column === normalizedSelection.start.column &&
expressionPosition.start.line === normalizedSelection.start.line &&
expressionPosition.end.column === normalizedSelection.end.column &&
expressionPosition.end.line === normalizedSelection.end.line
)
}
return filter(allPaths, isSearchedPath)[0]
}
export function extractVariable (ast: Object, selection: selection, code: string) {
const path = pathAt(ast, selection)
if (path) {
const extractedCode = code.slice(path.node.start, path.node.end)
let closestStatement = path.parentPath
while (!closestStatement.node.body) {
closestStatement = closestStatement.parentPath
}
const statementPositon = closestStatement.node.loc.start
const identifier = '_ref'
const insertedCode = `\nvar ${identifier} = ${extractedCode}\n`
const operations = [
{type: 'replace', selection, code: identifier},
{type: 'add', statementPositon, code: insertedCode}
]
return operations
}
}
// @flow
import {
parse,
expressionsAt
expressionsAt,
extractVariable
} from '../src/main'

@@ -39,1 +40,10 @@ import * as exampleCode from './fixtures'

})
describe('extractVariable', () => {
test('returns an array of diffs needed to be made to extract variable', () => {
const location = {start: {line: 0, column: 0}, end: {line: 0, column: 1}}
const code = exampleCode.binaryExpression
const ast = parse(code)
expect(extractVariable(ast, location, code)).toMatchSnapshot()
})
})

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