Меняем javascript с помощью javascript

Post on 13-Jan-2017

84 Views

Category:

Software

8 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Меняем JavaScript

с помощью JavaScript

Павел Волокитин

const React = require('react');

import React from 'react';

const React = require('react');

import React from 'react';

/const (\w+) = require\('([^']+')\);/

→ import $1 from '$2';

forEach(arr, function(item) { console.log(item); });

for (const item of arr) { console.log(item); }

define(['react'], function(React) { return {...}; });

const React = require('react');

module.exports = {...};

AMD

CommonJS

Abstract Syntax Tree

1 + 2 + 3

Abstract Syntax Tree

1 + 2 + 3

BinaryExpr +

BinaryExpr +

1 2

3

Abstract Syntax Tree

1 + 2 + 3

BinaryExpr +

BinaryExpr +

1 2

3

console.log(123)

Abstract Syntax Tree

1 + 2 + 3

BinaryExpr +

BinaryExpr +

1 2

3

console.log(123)

CallExpr

MemberExpr

Identifier "console"

Identifier "log"

123

Abstract Syntax Tree

jscodeshift

astexplorer.net

Demo: astexplorer

–Parse –Find –Replace –Print

react-codemod

const Comp = React.createClass({

getDefaultProps() { return {size: 10}; },

getInitialState() { return {focused: false}; },

render() {...}, });

class Comp extends React.Component {

static defaultProps = {size: 10};

state = {focused: false};

render() {...} }

Demo: React class

–Parse –Find –Replace –Print

Demo: nuclide outline Demo: react-docgen Demo: modules

Трудности

–Parse –Find –Replace –Print

–Parse –Find –Replace –Print

recast

function f(a, b) { ... }

function fn( argumentOne, argumentTwo, ) { ... }

obj = { one: 1, two: 2, };

obj = {

func() {},

value: null, };

– recast/lib/printer.js

const React = require('react');

obj = {

one: 1,

two: 2, };

– recast/lib/printer.js–Временные ноды

$$newline;

$$newline,

$$newline,

const React = require('react');

obj = {

one: 1,

two: 2, };

– recast/lib/printer.js–Временные ноды

Спасибо

top related