Code Transformation and Linting with Abstract Syntax Trees
Introduction
What this talk is
- Introduction to Abstract Syntax Trees
 - Introduction to inspecting and traversing ASTs with ESLint and Babel
 
What this talk is not
- Deep dive into ASTs
 - Publishing actual plugin packages
 
Setup
https://github.com/kentcdodds/asts-workshop#setup
1  | git clone https://github.com/kentcdodds/asts-workshop.git  | 
Abstract Syntax Trees
Example of Abstract Syntax Trees
BABEL使用了AST来转化代码。
例如可以使用babel-plugin-lodash来转化文件
babel-plugin-lodash A simple transform to cherry-pick Lodash modules so you don’t have to.
- Example
- Transforms
 
 
1  | import _ from 'lodash'  | 
- roughly to
 
1  | import _add from 'lodash/fp/add'  | 
eslint-plugin-react
eslint-plugin-security
eslint-plugin-jsx-a11y
Introducing Abstract Syntax Trees
What Is an AST
Code for humans
1  | const uniqueRandomArray = require('unique-random-array')  | 
Code for computers
1  | {  | 
Exploring ASTs
ESLint Plugin and AST
Challenge 1: Making a Plugin
npm start exercise.eslint.0
1  | // eslint exercise 0 (no-console)  | 
Challenge 2: Adding New Methods
1  | // eslint exercise 1 (no-console)  | 
Challenge 3: Schema Property
npm start exercise.eslint.2
npm start
1  | const disallowedMethods = ['log', 'info', 'warn', 'error', 'dir']  | 
Challenge 4: Edge Cases
npm start ex.es.3
npm start
1  | const disallowedMethods = ['log', 'info', 'warn', 'error', 'dir']  | 
Challenge 5: Applying Fixes
npm start ex.es.4
npm start
1  | const disallowedMethods = ['log', 'info', 'warn', 'error', 'dir']  | 
Babel Plugin & AST
Introducing Babel & AST
Challenge 6: Captains Log
npm start ex.ba.0
npm start
1  | // babel exercise 0 (captains-log)  | 
Challenge 7: Prefixing
npm start ex.ba.1
npm start
1  | // babel exercise 1 (captains-log)  | 
Challenge 8: Arrow Function
npm start ex.ba.2
npm start
1  | // babel exercise 2 (captains-log)  | 
Challenge 9: Divide Function
npm start ex.ba.3
npm start
1  | // babel exercise 3 (captains-log)  | 
Codemods & AST
Introducing Codemods and AST
Challenge 10: Upgrade Patterns
npm start exercise.codemod.0
npm start
1  | // codemod exercise 0 (ymnn-jquery)  | 
Challenge 11: Swap Out Utillties
npm start exercise.codemod.3
npm start
1  | export default function(babel) {  |