Skip to content

Commit 854a7ff

Browse files
committed
Use Preconstruct
1 parent dc0d53e commit 854a7ff

File tree

3 files changed

+111
-5
lines changed

3 files changed

+111
-5
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
node_modules
2-
lib
2+
dist

lib/index.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict'
2+
3+
Object.defineProperty(exports, '__esModule', {
4+
value: true,
5+
})
6+
exports.default = void 0
7+
const PURE_ANNOTATION = '#__PURE__'
8+
const isPureAnnotated = node => {
9+
const leadingComments = node.leadingComments
10+
if (!leadingComments) {
11+
return false
12+
}
13+
return leadingComments.some(comment => /[@#]__PURE__/.test(comment.value))
14+
}
15+
const annotateAsPure = path => {
16+
if (isPureAnnotated(path.node)) {
17+
return
18+
}
19+
path.addComment('leading', PURE_ANNOTATION)
20+
}
21+
const hasCallableParent = ({ parentPath }) =>
22+
parentPath.isCallExpression() || parentPath.isNewExpression()
23+
const isUsedAsCallee = path => {
24+
if (!hasCallableParent(path)) {
25+
return false
26+
}
27+
return path.parentPath.get('callee') === path
28+
}
29+
const isInCallee = path => {
30+
do {
31+
path = path.parentPath
32+
if (isUsedAsCallee(path)) {
33+
return true
34+
}
35+
} while (!path.isStatement() && !path.isFunction())
36+
return false
37+
}
38+
const isExecutedDuringInitialization = path => {
39+
let functionParent = path.getFunctionParent()
40+
while (functionParent) {
41+
if (!isUsedAsCallee(functionParent)) {
42+
return false
43+
}
44+
functionParent = functionParent.getFunctionParent()
45+
}
46+
return true
47+
}
48+
const isInAssignmentContext = path => {
49+
const statement = path.getStatementParent()
50+
let parentPath
51+
do {
52+
var _ref = parentPath || path
53+
parentPath = _ref.parentPath
54+
if (
55+
parentPath.isVariableDeclaration() ||
56+
parentPath.isAssignmentExpression() ||
57+
parentPath.isClass()
58+
) {
59+
return true
60+
}
61+
} while (parentPath !== statement)
62+
return false
63+
}
64+
const callableExpressionVisitor = path => {
65+
if (isUsedAsCallee(path) || isInCallee(path)) {
66+
return
67+
}
68+
if (!isExecutedDuringInitialization(path)) {
69+
return
70+
}
71+
if (
72+
!isInAssignmentContext(path) &&
73+
!path.getStatementParent().isExportDefaultDeclaration()
74+
) {
75+
return
76+
}
77+
annotateAsPure(path)
78+
}
79+
var _default = () => ({
80+
name: 'annotate-pure-calls',
81+
visitor: {
82+
'CallExpression|NewExpression': callableExpressionVisitor,
83+
},
84+
})
85+
exports.default = _default

package.json

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,22 @@
44
"description": "Babel plugin for annotating automatically pure function calls.",
55
"author": "Mateusz Burzyński <mateuszburzynski@gmail.com> (https://github.com/Andarist)",
66
"license": "MIT",
7-
"main": "lib/index.js",
7+
"main": "dist/babel-plugin-annotate-pure-calls.cjs.js",
8+
"module": "dist/babel-plugin-annotate-pure-calls.esm.js",
9+
"exports": {
10+
".": {
11+
"types": {
12+
"import": "./dist/babel-plugin-annotate-pure-calls.cjs.mjs",
13+
"default": "./dist/babel-plugin-annotate-pure-calls.cjs.js"
14+
},
15+
"module": "./dist/babel-plugin-annotate-pure-calls.esm.js",
16+
"import": "./dist/babel-plugin-annotate-pure-calls.cjs.mjs",
17+
"default": "./dist/babel-plugin-annotate-pure-calls.cjs.js"
18+
},
19+
"./package.json": "./package.json"
20+
},
821
"files": [
9-
"lib"
22+
"dist"
1023
],
1124
"repository": {
1225
"type": "git",
@@ -23,7 +36,6 @@
2336
"url": "https://github.com/Andarist/babel-plugin-annotate-pure-calls/issues"
2437
},
2538
"homepage": "https://github.com/Andarist/babel-plugin-annotate-pure-calls#readme",
26-
"dependencies": {},
2739
"peerDependencies": {
2840
"@babel/core": "^7.0.0"
2941
},
@@ -32,14 +44,15 @@
3244
"@babel/core": "^7.1.0",
3345
"@babel/plugin-transform-modules-commonjs": "^7.1.0",
3446
"@babel/preset-env": "^7.1.0",
47+
"@preconstruct/cli": "^2.8.10",
3548
"babel-plugin-tester": "^5.4.0",
3649
"husky": "^0.14.3",
3750
"jest": "^23.1.0",
3851
"lint-staged": "^7.1.3",
3952
"prettier": "^1.13.5"
4053
},
4154
"scripts": {
42-
"build": "babel src --out-dir lib",
55+
"build": "preconstruct build",
4356
"pretest": "npm run build",
4457
"test": "jest",
4558
"precommit": "lint-staged",
@@ -56,5 +69,13 @@
5669
"semi": false,
5770
"trailingComma": "all",
5871
"proseWrap": "always"
72+
},
73+
"preconstruct": {
74+
"exports": {
75+
"importConditionDefaultExport": "default"
76+
},
77+
"___experimentalFlags_WILL_CHANGE_IN_PATCH": {
78+
"importsConditions": true
79+
}
5980
}
6081
}

0 commit comments

Comments
 (0)