generated from ony3000/node-library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from ony3000/semi-rework-again
Partially reworked the plugin
- Loading branch information
Showing
9 changed files
with
234 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,6 @@ | |
"prepare-release": "pnpm run build:plain && npm version prerelease --preid=alpha --git-tag-version=false && pnpm run merge-deps && npm pack" | ||
}, | ||
"devDependencies": { | ||
"@prettier/sync": "0.3.0", | ||
"@trivago/prettier-plugin-sort-imports": "4.2.1", | ||
"@types/node": "20.2.5", | ||
"@types/prettier": "2.7.3", | ||
|
@@ -50,14 +49,8 @@ | |
"zod": "3.22.4" | ||
}, | ||
"peerDependencies": { | ||
"@prettier/sync": "^0.3.0", | ||
"prettier": "^2 || ^3" | ||
}, | ||
"peerDependenciesMeta": { | ||
"@prettier/sync": { | ||
"optional": true | ||
} | ||
}, | ||
"packageManager": "[email protected]", | ||
"engines": { | ||
"node": ">=14" | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,65 @@ | ||
import type { Parser } from 'prettier'; | ||
import { parseLineByLineAndReplace } from 'core-parts'; | ||
import type { Parser, ParserOptions } from 'prettier'; | ||
import { format } from 'prettier'; | ||
import { parsers as babelParsers } from 'prettier/parser-babel'; | ||
import { parsers as htmlParsers } from 'prettier/parser-html'; | ||
import { parsers as typescriptParsers } from 'prettier/parser-typescript'; | ||
|
||
const addon = { | ||
parseBabel: (text: string, options: ParserOptions) => | ||
babelParsers.babel.parse(text, { babel: babelParsers.babel }, options), | ||
parseTypescript: (text: string, options: ParserOptions) => | ||
typescriptParsers.typescript.parse(text, { typescript: typescriptParsers.typescript }, options), | ||
}; | ||
|
||
function transformParser( | ||
parserName: 'babel' | 'typescript' | 'vue', | ||
defaultParser: Parser, | ||
): Parser { | ||
return { | ||
...defaultParser, | ||
parse: (text: string, parsers: { [parserName: string]: Parser }, options: ParserOptions) => { | ||
const firstFormattedText = format(text, { | ||
...options, | ||
plugins: [], | ||
endOfLine: 'lf', | ||
}); | ||
const ast = defaultParser.parse(firstFormattedText, { [parserName]: defaultParser }, options); | ||
|
||
const classNameWrappedText = parseLineByLineAndReplace( | ||
firstFormattedText, | ||
ast, | ||
// @ts-ignore | ||
options, | ||
format, | ||
addon, | ||
); | ||
|
||
if (parserName === 'vue') { | ||
return { | ||
type: 'FormattedText', | ||
body: classNameWrappedText, | ||
}; | ||
} | ||
|
||
const secondFormattedText = format(classNameWrappedText, { | ||
...options, | ||
plugins: [], | ||
endOfLine: 'lf', | ||
rangeEnd: Infinity, | ||
}); | ||
|
||
return { | ||
type: 'FormattedText', | ||
body: secondFormattedText, | ||
}; | ||
}, | ||
astFormat: 'classnames-ast', | ||
}; | ||
} | ||
|
||
export const parsers: { [parserName: string]: Parser } = { | ||
babel: { | ||
...babelParsers.babel, | ||
astFormat: 'babel-ast', | ||
}, | ||
typescript: { | ||
...typescriptParsers.typescript, | ||
astFormat: 'typescript-ast', | ||
}, | ||
vue: { | ||
...htmlParsers.vue, | ||
astFormat: 'vue-ast', | ||
}, | ||
babel: transformParser('babel', babelParsers.babel), | ||
typescript: transformParser('typescript', typescriptParsers.typescript), | ||
vue: transformParser('vue', htmlParsers.vue), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
"author": "Hyeonjong <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@prettier/sync": "0.3.0", | ||
"core-parts": "workspace:*", | ||
"prettier": "3.0.3" | ||
} | ||
|
Oops, something went wrong.