Skip to content

Commit

Permalink
Use Preconstruct
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Jan 7, 2025
1 parent dc0d53e commit 854a7ff
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules
lib
dist
85 changes: 85 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict'

Object.defineProperty(exports, '__esModule', {
value: true,
})
exports.default = void 0
const PURE_ANNOTATION = '#__PURE__'
const isPureAnnotated = node => {
const leadingComments = node.leadingComments
if (!leadingComments) {
return false
}
return leadingComments.some(comment => /[@#]__PURE__/.test(comment.value))
}
const annotateAsPure = path => {
if (isPureAnnotated(path.node)) {
return
}
path.addComment('leading', PURE_ANNOTATION)
}
const hasCallableParent = ({ parentPath }) =>
parentPath.isCallExpression() || parentPath.isNewExpression()
const isUsedAsCallee = path => {
if (!hasCallableParent(path)) {
return false
}
return path.parentPath.get('callee') === path
}
const isInCallee = path => {
do {
path = path.parentPath
if (isUsedAsCallee(path)) {
return true
}
} while (!path.isStatement() && !path.isFunction())
return false
}
const isExecutedDuringInitialization = path => {
let functionParent = path.getFunctionParent()
while (functionParent) {
if (!isUsedAsCallee(functionParent)) {
return false
}
functionParent = functionParent.getFunctionParent()
}
return true
}
const isInAssignmentContext = path => {
const statement = path.getStatementParent()
let parentPath
do {
var _ref = parentPath || path
parentPath = _ref.parentPath
if (
parentPath.isVariableDeclaration() ||
parentPath.isAssignmentExpression() ||
parentPath.isClass()
) {
return true
}
} while (parentPath !== statement)
return false
}
const callableExpressionVisitor = path => {
if (isUsedAsCallee(path) || isInCallee(path)) {
return
}
if (!isExecutedDuringInitialization(path)) {
return
}
if (
!isInAssignmentContext(path) &&
!path.getStatementParent().isExportDefaultDeclaration()
) {
return
}
annotateAsPure(path)
}
var _default = () => ({
name: 'annotate-pure-calls',
visitor: {
'CallExpression|NewExpression': callableExpressionVisitor,
},
})
exports.default = _default
29 changes: 25 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,22 @@
"description": "Babel plugin for annotating automatically pure function calls.",
"author": "Mateusz Burzyński <[email protected]> (https://github.com/Andarist)",
"license": "MIT",
"main": "lib/index.js",
"main": "dist/babel-plugin-annotate-pure-calls.cjs.js",
"module": "dist/babel-plugin-annotate-pure-calls.esm.js",
"exports": {
".": {
"types": {
"import": "./dist/babel-plugin-annotate-pure-calls.cjs.mjs",
"default": "./dist/babel-plugin-annotate-pure-calls.cjs.js"
},
"module": "./dist/babel-plugin-annotate-pure-calls.esm.js",
"import": "./dist/babel-plugin-annotate-pure-calls.cjs.mjs",
"default": "./dist/babel-plugin-annotate-pure-calls.cjs.js"
},
"./package.json": "./package.json"
},
"files": [
"lib"
"dist"
],
"repository": {
"type": "git",
Expand All @@ -23,7 +36,6 @@
"url": "https://github.com/Andarist/babel-plugin-annotate-pure-calls/issues"
},
"homepage": "https://github.com/Andarist/babel-plugin-annotate-pure-calls#readme",
"dependencies": {},
"peerDependencies": {
"@babel/core": "^7.0.0"
},
Expand All @@ -32,14 +44,15 @@
"@babel/core": "^7.1.0",
"@babel/plugin-transform-modules-commonjs": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"@preconstruct/cli": "^2.8.10",
"babel-plugin-tester": "^5.4.0",
"husky": "^0.14.3",
"jest": "^23.1.0",
"lint-staged": "^7.1.3",
"prettier": "^1.13.5"
},
"scripts": {
"build": "babel src --out-dir lib",
"build": "preconstruct build",
"pretest": "npm run build",
"test": "jest",
"precommit": "lint-staged",
Expand All @@ -56,5 +69,13 @@
"semi": false,
"trailingComma": "all",
"proseWrap": "always"
},
"preconstruct": {
"exports": {
"importConditionDefaultExport": "default"
},
"___experimentalFlags_WILL_CHANGE_IN_PATCH": {
"importsConditions": true
}
}
}

0 comments on commit 854a7ff

Please sign in to comment.