Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Js node range #595

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
38 changes: 30 additions & 8 deletions src/language-js/postprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

const {
getLast,
getPenultimate,
getNextNonSpaceNonCommentCharacter,
getShebang,
} = require("../common/util");
const { composeLoc, locStart, locEnd } = require("./loc");
const { isTypeCastComment } = require("./comments");
const { isSemicolonToken } = require("./utils");

function postprocess(ast, options) {
if (options.parser === "typescript" || options.parser === "flow") {
Expand Down Expand Up @@ -112,6 +114,30 @@ function postprocess(ast, options) {
}
});

// Remove `;` from range
const { tokens } = ast;
if (Array.isArray(tokens) && tokens.length) {
ast = visitNode(ast, (node) => {
if (node && node.type) {
const start = locStart(node);
const end = locEnd(node);

const tokensInside = tokens.filter(
(token) => locStart(token) >= start && locEnd(token) <= end
);

const lastToken = getLast(tokensInside);

if (isSemicolonToken(lastToken)) {
const penultimateToken = getPenultimate(tokensInside);
if (penultimateToken && locEnd(penultimateToken) !== end) {
node.range = [start, locEnd(penultimateToken)];
}
}
}
});
}

return ast;

/**
Expand All @@ -122,14 +148,10 @@ function postprocess(ast, options) {
if (options.originalText[locEnd(toOverrideNode)] === ";") {
return;
}
if (Array.isArray(toBeOverriddenNode.range)) {
toBeOverriddenNode.range = [
toBeOverriddenNode.range[0],
toOverrideNode.range[1],
];
} else {
toBeOverriddenNode.end = toOverrideNode.end;
}
toBeOverriddenNode.range = [
locStart(toBeOverriddenNode),
locEnd(toOverrideNode),
];
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/language-js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,18 @@ function isBitwiseOperator(operator) {
);
}

function isSemicolonToken(token) {
return (
token &&
// babel
((token.type && token.type.label === ";") ||
// typescript
(token.type === "Punctuator" && token.value === ";") ||
// flow
(token.type === "T_SEMICOLON" && token.value === ";"))
);
}

module.exports = {
classChildNeedsASIProtection,
classPropMayCauseASIProblems,
Expand Down Expand Up @@ -1208,6 +1220,7 @@ module.exports = {
isTheOnlyJSXElementInMarkdown,
isTSXFile,
isTypeAnnotationAFunction,
isSemicolonToken,
matchJsxWhitespaceRegex,
needsHardlineAfterDanglingComment,
rawText,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ export type InferredType =
| "number"
| "string"
| "error";

export type Pos = {
firstLine: number,
firstColumn: number,
Expand Down Expand Up @@ -1016,7 +1015,6 @@ export type BinaryOperator =
| "in"
| "instanceof"
| "..";

export type UnaryOperator =
| "-"
| "+"
Expand All @@ -1025,7 +1023,6 @@ export type UnaryOperator =
| "typeof"
| "void"
| "delete";

export type AssignmentOperator =
| "="
| "+="
Expand All @@ -1039,11 +1036,8 @@ export type AssignmentOperator =
| "|="
| "^="
| "&=";

export type UpdateOperator = "++" | "--";

export type LogicalOperator = "&&" | "||";

export type Node =
| EmptyStatement
| BlockStatement
Expand Down Expand Up @@ -1077,7 +1071,6 @@ export type Node =
| VariableDeclaration
| FunctionDeclaration
| VariableDeclarator;

export type Statement =
| BlockStatement
| EmptyStatement
Expand All @@ -1092,7 +1085,6 @@ export type Statement =
| ForInStatement
| TryStatement
| Declaration;

export type EmptyStatement = {
source: ?string,
start: SourcePosition,
Expand Down Expand Up @@ -1238,7 +1230,6 @@ export type Expression =
| MemberExpression
| ArrayExpression
| ObjectExpreession;

export type Identifier = {
source: ?string,
start: SourcePosition,
Expand Down Expand Up @@ -1405,9 +1396,7 @@ export type MemberExpression = {
// ast-types exports all expressions as patterns.
// That seems not like it was intended.
export type Pattern = Identifier;

export type Declaration = VariableDeclaration | FunctionDeclaration;

export type VariableDeclaration = {
source: ?string,
start: SourcePosition,
Expand Down
1 change: 0 additions & 1 deletion tests/flow-repo/recheck/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ export const LIFE = 42;
// @flow

export type Action = { type: "FOO" } | { type: "BAR" };

export const LIFE = 42;

================================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const LIFE = 42;
// @flow

export type Action = { type: "QUX" } | { type: "BAR" };

export const LIFE = 42;

================================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const LIFE = 0;
// @flow

export type Action = { type: "QUX" } | { type: "BAR" };

export const LIFE = 0;

================================================================================
Expand Down
1 change: 0 additions & 1 deletion tests/flow/comments/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class A {
=====================================output=====================================
class A {
x /*: string */;

method(a /*: T */, b /*: T */) /*: T */ {}
}

Expand Down
2 changes: 0 additions & 2 deletions tests/js/for/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ y);
=====================================output=====================================
/*a*/
for (x in y); //b //c

for (x /*a*/ in y); //b //c

for (x /*a*/ in y); //b //c
Expand All @@ -60,7 +59,6 @@ for (x in y);

/*a*/
for (x of y); //b //c

for (x /*a*/ of y); //b //c

for (x /*a*/ of y); //b //c
Expand Down
10 changes: 5 additions & 5 deletions tests/js/ignore/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,25 @@ function a() {
'var fn=' + this.generateFunction('fn', 's,l,a,i') +
extra +
this.watchFns() +
'return fn;';
'return fn;'

// prettier-ignore
const identity = Matrix.create(
1, 0, 0,
0, 1, 0,
0, 0, 0
);
)

// Let's make sure that this comment doesn't interfere

// prettier-ignore
const commentsWithPrettierIgnore = {
"ewww":
"gross-formatting",
};
}

function giveMeSome() {
a( a ); // prettier-ignore
a( a ) // prettier-ignore
// shouldn't I return something? :shrug:
}

Expand All @@ -127,7 +127,7 @@ function a() {
'\\n' +
'Once this is done, commit anything to the repository to restart ' +
'Travis and it should work :)'
);
)

// Incorrectly indented on purpose
function f</* prettier-ignore */ T : B>(
Expand Down
20 changes: 13 additions & 7 deletions tests/js/range/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ printWidth: 80
2 | 'bbb';
3 |
=====================================output=====================================
"aaa";
"aaa";;
'bbb';

================================================================================
Expand Down Expand Up @@ -269,7 +269,7 @@ printWidth: 80
=====================================output=====================================
export const Button = styled.button\`
color: blue;
\`;
\`;;

================================================================================
`;
Expand Down Expand Up @@ -299,7 +299,7 @@ function ugly() {
anotherKey: "another value",
firstNumber: 1,
secondNumber: 2,
};
};;
}

================================================================================
Expand Down Expand Up @@ -464,7 +464,7 @@ call(

call(1, 2, 3);

call(1, 2, 3);
call(1, 2, 3);;

call(
1, 2,3
Expand Down Expand Up @@ -509,9 +509,13 @@ call(
1, 2,3
);

call(1, 2, 3);
call(
1, 2,3
);

call(1, 2, 3);
call(
1, 2,3
);

call(
1, 2,3
Expand Down Expand Up @@ -758,7 +762,9 @@ printWidth: 80
9 | );
10 |
=====================================output=====================================
call(1, 2, 3);
call(
1, 2,3
);


// Unchanged
Expand Down
16 changes: 8 additions & 8 deletions tests/typescript/interface/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ interface Interface {
// prettier-ignore
prop: type
// prettier-ignore
prop: type;
prop: type
prop: type
}

Expand All @@ -249,15 +249,15 @@ interface Interface {

interface foo extends bar {
// prettier-ignore
f(): void;
f(): void
// prettier-ignore
g(): void;
g(): void
h(): void
}

interface T<T> {
// prettier-ignore
new<T>(): T<T>;
new<T>(): T<T>
new <T>(): T<T>
}

Expand Down Expand Up @@ -304,7 +304,7 @@ interface Interface {
// prettier-ignore
prop: type
// prettier-ignore
prop: type;
prop: type
prop: type;
}

Expand All @@ -317,15 +317,15 @@ interface Interface {

interface foo extends bar {
// prettier-ignore
f(): void;
f(): void
// prettier-ignore
g(): void;
g(): void
h(): void;
}

interface T<T> {
// prettier-ignore
new<T>(): T<T>;
new<T>(): T<T>
new <T>(): T<T>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ let b11: /*1*/ /*2*/ C;

let bb1: /*1*/ /*2*/ C & D;
let bb2: /*1*/ /*2*/ C & /*3*/ D;
let bb3: /*1*/ /*2*/ C & /*3*/ D /*5*/;
let bb3: /*1*/ /*2*/ C & /*3*/ D; /*5*/

type B2 = C;
type B3 = C;
Expand All @@ -185,7 +185,7 @@ type B12 = /*1*/ C;

type Bb1 = /*1*/ /*2*/ C & D;
type Bb2 = /*1*/ /*2*/ C & /*3*/ D;
type Bb3 = /*1*/ /*2*/ C & /*3*/ D /*4*/;
type Bb3 = /*1*/ /*2*/ C & /*3*/ D; /*4*/

type D1 = /*1*/ a & b;
type D2 = /*1*/ a & b;
Expand Down
2 changes: 1 addition & 1 deletion tests/typescript/range/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ printWidth: 80
4 |
=====================================output=====================================
f ( );
export = f;
export = f;;
g( )

================================================================================
Expand Down
Loading