babel-plugin-namespace-amd-define
Advanced tools
Comparing version 1.7.0-alpha.43ab330a to 1.7.0-alpha.6d6b0c8b
@@ -66,14 +66,2 @@ 'use strict'; | ||
it('namespaces references to global define identifier', function () { | ||
var source = '\n\tif (typeof define === "function" && define.amd) {\n\t\tconsole.log(\'UMD!\');\n\t}\n\t'; | ||
var _babel$transform3 = babel.transform(source, { | ||
filenameRelative: __filename, | ||
plugins: [_index2.default] | ||
}), | ||
code = _babel$transform3.code; | ||
expect(code).toMatchSnapshot(); | ||
}); | ||
describe('does not namespace references to local define identifier', function () { | ||
@@ -83,7 +71,7 @@ it('when it is a variable', function () { | ||
var _babel$transform4 = babel.transform(source, { | ||
var _babel$transform3 = babel.transform(source, { | ||
filenameRelative: __filename, | ||
plugins: [_index2.default] | ||
}), | ||
code = _babel$transform4.code; | ||
code = _babel$transform3.code; | ||
@@ -96,7 +84,7 @@ expect(code).toMatchSnapshot(); | ||
var _babel$transform5 = babel.transform(source, { | ||
var _babel$transform4 = babel.transform(source, { | ||
filenameRelative: __filename, | ||
plugins: [_index2.default] | ||
}), | ||
code = _babel$transform5.code; | ||
code = _babel$transform4.code; | ||
@@ -109,7 +97,7 @@ expect(code).toMatchSnapshot(); | ||
var _babel$transform6 = babel.transform(source, { | ||
var _babel$transform5 = babel.transform(source, { | ||
filenameRelative: __filename, | ||
plugins: [_index2.default] | ||
}), | ||
code = _babel$transform6.code; | ||
code = _babel$transform5.code; | ||
@@ -122,7 +110,7 @@ expect(code).toMatchSnapshot(); | ||
var _babel$transform7 = babel.transform(source, { | ||
var _babel$transform6 = babel.transform(source, { | ||
filenameRelative: __filename, | ||
plugins: [_index2.default] | ||
}), | ||
code = _babel$transform7.code; | ||
code = _babel$transform6.code; | ||
@@ -129,0 +117,0 @@ expect(code).toMatchSnapshot(); |
@@ -8,3 +8,4 @@ 'use strict'; | ||
exports.default = function () { | ||
var namespaceCount = void 0; | ||
var extraNamespaceCount = void 0; | ||
var firstDefineNamespaced = void 0; | ||
@@ -31,10 +32,16 @@ var namespaceVisitor = { | ||
// If 'define' is not defined in any scope namespace or defined | ||
// in the root scope as global, namespace it | ||
if (scope == null || scope.parent == null && !scope.bindings.define) { | ||
var namespace = this.opts.namespace || 'Liferay.Loader'; | ||
// If 'define' is not defined in any scope namespace or | ||
// defined in the root scope as global... | ||
if (!firstDefineNamespaced) { | ||
// ...and it's its first appearance, namespace it | ||
var namespace = this.opts.namespace || 'Liferay.Loader'; | ||
path.node.name = namespace + '.define'; | ||
path.node.name = namespace + '.define'; | ||
namespaceCount++; | ||
firstDefineNamespaced = true; | ||
} else { | ||
// ...and appeared before, record a new extra appearance | ||
extraNamespaceCount++; | ||
} | ||
} | ||
@@ -52,7 +59,8 @@ } | ||
// Program exit :-( | ||
namespaceCount = 0; | ||
firstDefineNamespaced = false; | ||
extraNamespaceCount = 0; | ||
path.traverse(namespaceVisitor, { opts: state.opts }); | ||
if (namespaceCount > 0) { | ||
if (extraNamespaceCount > 0) { | ||
var _babelIpc$get = babelIpc.get(state, function () { | ||
@@ -65,3 +73,9 @@ return { | ||
log.info('namespace-amd-define', 'Namespaced', namespaceCount, 'AMD defines'); | ||
if (firstDefineNamespaced) { | ||
log.info('namespace-amd-define', 'Namespaced first AMD define in file'); | ||
} | ||
if (extraNamespaceCount) { | ||
log.warn('namespace-amd-define', 'Found', extraNamespaceCount, 'define() calls inside the module definition', 'which have been ignored as they should never', 'be executed during runtime'); | ||
} | ||
} | ||
@@ -68,0 +82,0 @@ } |
{ | ||
"name": "babel-plugin-namespace-amd-define", | ||
"version": "1.7.0-alpha.43ab330a", | ||
"version": "1.7.0-alpha.6d6b0c8b", | ||
"description": "A Babel plugin to namespace (prefix) AMD define() calls.", | ||
@@ -15,4 +15,4 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"liferay-npm-build-tools-common": "1.7.0-alpha.43ab330a" | ||
"liferay-npm-build-tools-common": "1.7.0-alpha.6d6b0c8b" | ||
} | ||
} |
@@ -56,17 +56,2 @@ import * as babel from 'babel-core'; | ||
it('namespaces references to global define identifier', () => { | ||
const source = ` | ||
if (typeof define === "function" && define.amd) { | ||
console.log('UMD!'); | ||
} | ||
`; | ||
const {code} = babel.transform(source, { | ||
filenameRelative: __filename, | ||
plugins: [plugin], | ||
}); | ||
expect(code).toMatchSnapshot(); | ||
}); | ||
describe('does not namespace references to local define identifier', () => { | ||
@@ -73,0 +58,0 @@ it('when it is a variable', () => { |
@@ -10,3 +10,4 @@ import * as babelIpc from 'liferay-npm-build-tools-common/lib/babel-ipc'; | ||
export default function() { | ||
let namespaceCount; | ||
let extraNamespaceCount; | ||
let firstDefineNamespaced; | ||
@@ -39,4 +40,2 @@ const namespaceVisitor = { | ||
// If 'define' is not defined in any scope namespace or defined | ||
// in the root scope as global, namespace it | ||
if ( | ||
@@ -46,7 +45,16 @@ scope == null || | ||
) { | ||
const namespace = this.opts.namespace || 'Liferay.Loader'; | ||
// If 'define' is not defined in any scope namespace or | ||
// defined in the root scope as global... | ||
if (!firstDefineNamespaced) { | ||
// ...and it's its first appearance, namespace it | ||
const namespace = | ||
this.opts.namespace || 'Liferay.Loader'; | ||
path.node.name = `${namespace}.define`; | ||
path.node.name = `${namespace}.define`; | ||
namespaceCount++; | ||
firstDefineNamespaced = true; | ||
} else { | ||
// ...and appeared before, record a new extra appearance | ||
extraNamespaceCount++; | ||
} | ||
} | ||
@@ -64,7 +72,8 @@ } | ||
// Program exit :-( | ||
namespaceCount = 0; | ||
firstDefineNamespaced = false; | ||
extraNamespaceCount = 0; | ||
path.traverse(namespaceVisitor, {opts: state.opts}); | ||
if (namespaceCount > 0) { | ||
if (extraNamespaceCount > 0) { | ||
const {log} = babelIpc.get(state, () => ({ | ||
@@ -74,8 +83,19 @@ log: new PluginLogger(), | ||
log.info( | ||
'namespace-amd-define', | ||
'Namespaced', | ||
namespaceCount, | ||
'AMD defines' | ||
); | ||
if (firstDefineNamespaced) { | ||
log.info( | ||
'namespace-amd-define', | ||
'Namespaced first AMD define in file' | ||
); | ||
} | ||
if (extraNamespaceCount) { | ||
log.warn( | ||
'namespace-amd-define', | ||
'Found', | ||
extraNamespaceCount, | ||
'define() calls inside the module definition', | ||
'which have been ignored as they should never', | ||
'be executed during runtime' | ||
); | ||
} | ||
} | ||
@@ -82,0 +102,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
24571
341
+ Addedliferay-npm-build-tools-common@1.7.0-alpha.6d6b0c8b(transitive)
- Removedliferay-npm-build-tools-common@1.7.0-alpha.43ab330a(transitive)