refactor(massageAst): move stripLocation to corresponding plugins (#4477)

master
Ika 2018-05-14 20:52:27 +08:00 committed by GitHub
parent 6cd6ea383e
commit d0f6aac001
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 31 deletions

View File

@ -643,8 +643,14 @@ function determineInterfaceSeparator(originalSource) {
return originalSource.substr(start, end).includes("&") ? " & " : ", ";
}
function clean(node, newNode /*, parent*/) {
delete newNode.loc;
delete newNode.comments;
}
module.exports = {
print: genericPrint,
massageAstNode: clean,
hasPrettierIgnore: privateUtil.hasIgnoreComment,
printComment,
canAttachComment

View File

@ -309,6 +309,8 @@ function printCloseBlock(path, print) {
}
function clean(ast, newObj) {
delete newObj.loc;
// (Glimmer/HTML) ignore TextNode whitespace
if (ast.type === "TextNode") {
if (ast.chars.replace(/\s+/, "") === "") {

View File

@ -1,6 +1,8 @@
"use strict";
module.exports = function(ast) {
module.exports = function(ast, newNode) {
delete newNode.__location;
if (ast.type === "text") {
return null;
}

View File

@ -2,6 +2,9 @@
function clean(ast, newObj, parent) {
[
"range",
"raw",
"comments",
"leadingComments",
"trailingComments",
"extra",

View File

@ -61,6 +61,8 @@ function clean(node, newNode /*, parent*/) {
delete newNode.start;
delete newNode.end;
delete newNode.extra;
delete newNode.loc;
delete newNode.comments;
if (node.type === "Identifier") {
return { type: "StringLiteral", value: node.name };

View File

@ -89,37 +89,8 @@ function run_spec(dirname, parsers, options) {
global.run_spec = run_spec;
function stripLocation(ast) {
if (Array.isArray(ast)) {
return ast.map(e => stripLocation(e));
}
if (typeof ast === "object") {
const newObj = {};
for (const key in ast) {
if (
[
"loc",
"range",
"raw",
"comments",
"parent",
"prev",
"__location"
].indexOf(key) !== -1
) {
continue;
}
newObj[key] = stripLocation(ast[key]);
}
return newObj;
}
return ast;
}
function parse(string, opts) {
return stripLocation(
prettier.__debug.parse(string, opts, /* massage */ true).ast
);
return prettier.__debug.parse(string, opts, /* massage */ true).ast;
}
function prettyprint(src, filename, options) {