diff --git a/index.js b/index.js index 677e1c21..e3dc5630 100644 --- a/index.js +++ b/index.js @@ -97,7 +97,9 @@ function formatWithCursor(text, opts, addAlignmentSize) { addAlignmentSize = addAlignmentSize || 0; - const ast = parser.parse(text, opts); + const result = parser.parse(text, opts); + const ast = result.ast; + text = result.text; const formattedRangeOnly = formatRange(text, opts, ast); if (formattedRangeOnly) { @@ -432,7 +434,9 @@ module.exports = { }, printToDoc: function(text, opts) { opts = normalizeOptions(opts); - const ast = parser.parse(text, opts); + const result = parser.parse(text, opts); + const ast = result.ast; + text = result.text; attachComments(text, ast, opts); const doc = printAstToDoc(ast, opts); return doc; diff --git a/src/cli/util.js b/src/cli/util.js index 5d7172fb..31cd27a3 100644 --- a/src/cli/util.js +++ b/src/cli/util.js @@ -131,8 +131,14 @@ function format(argv, input, opt) { ); } else { const normalizedOpts = normalizeOptions(opt); - const ast = cleanAST(prettier.__debug.parse(input, opt), normalizedOpts); - const past = cleanAST(prettier.__debug.parse(pp, opt), normalizedOpts); + const ast = cleanAST( + prettier.__debug.parse(input, opt).ast, + normalizedOpts + ); + const past = cleanAST( + prettier.__debug.parse(pp, opt).ast, + normalizedOpts + ); if (ast !== past) { const MAX_AST_SIZE = 2097152; // 2MB diff --git a/src/main/multiparser.js b/src/main/multiparser.js index 3704e658..7390d551 100644 --- a/src/main/multiparser.js +++ b/src/main/multiparser.js @@ -23,7 +23,10 @@ function textToDoc(text, partialNextOptions, parentOptions) { }) ); - const ast = require("./parser").parse(text, nextOptions); + const result = require("./parser").parse(text, nextOptions); + const ast = result.ast; + text = result.text; + const astComments = ast.comments; delete ast.comments; comments.attach(astComments, ast, text, nextOptions); diff --git a/src/main/parser.js b/src/main/parser.js index 6e04267a..80e01eff 100644 --- a/src/main/parser.js +++ b/src/main/parser.js @@ -58,7 +58,14 @@ function parse(text, opts) { const parser = resolveParser(opts, parsers); try { - return parser.parse(text, parsersForCustomParserApi, opts); + if (parser.preprocess) { + text = parser.preprocess(text, opts); + } + + return { + text, + ast: parser.parse(text, parsersForCustomParserApi, opts) + }; } catch (error) { const loc = error.loc; diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index d79c9e4b..2951f9ee 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -112,7 +112,7 @@ function stripLocation(ast) { } function parse(string, opts) { - return stripLocation(prettier.__debug.parse(string, opts)); + return stripLocation(prettier.__debug.parse(string, opts).ast); } function prettyprint(src, filename, options) { diff --git a/tests_integration/__tests__/plugin-preprocess.js b/tests_integration/__tests__/plugin-preprocess.js new file mode 100644 index 00000000..a2efd02d --- /dev/null +++ b/tests_integration/__tests__/plugin-preprocess.js @@ -0,0 +1,12 @@ +"use strict"; + +const runPrettier = require("../runPrettier"); + +describe("parser preprocess function is used to reshape input text", () => { + runPrettier("plugins/preprocess", ["*.foo", "--plugin=./plugin"]).test({ + stdout: "preprocessed:contents\n", + stderr: "", + status: 0, + write: [] + }); +}); diff --git a/tests_integration/plugins/preprocess/file.foo b/tests_integration/plugins/preprocess/file.foo new file mode 100644 index 00000000..12f00e90 --- /dev/null +++ b/tests_integration/plugins/preprocess/file.foo @@ -0,0 +1 @@ +contents diff --git a/tests_integration/plugins/preprocess/plugin.js b/tests_integration/plugins/preprocess/plugin.js new file mode 100644 index 00000000..2405c6c1 --- /dev/null +++ b/tests_integration/plugins/preprocess/plugin.js @@ -0,0 +1,24 @@ +"use strict"; + +module.exports = { + languages: [ + { + name: "foo", + parsers: ["foo-parser"], + extensions: [".foo"], + since: "1.0.0" + } + ], + parsers: { + "foo-parser": { + preprocess: text => `preprocessed:${text}`, + parse: text => ({ text }), + astFormat: "foo-ast" + } + }, + printers: { + "foo-ast": { + print: path => path.getValue().text + } + } +}; diff --git a/website/static/worker.js b/website/static/worker.js index ef6b6908..3262ae13 100644 --- a/website/static/worker.js +++ b/website/static/worker.js @@ -84,7 +84,7 @@ self.onmessage = function(message) { var actualAst; var errored = false; try { - actualAst = prettier.__debug.parse(message.data.text, options); + actualAst = prettier.__debug.parse(message.data.text, options).ast; ast = JSON.stringify(actualAst); } catch (e) { errored = true;