Allow Plugins to preprocess text (#3664)
* Allow Plugins to preprocess text * Actually pull up text * Always expect the preprocessor to set the text * Add tests for preprocessing * Remove unused variablemaster
parent
01a59eeee3
commit
a27d19b2c1
8
index.js
8
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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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: []
|
||||
});
|
||||
});
|
|
@ -0,0 +1 @@
|
|||
contents
|
|
@ -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
|
||||
}
|
||||
}
|
||||
};
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue