prettier-plugin-hermes-parser
Advanced tools
Comparing version 0.12.0 to 0.12.1
@@ -12,4 +12,2 @@ /** | ||
var _hermesParser = require("hermes-parser"); | ||
// copied from https://github.com/prettier/prettier/blob/20ab6d6f1c5bd774621230b493a3b71d39383a2c/src/language-js/parse/utils/replace-hashbang.js | ||
@@ -25,4 +23,9 @@ function replaceHashbang(text) { | ||
function parse(originalText) { | ||
const { | ||
parse: hermesParserParse, | ||
mutateESTreeASTForPrettier | ||
} = require('hermes-parser'); | ||
const textToParse = replaceHashbang(originalText); | ||
const result = (0, _hermesParser.parse)(textToParse, { | ||
const result = hermesParserParse(textToParse, { | ||
allowReturnOutsideFunction: true, | ||
@@ -34,3 +37,3 @@ enableExperimentalComponentSyntax: true, | ||
}); | ||
(0, _hermesParser.mutateESTreeASTForPrettier)(result); | ||
mutateESTreeASTForPrettier(result); | ||
return result; | ||
@@ -45,11 +48,15 @@ } | ||
function createPrettierV3HermesPlugin() { | ||
// Lazy require this module as its only available in prettier v3. | ||
const flowPlugin = require('prettier/plugins/flow'); | ||
return { | ||
// $FlowExpectedError[unsafe-getters-setters] | ||
get parsers() { | ||
// Lazy require this module as its only available in prettier v3. | ||
const flowPlugin = require('prettier/plugins/flow'); | ||
return { | ||
parsers: { | ||
hermes: { ...flowPlugin.parsers.flow, | ||
parse | ||
} | ||
return { | ||
hermes: { ...flowPlugin.parsers.flow, | ||
parse | ||
} | ||
}; | ||
} | ||
}; | ||
@@ -66,8 +73,5 @@ } | ||
const { | ||
flowPlugin, | ||
estreePlugin, | ||
graphqlPlugin, | ||
postcssPlugin, | ||
htmlPlugin, | ||
markdownPlugin, | ||
getFlowPlugin, | ||
getESTreePlugin, | ||
getEmbeddedESTreePlugins, | ||
printAstToDoc | ||
@@ -77,73 +81,96 @@ } = require('./third-party/internal-prettier-v3'); | ||
return { | ||
parsers: { | ||
hermes: { // $FlowExpectedError[incompatible-use] We know it exists | ||
...flowPlugin.parsers.flow, | ||
// Switch to our own estree printer | ||
astFormat: 'estree-v3', | ||
// $FlowExpectedError[unsafe-getters-setters] | ||
get parsers() { | ||
return { | ||
hermes: { // $FlowExpectedError[incompatible-use] We know it exists | ||
...getFlowPlugin().parsers.flow, | ||
// Switch to our own estree printer | ||
astFormat: 'estree-v3', | ||
parse(originalText) { | ||
const ast = parse(originalText); // We don't want prettier v2 to try attaching these comments | ||
// so we hide them under a different name. | ||
parse(originalText) { | ||
const ast = parse(originalText); // We don't want prettier v2 to try attaching these comments | ||
// so we hide them under a different name. | ||
if (ast.comments != null) { | ||
// $FlowExpectedError[prop-missing] | ||
ast.__comments = ast.comments; // $FlowExpectedError[cannot-write] | ||
if (ast.comments != null) { | ||
// $FlowExpectedError[prop-missing] | ||
ast.__comments = ast.comments; // $FlowExpectedError[cannot-write] | ||
delete ast.comments; | ||
delete ast.comments; | ||
} | ||
return ast; | ||
} | ||
return ast; | ||
} | ||
} | ||
}; | ||
}, | ||
printers: { | ||
'estree-v3': { // $FlowExpectedError[incompatible-use] We know it exists | ||
...estreePlugin.printers.estree, | ||
// Skip prettier v2 trying to print comments for the top level node. | ||
willPrintOwnComments() { | ||
return true; | ||
}, | ||
// $FlowExpectedError[unsafe-getters-setters] | ||
get printers() { | ||
const estreePlugin = getESTreePlugin(); | ||
return { | ||
'estree-v3': { // $FlowExpectedError[incompatible-use] We know it exists | ||
...estreePlugin.printers.estree, | ||
// Skip prettier v2 trying to handle prettier ignore comments. | ||
hasPrettierIgnore() { | ||
return false; | ||
}, | ||
// Skip prettier v2 trying to print comments for the top level node. | ||
willPrintOwnComments() { | ||
return true; | ||
}, | ||
// Override printer and instead call the prettier v3 printing logic. | ||
print(path, options) { | ||
// Get top level AST node from prettier v2. | ||
const ast = path.getValue(); // Reattach comments to AST | ||
// $FlowExpectedError[prop-missing] | ||
// Skip prettier v2 trying to handle prettier ignore comments. | ||
hasPrettierIgnore() { | ||
return false; | ||
}, | ||
if (ast.__comments != null) { | ||
// $FlowExpectedError[cannot-write] | ||
ast.comments = ast.__comments; // $FlowExpectedError[prop-missing] | ||
// Override printer and instead call the prettier v3 printing logic. | ||
print(path, options) { | ||
var _estreePlugin$printer, _estreePlugin$printer2, _estreePlugin$printer3; | ||
delete ast.__comments; | ||
} // Override prettier v2 options for prettier v3. | ||
// Get top level AST node from prettier v2. | ||
const ast = path.getValue(); // Reattach comments to AST | ||
// $FlowExpectedError[prop-missing] | ||
if (ast.__comments != null) { | ||
// $FlowExpectedError[cannot-write] | ||
ast.comments = ast.__comments; // $FlowExpectedError[prop-missing] | ||
const v3Options = { ...options, | ||
// Update plugins list to use prettier v3 plugins when processing | ||
// embedded syntax found by the prettier v3 estree printer. | ||
plugins: [graphqlPlugin, markdownPlugin, htmlPlugin, postcssPlugin], | ||
printer: { // $FlowExpectedError[prop-missing] We know it exists | ||
...options.printer, | ||
// $FlowExpectedError[incompatible-use] We know it exists | ||
print: estreePlugin.printers.estree.print, | ||
// Rename options overridden for prettier v2 above. | ||
willPrintOwnComments: // $FlowExpectedError[incompatible-use] We know it exists | ||
estreePlugin.printers.estree.willPrintOwnComments, | ||
// $FlowExpectedError[incompatible-use] We know it exists | ||
hasPrettierIgnore: estreePlugin.printers.estree.hasPrettierIgnore | ||
} | ||
}; // Call prettier v3 printing logic. This will recursively print the AST. | ||
delete ast.__comments; | ||
} // Collect existing V2 plugins that have not changed in V3. | ||
return printAstToDoc(ast, v3Options); | ||
const existingPlugins = options.plugins.filter(plugin => { | ||
if (typeof plugin === 'string') { | ||
return false; | ||
} | ||
const printers = plugin.printers; | ||
if (printers == null) { | ||
return false; | ||
} | ||
return Object.keys(printers).some(printer => // Markdown plugin | ||
printer === 'mdast' || // HTML plugin | ||
printer === 'html'); | ||
}); // Override prettier v2 options for prettier v3. | ||
const v3Options = { ...options, | ||
// Update plugins list to use prettier v3 plugins when processing | ||
// embedded syntax found by the prettier v3 estree printer. | ||
plugins: [...existingPlugins, ...getEmbeddedESTreePlugins()], | ||
printer: { ...options.printer, | ||
print: (_estreePlugin$printer = estreePlugin.printers) == null ? void 0 : _estreePlugin$printer.estree.print, | ||
// Rename options overridden for prettier v2 above. | ||
willPrintOwnComments: (_estreePlugin$printer2 = estreePlugin.printers) == null ? void 0 : _estreePlugin$printer2.estree.willPrintOwnComments, | ||
hasPrettierIgnore: (_estreePlugin$printer3 = estreePlugin.printers) == null ? void 0 : _estreePlugin$printer3.estree.hasPrettierIgnore | ||
} | ||
}; // Call prettier v3 printing logic. This will recursively print the AST. | ||
return printAstToDoc(ast, v3Options); | ||
} | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
}; | ||
@@ -150,0 +177,0 @@ } |
@@ -6,22 +6,7 @@ "use strict"; | ||
}); | ||
exports.printAstToDoc = exports.postcssPlugin = exports.markdownPlugin = exports.htmlPlugin = exports.graphqlPlugin = exports.flowPlugin = exports.estreePlugin = void 0; | ||
exports.getESTreePlugin = getESTreePlugin; | ||
exports.getEmbeddedESTreePlugins = getEmbeddedESTreePlugins; | ||
exports.getFlowPlugin = getFlowPlugin; | ||
exports.printAstToDoc = printAstToDoc; | ||
var flowPluginUntyped = _interopRequireWildcard(require("./plugins/flow")); | ||
var estreePluginUntyped = _interopRequireWildcard(require("./plugins/estree")); | ||
var graphqlPluginUntyped = _interopRequireWildcard(require("./plugins/graphql")); | ||
var postcssPluginUntyped = _interopRequireWildcard(require("./plugins/postcss")); | ||
var htmlPluginUntyped = _interopRequireWildcard(require("./plugins/html")); | ||
var markdownPluginUntyped = _interopRequireWildcard(require("./plugins/markdown")); | ||
var _astToDoc = require("./ast-to-doc.js"); | ||
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | ||
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
/** | ||
@@ -36,15 +21,16 @@ * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
*/ | ||
const flowPlugin = flowPluginUntyped; | ||
exports.flowPlugin = flowPlugin; | ||
const estreePlugin = estreePluginUntyped; | ||
exports.estreePlugin = estreePlugin; | ||
const graphqlPlugin = graphqlPluginUntyped; | ||
exports.graphqlPlugin = graphqlPlugin; | ||
const postcssPlugin = postcssPluginUntyped; | ||
exports.postcssPlugin = postcssPlugin; | ||
const htmlPlugin = htmlPluginUntyped; | ||
exports.htmlPlugin = htmlPlugin; | ||
const markdownPlugin = markdownPluginUntyped; | ||
exports.markdownPlugin = markdownPlugin; | ||
const printAstToDoc = _astToDoc.printAstToDoc; | ||
exports.printAstToDoc = printAstToDoc; | ||
function getFlowPlugin() { | ||
return require('./plugins/flow'); | ||
} | ||
function getESTreePlugin() { | ||
return require('./plugins/estree'); | ||
} | ||
function getEmbeddedESTreePlugins() { | ||
return [require('./plugins/graphql'), require('./plugins/postcss')]; | ||
} | ||
function printAstToDoc(program, options) { | ||
return require('./ast-to-doc').printAstToDoc(program, options); | ||
} |
{ | ||
"name": "prettier-plugin-hermes-parser", | ||
"version": "0.12.0", | ||
"version": "0.12.1", | ||
"description": "Hermes parser plugin for Prettier.", | ||
@@ -12,4 +12,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"hermes-estree": "0.12.0", | ||
"hermes-parser": "0.12.0" | ||
"hermes-estree": "0.12.1", | ||
"hermes-parser": "0.12.1" | ||
}, | ||
@@ -16,0 +16,0 @@ "peerDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1108932
14
28405
+ Addedhermes-estree@0.12.1(transitive)
+ Addedhermes-parser@0.12.1(transitive)
- Removedhermes-estree@0.12.0(transitive)
- Removedhermes-parser@0.12.0(transitive)
Updatedhermes-estree@0.12.1
Updatedhermes-parser@0.12.1