Skip to content

Commit 769a240

Browse files
authored
Merge pull request #22 from konard/issue-19-793570d5e6b2
refactor(app): remove unused ViteBabelState and extract plugin name constant
2 parents b6a0fc8 + 52fe28e commit 769a240

4 files changed

Lines changed: 20 additions & 11 deletions

File tree

packages/app/babel.cjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
const path = require("node:path")
2424

25+
const babelPluginName = "component-path-babel-tagger"
2526
const componentPathAttributeName = "data-path"
2627
const jsxFilePattern = /\.(tsx|jsx)(\?.*)?$/u
2728

@@ -37,7 +38,7 @@ const attrExists = (node, attrName, t) =>
3738

3839
module.exports = function componentTaggerBabelPlugin({ types: t }) {
3940
return {
40-
name: "component-path-babel-tagger",
41+
name: babelPluginName,
4142
visitor: {
4243
JSXOpeningElement(nodePath, state) {
4344
const { node } = nodePath

packages/app/src/core/component-path.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
const jsxFilePattern = /\.(tsx|jsx)(\?.*)?$/u
22

3+
// CHANGE: define canonical Babel plugin name for component path tagging.
4+
// WHY: eliminate magic string duplication across plugin implementations.
5+
// QUOTE(ТЗ): "Вынести строки имён плагинов в константы (чтобы не дублировать \"component-path-babel-tagger\")."
6+
// REF: issue-19
7+
// SOURCE: n/a
8+
// FORMAT THEOREM: forall p in PluginName: p = "component-path-babel-tagger"
9+
// PURITY: CORE
10+
// EFFECT: n/a
11+
// INVARIANT: plugin name remains stable across all implementations
12+
// COMPLEXITY: O(1)/O(1)
13+
export const babelPluginName = "component-path-babel-tagger"
14+
315
/**
416
* Normalizes a module ID by stripping query parameters.
517
*

packages/app/src/shell/babel-plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type PluginObj, types as t } from "@babel/core"
22

3-
import { componentPathAttributeName, isJsxFile } from "../core/component-path.js"
3+
import { babelPluginName, componentPathAttributeName, isJsxFile } from "../core/component-path.js"
44
import { createJsxTaggerVisitor, type JsxTaggerContext } from "../core/jsx-tagger.js"
55
import { computeRelativePath } from "../core/path-service.js"
66

@@ -112,7 +112,7 @@ const getContextFromState = (state: BabelState): JsxTaggerContext | null => {
112112
// INVARIANT: each JSX opening element has at most one path attribute
113113
// COMPLEXITY: O(n)/O(1)
114114
export const componentTaggerBabelPlugin = (): PluginObj<BabelState> => ({
115-
name: "component-path-babel-tagger",
115+
name: babelPluginName,
116116
visitor: createJsxTaggerVisitor<BabelState>(getContextFromState, t)
117117
})
118118

packages/app/src/shell/component-tagger.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Path } from "@effect/platform/Path"
33
import { Effect, pipe } from "effect"
44
import type { PluginOption } from "vite"
55

6-
import { componentPathAttributeName, isJsxFile, normalizeModuleId } from "../core/component-path.js"
6+
import { babelPluginName, componentPathAttributeName, isJsxFile, normalizeModuleId } from "../core/component-path.js"
77
import { createJsxTaggerVisitor, type JsxTaggerContext } from "../core/jsx-tagger.js"
88
import { NodePathLayer, relativeFromRoot } from "../core/path-service.js"
99

@@ -56,16 +56,12 @@ const toViteResult = (result: BabelTransformResult): ViteTransformResult | null
5656
// EFFECT: Babel AST transformation
5757
// INVARIANT: each JSX opening element has at most one path attribute
5858
// COMPLEXITY: O(n)/O(1), n = number of JSX elements
59-
type ViteBabelState = {
60-
readonly context: JsxTaggerContext
61-
}
62-
63-
const makeBabelTagger = (relativeFilename: string, attributeName: string): PluginObj<ViteBabelState> => {
59+
const makeBabelTagger = (relativeFilename: string, attributeName: string): PluginObj => {
6460
const context: JsxTaggerContext = { relativeFilename, attributeName }
6561

6662
return {
67-
name: "component-path-babel-tagger",
68-
visitor: createJsxTaggerVisitor<ViteBabelState>(
63+
name: babelPluginName,
64+
visitor: createJsxTaggerVisitor(
6965
() => context,
7066
t
7167
)

0 commit comments

Comments
 (0)