forked from ember-cli/ember-compatibility-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparision-plugin.js
90 lines (71 loc) · 2.89 KB
/
comparision-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict';
const semver = require('semver');
const VersionChecker = require('ember-cli-version-checker');
const extractTrueVersion = require('./utils/extract-true-version');
function versionFor(depName, state) {
let VERSIONS = state.version_cache;
let version = VERSIONS[depName];
if (version === undefined) {
let checker = new VersionChecker(
{
name: state.opts.name,
root: state.opts.root
}
).for(depName, 'npm');
if (!checker.version) {
throw new Error(`Expected "${state.opts.name}" to have "${depName}" as a dependency, but it was not found.`);
}
version = VERSIONS[depName] = extractTrueVersion(checker.version);
}
return version;
}
function shouldReplaceLegacyCallWithTrue(testFn, path, state) {
let argument = path.node.arguments[0];
return testFn(state.opts.emberVersion, argument.value);
}
function shouldReplaceCallWithTrue(testFn, path, state) {
if (path.node.arguments.length === 1) {
return shouldReplaceLegacyCallWithTrue(testFn, path, state);
}
let depNameArgument = path.node.arguments[0];
let versionArgument = path.node.arguments[1];
let depVersion = versionFor(depNameArgument.value, state);
return testFn(depVersion, versionArgument.value);
}
function comparisonPlugin(babel) {
const t = babel.types;
const trueIdentifier = t.identifier('true');
const falseIdentifier = t.identifier('false');
return {
name: "ember-compatibility-helpers",
visitor: {
ImportSpecifier(path, state) {
if (path.parent.source.value === 'ember-compatibility-helpers') {
state.version_cache = state.version_cache || Object.create(null);
let importedName = path.node.imported.name;
if (importedName === 'gte') {
state.gteImportId = state.gteImportId || path.scope.generateUidIdentifierBasedOnNode(path.node.id);
path.scope.rename(path.node.local.name, state.gteImportId.name);
path.remove();
}
if (importedName === 'lte') {
state.lteImportId = state.lteImportId || path.scope.generateUidIdentifierBasedOnNode(path.node.id);
path.scope.rename(path.node.local.name, state.lteImportId.name);
path.remove();
}
}
},
CallExpression(path, state) {
if (state.gteImportId && path.node.callee.name === state.gteImportId.name) {
let replacementIdentifier = shouldReplaceCallWithTrue(semver.gte, path, state) ? trueIdentifier : falseIdentifier;
path.replaceWith(replacementIdentifier);
} else if (state.lteImportId && path.node.callee.name === state.lteImportId.name) {
let replacementIdentifier = shouldReplaceCallWithTrue(semver.lte, path, state) ? trueIdentifier : falseIdentifier;
path.replaceWith(replacementIdentifier);
}
}
}
};
}
comparisonPlugin.baseDir = () => __dirname;
module.exports = comparisonPlugin;