├── .gitignore ├── test ├── fixtures │ ├── no-decorator │ │ ├── fixture.js │ │ └── expected.js │ ├── plain │ │ ├── fixture.js │ │ └── expected.js │ └── export │ │ ├── fixture.js │ │ └── expected.js └── index.js ├── src ├── index.js └── auto-assign.js ├── package.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | lib 2 | -------------------------------------------------------------------------------- /test/fixtures/no-decorator/fixture.js: -------------------------------------------------------------------------------- 1 | class Hello { 2 | constructor(foo, bar, baz) { 3 | console.log('hello'); 4 | } 5 | 6 | say() { 7 | return 'hello'; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /test/fixtures/plain/fixture.js: -------------------------------------------------------------------------------- 1 | @autoAssign 2 | class Hello { 3 | constructor(foo, bar, baz) { 4 | console.log('hello'); 5 | } 6 | 7 | say() { 8 | return 'hello'; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/export/fixture.js: -------------------------------------------------------------------------------- 1 | @autoAssign 2 | export default class Hello { 3 | constructor(foo, bar, baz) { 4 | console.log('hello'); 5 | } 6 | 7 | say() { 8 | return 'hello'; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import AutoAssign from './auto-assign'; 2 | 3 | export default function ({ Plugin, types: t }) { 4 | return new Plugin('auto-assign', { 5 | visitor: { 6 | ClassDeclaration: function (node, parent) { 7 | new AutoAssign(t).run(node); 8 | } 9 | } 10 | }); 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/no-decorator/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var Hello = (function () { 4 | function Hello(foo, bar, baz) { 5 | babelHelpers.classCallCheck(this, Hello); 6 | 7 | console.log('hello'); 8 | } 9 | 10 | babelHelpers.createClass(Hello, [{ 11 | key: 'say', 12 | value: function say() { 13 | return 'hello'; 14 | } 15 | }]); 16 | return Hello; 17 | })(); 18 | -------------------------------------------------------------------------------- /test/fixtures/plain/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var Hello = (function () { 4 | function Hello(foo, bar, baz) { 5 | babelHelpers.classCallCheck(this, _Hello); 6 | this.foo = foo; 7 | this.bar = bar; 8 | this.baz = baz; 9 | 10 | console.log('hello'); 11 | } 12 | 13 | babelHelpers.createClass(Hello, [{ 14 | key: 'say', 15 | value: function say() { 16 | return 'hello'; 17 | } 18 | }]); 19 | var _Hello = Hello; 20 | return Hello; 21 | })(); 22 | -------------------------------------------------------------------------------- /test/fixtures/export/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, '__esModule', { 4 | value: true 5 | }); 6 | 7 | var Hello = (function () { 8 | function Hello(foo, bar, baz) { 9 | babelHelpers.classCallCheck(this, _Hello); 10 | this.foo = foo; 11 | this.bar = bar; 12 | this.baz = baz; 13 | 14 | console.log('hello'); 15 | } 16 | 17 | babelHelpers.createClass(Hello, [{ 18 | key: 'say', 19 | value: function say() { 20 | return 'hello'; 21 | } 22 | }]); 23 | var _Hello = Hello; 24 | return Hello; 25 | })(); 26 | 27 | exports['default'] = Hello; 28 | module.exports = exports['default']; 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-plugin-auto-assign", 3 | "version": "0.1.0", 4 | "description": "A babel plugin that automatically assigns constructor parameters to instance properties.", 5 | "keywords": [ 6 | "babel", 7 | "babel-plugin" 8 | ], 9 | "main": "lib/index.js", 10 | "scripts": { 11 | "build": "babel-plugin build", 12 | "push": "babel-plugin publish", 13 | "test": "npm run build && mocha" 14 | }, 15 | "files": [ 16 | "README.md", 17 | "LICENSE", 18 | "lib" 19 | ], 20 | "author": "Shuhei Kagawa ", 21 | "license": "ISC", 22 | "devDependencies": { 23 | "babel": "^5.8.23", 24 | "mocha": "^2.3.2" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Shuhei Kagawa 2 | 3 | Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. 4 | 5 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 6 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var fs = require('fs'); 3 | var path = require('path'); 4 | var babel = require('babel'); 5 | 6 | function test(fixtureName) { 7 | it(fixtureName, function () { 8 | var fixturePath = path.resolve(__dirname, 'fixtures', fixtureName, 'fixture.js'); 9 | var expectedPath = path.resolve(__dirname, 'fixtures', fixtureName, 'expected.js'); 10 | var actual = babel.transformFileSync(fixturePath, { 11 | optional: ['es7.decorators'], 12 | plugins: ['../lib'], 13 | externalHelpers: true 14 | }).code; 15 | var expected = fs.readFileSync(expectedPath, { encoding: 'utf8' }); 16 | assert.equal(actual + '\n', expected); 17 | }); 18 | } 19 | 20 | [ 21 | 'no-decorator', 22 | 'plain', 23 | 'export' 24 | ].map(test); 25 | -------------------------------------------------------------------------------- /src/auto-assign.js: -------------------------------------------------------------------------------- 1 | export default class AutoAssign { 2 | constructor(types) { 3 | this.types = types; 4 | } 5 | 6 | run(klass) { 7 | const decorators = this.findAutoAssignDecorators(klass); 8 | if (decorators.length > 0) { 9 | const ctor = this.findConstructor(klass); 10 | const args = this.getArguments(ctor); 11 | this.prependAssignments(ctor, args); 12 | this.deleteDecorators(klass, decorators); 13 | } 14 | } 15 | 16 | findAutoAssignDecorators(klass) { 17 | return (klass.decorators || []).filter((decorator) => { 18 | return decorator.expression.name === 'autoAssign'; 19 | }); 20 | } 21 | 22 | deleteDecorators(klass, decorators) { 23 | decorators.forEach((decorator) => { 24 | const index = klass.decorators.indexOf(decorator); 25 | if (index >= 0) { 26 | klass.decorators.splice(index, 1); 27 | } 28 | }); 29 | } 30 | 31 | findConstructor(klass) { 32 | return klass.body.body.filter((body) => { 33 | return body.kind === 'constructor'; 34 | })[0]; 35 | } 36 | 37 | getArguments(ctor) { 38 | return ctor.value.params; 39 | } 40 | 41 | prependAssignments(ctor, args) { 42 | const body = ctor.value.body.body; 43 | args.slice().reverse().forEach((arg) => { 44 | const assignment = this.buildAssignment(arg); 45 | body.unshift(assignment); 46 | }); 47 | } 48 | 49 | buildAssignment(arg) { 50 | const self = this.types.identifier('this'); 51 | const prop = this.types.memberExpression(self, arg); 52 | const assignment = this.types.assignmentExpression('=', prop, arg); 53 | return this.types.expressionStatement(assignment); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [DEPRECATED] babel-plugin-auto-assign 2 | 3 | [![npm version](https://img.shields.io/npm/v/babel-plugin-auto-assign.svg)](https://www.npmjs.org/package/babel-plugin-auto-assign) 4 | [![npm downloads](https://img.shields.io/npm/dm/babel-plugin-auto-assign.svg)](https://www.npmjs.org/package/babel-plugin-auto-assign) 5 | 6 | **This plugin is no longer maintained, and works only with Babel v5. To use the feature in newer versions of Babel, please check out [jakewhelan/babel-plugin-transform-constructor-self-assign](https://github.com/jakewhelan/babel-plugin-transform-constructor-self-assign)** 7 | 8 | --- 9 | 10 | A babel plugin that automatically assigns constructor parameters to instance properties. Might be convenient for class-based DI like Angular does. 11 | 12 | Before: 13 | 14 | ```js 15 | @autoAssign 16 | class Hello { 17 | constructor(foo, bar, baz) { 18 | } 19 | } 20 | ``` 21 | 22 | After: 23 | 24 | ```js 25 | class Hello { 26 | constructor(foo, bar, baz) { 27 | this.foo = foo; 28 | this.bar = bar; 29 | this.baz = baz; 30 | } 31 | } 32 | ``` 33 | 34 | ## Installation 35 | 36 | ```sh 37 | $ npm install babel-plugin-auto-assign 38 | ``` 39 | 40 | ## Usage 41 | 42 | ### Via `.babelrc` (Recommended) 43 | 44 | **.babelrc** 45 | 46 | ```json 47 | { 48 | "optional": ["es7.decorators"], 49 | "plugins": ["auto-assign"] 50 | } 51 | ``` 52 | 53 | ### Via CLI 54 | 55 | ```sh 56 | $ babel --optional es7.decorators --plugins auto-assgin script.js 57 | ``` 58 | 59 | ### Via Node API 60 | 61 | ```js 62 | require('babel-core').transform('code', { 63 | optional: ['es7.decorators'], 64 | plugins: ['auto-assign'] 65 | }); 66 | ``` 67 | 68 | ## License 69 | 70 | ISC 71 | --------------------------------------------------------------------------------