Skip to content

Commit e46a1ab

Browse files
wip(vue2): fix nested with blocks (+ misc comments)
1 parent 88c9356 commit e46a1ab

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

build/fakeBuble.mjs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export function transform(source, opts) {
2929

3030
const srcAst = parse(source);
3131

32+
// TBD: add import() ?
33+
3234
const names = 'Infinity,undefined,NaN,isFinite,isNaN,' +
3335
'parseFloat,parseInt,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,' +
3436
'Math,Number,Date,Array,Object,Boolean,String,RegExp,Map,Set,JSON,Intl,' +
@@ -87,7 +89,7 @@ export function transform(source, opts) {
8789
!hash[identifier.node.name] &&
8890

8991
// not already in scope
90-
!identifier.scope.hasBinding(identifier.node.name)
92+
!identifier.scope.hasBinding(identifier.node.name, false /* noGlobals */) // noGlobals false mean include globals (Array, Date, ...) and contextVariables (arguments, ...)
9193

9294
) {
9395

@@ -96,6 +98,7 @@ export function transform(source, opts) {
9698
}
9799

98100

101+
// TBD: check https://github.com/yyx990803/buble/commit/af5d322e6925d65ee6cc7fcaadbe25a4151bfcdd
99102

100103
const withStatementVisitor = {
101104
Identifier(path) {
@@ -107,21 +110,37 @@ export function transform(source, opts) {
107110
// then use:
108111
path.node.name = '_vm.' + path.node.name;
109112
}
110-
}
113+
},
114+
WithStatement(path) {
115+
116+
// let handle this by the parent traverse
117+
path.skip();
118+
},
111119
};
112120

113121
traverse(srcAst, {
114122

115123
// babel withstatement https://babeljs.io/docs/en/babel-types#withstatement
116124
// see yyx990803/buble WithStatement.js : https://github.com/yyx990803/buble/blob/master/src/program/types/WithStatement.js
117125

118-
WithStatement(path) {
126+
WithStatement: {
127+
enter(path) {
128+
129+
path.traverse(withStatementVisitor);
130+
},
131+
exit(path) {
119132

120-
path.traverse(withStatementVisitor);
133+
const parentWithStatement = path.findParent(e => e.isWithStatement());
134+
if ( parentWithStatement === null ) {
121135

122-
const left = parse('var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h');
123-
path.replaceWithMultiple([ ...left.program.body, ...path.node.body.body ]);
136+
const left = parse('var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h');
137+
path.replaceWithMultiple([ ...left.program.body, ...path.node.body.body ]);
138+
} else {
124139

140+
// just remove with statement
141+
path.replaceWithMultiple(path.node.body.body);
142+
}
143+
}
125144
}
126145

127146
});

0 commit comments

Comments
 (0)