Skip to content

Commit

Permalink
Stringifier#ArrowFunctionExpression: remove extra space when not ne…
Browse files Browse the repository at this point in the history
…eded (#193)

* Stringifier#ArrowFunctionExpression: remove extra space after `=>` when not needed

* Add unit test
  • Loading branch information
kungfooman authored Jul 15, 2024
1 parent 47917aa commit 740bf5f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src-transpiler/Stringifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,10 @@ class Stringifier {
out += ' * ';
}
out += this.FunctionDeclarationParams(params);
out += ' => ';
out += ' =>';
if (body.type !== 'BlockStatement') {
out += ' ';
}
out += this.toSource(body);
return out;
}
Expand Down
11 changes: 9 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import {expandType } from './src-transpiler/expandType.js';
import {Asserter } from './src-transpiler/Asserter.js';
import {Stringifier } from './src-transpiler/Stringifier.js';
import {parserOptions} from './src-transpiler/parserOptions.js';
/**
* @typedef InputOutput
* @property {string} input
* @property {string} output
*/
/**
* @param {string} a - Left source code.
* @param {string} b - Right source code.
Expand All @@ -27,10 +32,11 @@ function compareLineByLine(a, b) {
console.log(t);
}
const content = readFileSync('./test/typechecking.json', 'utf8');
/** @type {InputOutput[]} */
const tests = JSON.parse(content);
let discrepancies = 0;
/**
* @param {*} input - Source code to normalize.
* @param {string} input - Source code to normalize.
* @returns {string} Normalized output.
*/
function normalize(input) {
Expand All @@ -49,7 +55,8 @@ function normalize(input) {
for (const {input, output} of tests) {
const inputContent = readFileSync(input, 'utf8');
const outputContent = readFileSync(output, 'utf8');
const Converter = input.includes('jsx') ? Stringifier : Asserter;
const useStringifer = input.includes('stringifier-') || input.includes('jsx');
const Converter = useStringifer ? Stringifier : Asserter;
const converter = new Converter({
expandType,
addHeader: input.includes('jsx'),
Expand Down
4 changes: 4 additions & 0 deletions test/typechecking.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@
"input": "./test/typechecking/simple-ObjectPattern-typedef-input.mjs",
"output": "./test/typechecking/simple-ObjectPattern-typedef-output.mjs"
},
{
"input": "./test/typechecking/stringifier-ArrowFunctionExpression-spaces-input.mjs",
"output": "./test/typechecking/stringifier-ArrowFunctionExpression-spaces-output.mjs"
},
{
"input": "./test/typechecking/template-types-1-input.mjs",
"output": "./test/typechecking/template-types-1-output.mjs"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const a = () => {
console.log("test");
};
const b = () => 123;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const a = () => {
console.log("test");
};
const b = () => 123;

0 comments on commit 740bf5f

Please sign in to comment.