forked from withastro/prettier-plugin-astro
-
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.
Correctly pass options to embedded parsers (withastro#339)
* Correctly pass options to embedded parsers * Add changeset * Add ignore and comment about why the types don’t match * Add test
- Loading branch information
1 parent
9cb4c4f
commit 88b0d84
Showing
8 changed files
with
99 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'prettier-plugin-astro': patch | ||
--- | ||
|
||
Correctly pass options to embedded parsers |
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
59 changes: 59 additions & 0 deletions
59
test/fixtures/other/embedded-expr-options/custom-plugin.js
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
let prettierParserBabel = require('prettier/parser-babel'); | ||
|
||
module.exports.options = { | ||
customPluginClass: { | ||
since: '1.0.0', | ||
category: 'foo', | ||
type: 'string', | ||
default: 'my-default-class', | ||
description: 'Replace all classes with this one.', | ||
}, | ||
}; | ||
|
||
let original = prettierParserBabel.parsers['babel-ts']; | ||
|
||
/** @type {Record<string, import('prettier').Parser<any>>} */ | ||
module.exports.parsers = { | ||
'babel-ts': { | ||
parse(text, parsers, options) { | ||
let ast = original.parse(text, parsers, options); | ||
|
||
let nodes = [ast.program]; | ||
while (nodes.length) { | ||
let node = nodes.shift(); | ||
switch (node.type) { | ||
case 'Program': | ||
nodes.push(...node.body); | ||
break; | ||
case 'ExpressionStatement': | ||
nodes.push(node.expression); | ||
break; | ||
case 'JSXExpressionContainer': | ||
nodes.push(node.expression); | ||
break; | ||
case 'JSXFragment': | ||
nodes.push(...node.children); | ||
break; | ||
case 'JSXElement': | ||
nodes.push(node.openingElement); | ||
nodes.push(...node.children); | ||
break; | ||
case 'JSXOpeningElement': | ||
nodes.push(...node.attributes); | ||
break; | ||
case 'JSXAttribute': | ||
if (node.name && node.name.type === 'JSXIdentifier' && node.name.name === 'class') { | ||
node.value.value = `${options.customPluginClass}`; | ||
node.value.extra = { | ||
rawValue: node.value.value, | ||
raw: `"${node.value.value}"`, | ||
}; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
return ast; | ||
}, | ||
}, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<div> | ||
{ | ||
<div class="foo bar"></div> | ||
} | ||
</div> |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
plugins: [require.resolve('../../../../'), require.resolve('./custom-plugin.js')], | ||
customPluginClass: 'my-custom-class', | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div> | ||
{(<div class="my-custom-class" />)} | ||
</div> |
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