-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decorators 2 Transform [WIP] #6107
Changes from all commits
57cdfaf
9904b61
1d64084
f214d1d
edb7ff2
10d763e
358cf70
626b92e
d8cebba
ef4eba5
b1092ca
a55e374
5f7bf93
8707f4e
6ec36a1
2e7c6a1
8e28ca7
1776e24
4e527a3
44465f2
f86b327
96f51aa
76f5d3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -223,13 +223,12 @@ export default class File extends Store { | |
return id; | ||
} | ||
|
||
addHelper(name: string): Object { | ||
addHelper(name: string, deps: Array<string>): Object { | ||
const declar = this.declarations[name]; | ||
if (declar) return declar; | ||
|
||
if (!this.usedHelpers[name]) { | ||
this.metadata.usedHelpers.push(name); | ||
this.usedHelpers[name] = true; | ||
} | ||
|
||
const generator = this.get("helperGenerator"); | ||
|
@@ -241,7 +240,18 @@ export default class File extends Store { | |
return t.memberExpression(runtime, t.identifier(name)); | ||
} | ||
|
||
const ref = getHelper(name); | ||
const opts = {}; | ||
if (deps) { | ||
for (const dep in deps) { | ||
if (!this.usedHelpers[name]) { | ||
throw "Helper dependencies must be added to the file first"; | ||
} else { | ||
opts["babelHelpers." + dep] = this.usedHelpers[name]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey. I completely missed this, I guess since it was working on babel tests I assumed it would be generating the correct code. Apparently not. Anyways, I'll track the issues with this feature separately in #6058. Also I don't intend to implement too complex features for this - just enough to make it work, because #5706 already is aimed at providing a superset of the functionality |
||
} | ||
} | ||
} | ||
|
||
const ref = getHelper(name, opts); | ||
const uid = (this.declarations[name] = this.scope.generateUidIdentifier( | ||
name, | ||
)); | ||
|
@@ -260,6 +270,8 @@ export default class File extends Store { | |
}); | ||
} | ||
|
||
this.usedHelpers[name] = uid.name; | ||
|
||
return uid; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
src | ||
test | ||
*.log |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# babel-plugin-syntax-decorators-2 | ||
|
||
> Updated parsing of decorators. | ||
|
||
## Installation | ||
|
||
```sh | ||
npm install --save-dev babel-plugin-syntax-decorators-2 | ||
``` | ||
|
||
## Usage | ||
|
||
### Via `.babelrc` (Recommended) | ||
|
||
**.babelrc** | ||
|
||
```json | ||
{ | ||
"plugins": ["syntax-decorators-2"] | ||
} | ||
``` | ||
|
||
### Via CLI | ||
|
||
```sh | ||
babel --plugins syntax-decorators-2 script.js | ||
``` | ||
|
||
### Via Node API | ||
|
||
```javascript | ||
require("babel-core").transform("code", { | ||
plugins: ["syntax-decorators-2"] | ||
}); | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "babel-plugin-syntax-decorators-2", | ||
"version": "7.0.0-alpha.19", | ||
"description": "Updated parsing of decorators", | ||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-decorators-2", | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
"keywords": [ | ||
"babel-plugin" | ||
], | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function() { | ||
return { | ||
manipulateOptions(opts, parserOpts) { | ||
parserOpts.plugins.push("decorators2"); | ||
}, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "babel-plugin-transform-decorators-2", | ||
"version": "7.0.0-alpha.19", | ||
"author": "Peeyush Kushwaha <peeyush.p97@gmail.com>", | ||
"license": "MIT", | ||
"description": "Transformer for stage-2 decorators", | ||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-decorators-2", | ||
"main": "lib/index.js", | ||
"keywords": [ | ||
"babel", | ||
"babel-plugin", | ||
"decorators" | ||
], | ||
"dependencies": { | ||
"babel-plugin-syntax-decorators-2": "7.0.0-alpha.19" | ||
}, | ||
"devDependencies": { | ||
"babel-helper-plugin-test-runner": "7.0.0-alpha.19", | ||
"babel-helper-transform-fixture-test-runner": "7.0.0-alpha.19", | ||
"babel-plugin-transform-decorators-2": "7.0.0-alpha.19" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you wrap this with
new Error()
? Otherwise babel throws something likeCannot add property _babel to "Helper dependencies must be added to the file first"
instead of justHelper dependencies must be added to the file first
. (Because ofbabel/packages/babel-core/src/transformation/file/index.js
Line 462 in 605adc9