Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@
"author": "Suchipi <[email protected]>",
"license": "MIT",
"main": "src",
"engines": {
"node": ">=10.13.0"
},
"files": [
"src"
],
"dependencies": {
"luaparse": "0.2.1"
},
"devDependencies": {
"cross-env": "^5.2.0",
"eslint": "4.19.1",
"eslint-config-unobtrusive": "^1.2.2",
"eslint-plugin-import": "2.10.0",
"eslint-plugin-prettier": "2.6.0",
"jest": "22.4.3",
"jest-runner-eslint": "0.4.0",
"prettier-from-github": "prettier/prettier#4c464133ac37a966c792cacc660853d74c776841",
"prettier": "1.13.7"
"cross-env": "^7.0.2",
"eslint": "^7.7.0",
"eslint-config-unobtrusive": "^1.2.5",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-prettier": "^3.1.4",
"jest": "^24.9.0",
"jest-runner-eslint": "^0.10.0",
"prettier": "^2.1.1"
},
"peerDependencies": {
"prettier": ">=1.0.0"
"prettier": ">=2.0.0"
},
"scripts": {
"test": "cross-env AST_COMPARE=1 jest",
Expand All @@ -37,3 +39,4 @@
]
}
}

4 changes: 2 additions & 2 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ function printBody(path, options, print) {
}

if (
isNextLineEmpty(text, statement, options) &&
isNextLineEmpty(text, statement, options["locEnd"]) &&
!isLastStatement(statementPath)
) {
parts.push(hardline);
Expand Down Expand Up @@ -614,7 +614,7 @@ function printArgumentsList(path, options, print) {

if (index === lastArgIndex) {
// do nothing
} else if (isNextLineEmpty(options.originalText, arg, options)) {
} else if (isNextLineEmpty(options.originalText, arg, options["locEnd"])) {
if (index === 0) {
hasEmptyLineFollowingFirstArg = true;
}
Expand Down
67 changes: 21 additions & 46 deletions tests_config/run_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
const fs = require("fs");
const { extname } = require("path");
const prettier = require("prettier");
const plugin = require("../src");
const { massageAST } = require("prettier-from-github/src/common/clean-ast");
const { normalize } = require("prettier-from-github/src/main/options");

const { AST_COMPARE } = process.env;

Expand All @@ -30,16 +27,20 @@ function run_spec(dirname, parsers, options) {
filename[0] !== "." &&
filename !== "jsfmt.spec.js"
) {
const source = read(path).replace(/\r\n/g, "\n");
let source = read(path);

if (!options.keepEOL) {
source = source.replace(/\r\n/g, "\n");
}

const mergedOptions = Object.assign(mergeDefaultOptions(options || {}), {
parser: parsers[0],
});
const output = prettyprint(source, path, mergedOptions);
test(`${filename} - ${mergedOptions.parser}-verify`, () => {
expect(raw(`${source + "~".repeat(80)}\n${output}`)).toMatchSnapshot(
filename
);
expect(
raw(`${source + "~".repeat(mergedOptions.printWidth)}\n${output}`)
).toMatchSnapshot(filename);
});

parsers.slice(1).forEach((parserName) => {
Expand All @@ -52,26 +53,23 @@ function run_spec(dirname, parsers, options) {
});
});

if (AST_COMPARE) {
const normalizedOptions = normalize(mergedOptions);
const ast = parse(source, mergedOptions);
const astMassaged = massageAST(ast, normalizedOptions);
if (AST_COMPARE && parsers.slice(0) === "lua") {
const compareOptions = Object.assign({}, mergedOptions);
const astMassaged = parse(source, compareOptions);
let ppastMassaged;
let pperr = null;
try {
const ppast = parse(
prettyprint(source, path, mergedOptions),
mergedOptions
expect(() => {
ppastMassaged = parse(
prettyprint(source, path, compareOptions),
compareOptions
);
ppastMassaged = massageAST(ppast, normalizedOptions);
} catch (e) {
pperr = e.stack;
}
}).not.toThrow();

expect(ppastMassaged).toBeDefined();
test(`${path} parse`, () => {
expect(pperr).toBe(null);
expect(ppastMassaged).toBeDefined();
if (!ast.errors || ast.errors.length === 0) {
if (!astMassaged.errors || astMassaged.errors.length === 0) {
expect(astMassaged).toEqual(ppastMassaged);
}
});
Expand All @@ -81,32 +79,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 (
key === "loc" ||
key === "range" ||
key === "raw" ||
key === "comments" ||
key === "parent" ||
key === "prev"
) {
continue;
}
newObj[key] = stripLocation(ast[key]);
}
return newObj;
}
return ast;
}

function parse(string, opts) {
return stripLocation(plugin.parsers.lua.parse(string, {}, opts));
return prettier.__debug.parse(string, opts, /* massage */ true).ast;
}

function prettyprint(src, filename, options) {
Expand All @@ -122,7 +96,7 @@ function prettyprint(src, filename, options) {
}

function read(filename) {
return fs.readFileSync(filename, "utf-8");
return fs.readFileSync(filename, "utf8");
}

/**
Expand All @@ -141,6 +115,7 @@ function mergeDefaultOptions(parserConfig) {
return Object.assign(
{
printWidth: 80,
trailingComma: "none",
},
parserConfig
);
Expand Down
Loading