@knighted/module 1.0.0-alpha.3 → 1.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,9 +6,9 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.format = void 0;
7
7
  var _magicString = _interopRequireDefault(require("magic-string"));
8
8
  var _traverse2 = _interopRequireDefault(require("@babel/traverse"));
9
+ var _identifier = require("./formatters/identifier.cjs");
9
10
  var _metaProperty = require("./formatters/metaProperty.cjs");
10
11
  var _memberExpression = require("./formatters/memberExpression.cjs");
11
- var _expressionStatement = require("./formatters/expressionStatement.cjs");
12
12
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
13
  const traverse = _traverse2.default.default;
14
14
 
@@ -19,12 +19,12 @@ const traverse = _traverse2.default.default;
19
19
  const format = (code, ast, options) => {
20
20
  const src = new _magicString.default(code);
21
21
  traverse(ast, {
22
+ Identifier(path) {
23
+ (0, _identifier.identifier)(path, src, options);
24
+ },
22
25
  MetaProperty(path) {
23
26
  (0, _metaProperty.metaProperty)(path, src, options);
24
27
  },
25
- ExpressionStatement(path) {
26
- (0, _expressionStatement.expressionStatement)(path, src, options);
27
- },
28
28
  MemberExpression(path) {
29
29
  (0, _memberExpression.memberExpression)(path, src, options);
30
30
  }
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.identifier = void 0;
7
+ const identifier = (nodePath, src, options) => {
8
+ if (options.type === 'module') {
9
+ const {
10
+ node
11
+ } = nodePath;
12
+ const {
13
+ start,
14
+ end
15
+ } = node;
16
+ if (typeof start === 'number' && typeof end === 'number' && node.type === 'Identifier') {
17
+ const {
18
+ name
19
+ } = node;
20
+ const isMemberExpression = Boolean(nodePath.findParent(path => path.isMemberExpression()));
21
+
22
+ // CommonJS globals in expression/statement
23
+ switch (name) {
24
+ case 'module':
25
+ {
26
+ if (!isMemberExpression) {
27
+ src.update(start, end, 'import.meta');
28
+ }
29
+ break;
30
+ }
31
+ case 'exports':
32
+ {
33
+ if (!isMemberExpression) {
34
+ src.update(start, end, '{}');
35
+ }
36
+ break;
37
+ }
38
+ case '__filename':
39
+ src.update(start, end, 'import.meta.filename');
40
+ break;
41
+ case '__dirname':
42
+ src.update(start, end, 'import.meta.dirname');
43
+ break;
44
+ }
45
+ }
46
+ }
47
+ };
48
+ exports.identifier = identifier;
@@ -0,0 +1,5 @@
1
+ import MagicString from 'magic-string';
2
+ import type { NodePath } from '@babel/traverse';
3
+ import type { Identifier } from '@babel/types';
4
+ import type { FormatterOptions } from '../types.cjs';
5
+ export declare const identifier: (nodePath: NodePath<Identifier>, src: MagicString, options: FormatterOptions) => void;
package/dist/format.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import MagicString from 'magic-string';
2
2
  import _traverse from '@babel/traverse';
3
+ import { identifier } from './formatters/identifier.js';
3
4
  import { metaProperty } from './formatters/metaProperty.js';
4
5
  import { memberExpression } from './formatters/memberExpression.js';
5
- import { expressionStatement } from './formatters/expressionStatement.js';
6
6
  const traverse = _traverse.default;
7
7
 
8
8
  /**
@@ -12,12 +12,12 @@ const traverse = _traverse.default;
12
12
  export const format = (code, ast, options) => {
13
13
  const src = new MagicString(code);
14
14
  traverse(ast, {
15
+ Identifier(path) {
16
+ identifier(path, src, options);
17
+ },
15
18
  MetaProperty(path) {
16
19
  metaProperty(path, src, options);
17
20
  },
18
- ExpressionStatement(path) {
19
- expressionStatement(path, src, options);
20
- },
21
21
  MemberExpression(path) {
22
22
  memberExpression(path, src, options);
23
23
  }
@@ -0,0 +1,5 @@
1
+ import MagicString from 'magic-string';
2
+ import type { NodePath } from '@babel/traverse';
3
+ import type { Identifier } from '@babel/types';
4
+ import type { FormatterOptions } from '../types.js';
5
+ export declare const identifier: (nodePath: NodePath<Identifier>, src: MagicString, options: FormatterOptions) => void;
@@ -0,0 +1,41 @@
1
+ export const identifier = (nodePath, src, options) => {
2
+ if (options.type === 'module') {
3
+ const {
4
+ node
5
+ } = nodePath;
6
+ const {
7
+ start,
8
+ end
9
+ } = node;
10
+ if (typeof start === 'number' && typeof end === 'number' && node.type === 'Identifier') {
11
+ const {
12
+ name
13
+ } = node;
14
+ const isMemberExpression = Boolean(nodePath.findParent(path => path.isMemberExpression()));
15
+
16
+ // CommonJS globals in expression/statement
17
+ switch (name) {
18
+ case 'module':
19
+ {
20
+ if (!isMemberExpression) {
21
+ src.update(start, end, 'import.meta');
22
+ }
23
+ break;
24
+ }
25
+ case 'exports':
26
+ {
27
+ if (!isMemberExpression) {
28
+ src.update(start, end, '{}');
29
+ }
30
+ break;
31
+ }
32
+ case '__filename':
33
+ src.update(start, end, 'import.meta.filename');
34
+ break;
35
+ case '__dirname':
36
+ src.update(start, end, 'import.meta.dirname');
37
+ break;
38
+ }
39
+ }
40
+ }
41
+ };
@@ -0,0 +1,5 @@
1
+ import MagicString from 'magic-string';
2
+ import type { NodePath } from '@babel/traverse';
3
+ import type { Identifier } from '@babel/types';
4
+ import type { FormatterOptions } from '../types.js';
5
+ export declare const identifier: (nodePath: NodePath<Identifier>, src: MagicString, options: FormatterOptions) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knighted/module",
3
- "version": "1.0.0-alpha.3",
3
+ "version": "1.0.0-alpha.4",
4
4
  "description": "Converts module differences in source files between ES and CommonJS.",
5
5
  "type": "module",
6
6
  "main": "dist/module.js",
@@ -68,9 +68,9 @@
68
68
  "eslint": "^9.3.0",
69
69
  "eslint-plugin-n": "^17.7.0",
70
70
  "prettier": "^3.2.5",
71
- "tsx": "^4.11.0",
71
+ "tsx": "^4.11.2",
72
72
  "typescript": "^5.4.5",
73
- "typescript-eslint": "^8.0.0-alpha.18"
73
+ "typescript-eslint": "^8.0.0-alpha.26"
74
74
  },
75
75
  "dependencies": {
76
76
  "@babel/parser": "^7.24.6",