From 854a7ff76e94bce2784d6487954c295492ebeac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Tue, 7 Jan 2025 10:31:12 +0100 Subject: [PATCH] Use Preconstruct --- .gitignore | 2 +- lib/index.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 29 +++++++++++++++--- 3 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 lib/index.js diff --git a/.gitignore b/.gitignore index 491fc35..f06235c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ node_modules -lib +dist diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..1527bea --- /dev/null +++ b/lib/index.js @@ -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 diff --git a/package.json b/package.json index b836e3a..73872d8 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,22 @@ "description": "Babel plugin for annotating automatically pure function calls.", "author": "Mateusz BurzyƄski (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", @@ -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" }, @@ -32,6 +44,7 @@ "@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", @@ -39,7 +52,7 @@ "prettier": "^1.13.5" }, "scripts": { - "build": "babel src --out-dir lib", + "build": "preconstruct build", "pretest": "npm run build", "test": "jest", "precommit": "lint-staged", @@ -56,5 +69,13 @@ "semi": false, "trailingComma": "all", "proseWrap": "always" + }, + "preconstruct": { + "exports": { + "importConditionDefaultExport": "default" + }, + "___experimentalFlags_WILL_CHANGE_IN_PATCH": { + "importsConditions": true + } } }