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

fix(javascript): preserve parens for type casting for sub-item #4648

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 31 additions & 10 deletions src/language-js/needs-parens.js
Expand Up @@ -5,19 +5,39 @@ const assert = require("assert");
const util = require("../common/util");
const comments = require("./comments");

function hasClosureCompilerTypeCastComment(text, node, locEnd) {
function hasClosureCompilerTypeCastComment(text, path, locStart, locEnd) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about dropping ClosureCompiler from this name since it supports TS too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, that syntax seems coming from closure compiler, see microsoft/TypeScript#5158.

This comment was marked as off-topic.

This comment was marked as off-topic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Salsa ≠ TypeScript (the language)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What’s Salsa then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// https://github.com/google/closure-compiler/wiki/Annotating-Types#type-casts
// Syntax example: var x = /** @type {string} */ (fruit);

const n = path.getValue();

return (
node.comments &&
node.comments.some(
comment =>
comment.leading &&
comments.isBlockComment(comment) &&
comment.value.match(/^\*\s*@type\s*{[^}]+}\s*$/) &&
util.getNextNonSpaceNonCommentCharacter(text, comment, locEnd) === "("
)
util.getNextNonSpaceNonCommentCharacter(text, n, locEnd) === ")" &&
(hasTypeCastComment(n) || hasAncestorTypeCastComment(0))
);

// for sub-item: /** @type {array} */ (numberOrString).map(x => x);
function hasAncestorTypeCastComment(index) {
const ancestor = path.getParentNode(index);
return ancestor &&
util.getNextNonSpaceNonCommentCharacter(text, ancestor, locEnd) !== ")" &&
/^[\s(]*$/.test(text.slice(locStart(ancestor), locStart(n)))
? hasTypeCastComment(ancestor) || hasAncestorTypeCastComment(index + 1)
: false;
}

function hasTypeCastComment(node) {
return (
node.comments &&
node.comments.some(
comment =>
comment.leading &&
comments.isBlockComment(comment) &&
comment.value.match(/^\*\s*@type\s*{[^}]+}\s*$/) &&
util.getNextNonSpaceNonCommentCharacter(text, comment, locEnd) === "("
)
);
}
}

function needsParens(path, options) {
Expand Down Expand Up @@ -46,7 +66,8 @@ function needsParens(path, options) {
if (
hasClosureCompilerTypeCastComment(
options.originalText,
node,
path,
options.locStart,
options.locEnd
)
) {
Expand Down
8 changes: 8 additions & 0 deletions tests/comments/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -151,6 +151,10 @@ functionCall(1 + /** @type {string} */ (value), /** @type {!Foo} */ ({}));
function returnValue() {
return /** @type {!Array.<string>} */ (['hello', 'you']);
}

var newArray = /** @type {array} */ (numberOrString).map(x => x);
var newArray = /** @type {array} */ ((numberOrString)).map(x => x);
var newArray = /** @type {array} */ ((numberOrString).map(x => x));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// test to make sure comments are attached correctly
let inlineComment = /* some comment */ someReallyLongFunctionCall(
Expand All @@ -171,6 +175,10 @@ function returnValue() {
return /** @type {!Array.<string>} */ (["hello", "you"]);
}

var newArray = /** @type {array} */ (numberOrString).map(x => x);
var newArray = /** @type {array} */ (numberOrString).map(x => x);
var newArray = /** @type {array} */ (numberOrString.map(x => x));

`;

exports[`dangling.js 1`] = `
Expand Down
4 changes: 4 additions & 0 deletions tests/comments/closure-compiler-type-cast.js
Expand Up @@ -14,3 +14,7 @@ functionCall(1 + /** @type {string} */ (value), /** @type {!Foo} */ ({}));
function returnValue() {
return /** @type {!Array.<string>} */ (['hello', 'you']);
}

var newArray = /** @type {array} */ (numberOrString).map(x => x);
var newArray = /** @type {array} */ ((numberOrString)).map(x => x);
var newArray = /** @type {array} */ ((numberOrString).map(x => x));