Skip to content

Commit

Permalink
Moved undecorate to separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Jul 1, 2020
1 parent d523576 commit ec2f58c
Show file tree
Hide file tree
Showing 11 changed files with 2,400 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
- attach_workspace:
at: .

- run: yarn test:codemod
- run: cd packages/mobx-undecorate && yarn && yarn test

test-size:
executor: my-executor
Expand Down
4 changes: 2 additions & 2 deletions notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- [x] add **PURE** annotations?
- [x] extract utils for getOwnPropertyDescriptor and defineProperty
- [x] configure property mangling like in Immer. Will it save anything?
- [ ] code mod
- [x] code mod
- [x] code mod, run on v4 tests?
- [x] codemod TS
- [x] codemod babel
Expand All @@ -24,7 +24,7 @@
- [c] migrate `@inject` calls
- [x] unit tests for `ignoreImports`
- [x] unit tests for `keepDecorators`
- [ ] put in separate package
- [x] put in separate package
- [ ] ES5 support
- [x] combine with ES5?
- [x] backport tests and code to v4(6)
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"test:coverage": "yarn test -i --coverage",
"test:performance": "PERSIST=true time node --expose-gc test/perf/index.js",
"test:size": "yarn build && yarn import-size --report . observable computed autorun action",
"test:codemod": "jest --testRegex 'codemod/.*spec' codemod/undecorate.spec.ts",
"test:codemod": "cd packages/mobx-undecorate && yarn test",
"prettier": "prettier --write **/*.{js,ts,md}",
"prebuild": "rimraf lib",
"build": "tsdx build --name mobx --format esm,cjs,umd",
Expand Down Expand Up @@ -65,14 +65,12 @@
"@babel/preset-typescript": "^7.9.0",
"@babel/runtime": "^7.9.2",
"@types/jest": "^25.2.1",
"@types/jscodeshift": "^0.7.0",
"@types/node": "^11.15.12",
"@typescript-eslint/eslint-plugin": "^2.29.0",
"@typescript-eslint/parser": "^2.29.0",
"babel-jest": "^25.4.0",
"conditional-type-checks": "^1.0.5",
"coveralls": "^3.1.0",
"dedent-js": "^1.0.1",
"eslint": "^6.8.0",
"flow-bin": "^0.123.0",
"husky": "^4.2.5",
Expand Down
21 changes: 21 additions & 0 deletions packages/mobx-undecorate/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Michel Weststrate

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions packages/mobx-undecorate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# mobx-undecorate

Converts MobX 5 to MobX 6 code

Running this code mod:

Go to the folder with your source files and run `npx mobx-undecorate`.

This package converts the following MobX 4/5 API's to their MobX 6 equivalent

- `@computed`
- `@action`
- `@observable`
- `@observer`
- `@inject`
- `decorate

### Options

The following flags are accepted:

- `--ignoreImports` Normally the codemod will only convert decorators if they are imported from a mobx package. This flag ignores checking for imports and converts all `@computed`, `@action`, `@observable`, `@observer` and `@inject` calls.
- `--keepDecorators` doesn't rewrite decorators but keeps them as is. But still does generate the required `makeObservable` calls. Use this option if you convert to MobX 6 but will keep using decorators.
- `--decoratorsAfterExport`: set this flag only if you have `decoratorsBeforeExport: false` in your Babel configuration. (Otherwise you will get an error like `SyntaxError: Decorators must be placed *before* the 'export' keyword. You can set the 'decoratorsBeforeExport' option to false`)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { applyTransform } from "jscodeshift/dist/testUtils"
const dedent = require("dedent-js")
import * as u from "./undecorate"
import * as u from "../src/undecorate"

function convert(
source: string,
Expand Down
17 changes: 17 additions & 0 deletions packages/mobx-undecorate/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env node
import { spawn } from "child_process"

// this is pretty lame, probably better make a .cmd and .sh file...
spawn(
"node_modules/.bin/codeshift",
[
"-t",
"src/undecorate.ts",
process.cwd(),
...process.argv.filter(arg => arg.startsWith("--")).join(" ")
],
{
cwd: __dirname,
stdio: "inherit"
}
)
67 changes: 67 additions & 0 deletions packages/mobx-undecorate/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"name": "mobx-undecorate",
"version": "0.0.1",
"description": "Migrate MobX 4/5 to MobX 6",
"bin": "cli.js",
"scripts": {
"test": "../../node_modules/.bin/jest"
},
"repository": {
"type": "git",
"url": "https://github.com/mobxjs/mobx.git"
},
"author": "Michel Weststrate",
"license": "MIT",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/mobx"
},
"bugs": {
"url": "https://github.com/mobxjs/mobx/issues"
},
"files": [
"cli.js",
"src",
"LICENSE",
"README.md"
],
"homepage": "https://mobx.js.org/",
"dependencies": {
"@babel/core": "^7.9.0",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-decorators": "^7.8.3",
"@babel/plugin-transform-runtime": "^7.9.0",
"@babel/preset-env": "^7.9.5",
"@babel/preset-typescript": "^7.9.0",
"@babel/runtime": "^7.9.2",
"@types/jest": "^25.2.1",
"@types/jscodeshift": "^0.7.0",
"@types/node": "^11.15.12",
"dedent-js": "^1.0.1",
"jscodeshift": "^0.7.0"
},
"keywords": [
"mobx"
],
"jest": {
"transform": {
"^.+\\.tsx?$": "ts-jest",
"^.+\\.jsx?$": "babel-jest"
},
"testRegex": "__tests__/.*\\.(t|j)sx?$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json"
],
"testPathIgnorePatterns": [
"/node_modules/",
"/\\./"
],
"watchPathIgnorePatterns": [
"<rootDir>/node_modules/"
]
}
}
File renamed without changes.
Loading

0 comments on commit ec2f58c

Please sign in to comment.