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 flow-remove-types for input with multi-byte characters #7781

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
55 changes: 33 additions & 22 deletions packages/flow-remove-types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@ module.exports = function flowRemoveTypes(source, options) {

var removedNodes = [];

var sourceBuffer = Buffer.from(source);

var context = {
ast: ast,
source: source,
sourceBuffer: sourceBuffer,
removedNodes: removedNodes,
pretty: Boolean(options && options.pretty),
bytesAdded: 0
};

// Remove the flow pragma.
Expand All @@ -85,10 +88,10 @@ module.exports = function flowRemoveTypes(source, options) {
// Remove all flow type definitions.
visit(ast, context, removeFlowVisitor);

return resultPrinter(options, source, removedNodes);
return resultPrinter(options, source, sourceBuffer, removedNodes, context.bytesAdded);
};

function resultPrinter(options, source, removedNodes) {
function resultPrinter(options, source, sourceBuffer, removedNodes, bytesAdded) {
// Options
var pretty = Boolean(options && options.pretty);

Expand All @@ -98,32 +101,34 @@ function resultPrinter(options, source, removedNodes) {
return source;
}

var result = '';
var buf = Buffer.alloc(sourceBuffer.length + bytesAdded);
var lastPos = 0;
var offset = 0;

// Step through the removed nodes, building up the resulting string.
for (var i = 0; i < removedNodes.length; i++) {
var node = removedNodes[i];
result += source.slice(lastPos, startOf(node));
offset += sourceBuffer.copy(buf, offset, lastPos, startOf(node));
lastPos = endOf(node);
if (typeof node.__spliceValue === 'string') {
result += node.__spliceValue;
offset += buf.write(node.__spliceValue, offset);
}
if (!pretty) {
var toReplace = source.slice(startOf(node), endOf(node));
if (!node.loc || node.loc.start.line === node.loc.end.line) {
result += space(toReplace.length);
offset += buf.write(space(endOf(node) - startOf(node)), offset);
} else {
var toReplaceLines = toReplace.split(LINE_RX);
result += space(toReplaceLines[0].length);
var toReplaceLines = sourceBuffer.toString('utf8', startOf(node), endOf(node)).split(LINE_RX);
offset += buf.write(space(toReplaceLines[0].length), offset);
for (var j = 1; j < toReplaceLines.length; j += 2) {
result += toReplaceLines[j] + space(toReplaceLines[j + 1].length);
offset += buf.write(toReplaceLines[j] + space(toReplaceLines[j + 1].length), offset);
}
}
}
}

return (result += source.slice(lastPos));
offset += sourceBuffer.copy(buf, offset, lastPos, sourceBuffer.length);

return buf.toString('utf8', 0, offset);
},
generateMap: function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

How does this issue translate to source maps? I'm suddenly worried that the source map spec is silent on what "columns" mean, but I'd imagine it's meant to be interpreted more like "code points" than "bytes". So we might need to do some Unicode bookkeeping in generateSourceMappings as well.

return {
Expand Down Expand Up @@ -324,17 +329,22 @@ function removeNode(context, node) {
return false;
}

var SPACE = ' '.charCodeAt(0);
var TAB = '\t'.charCodeAt(0);
var RETURN = '\r'.charCodeAt(0);
var NEWLINE = '\n'.charCodeAt(0);

function getPragmaNode(context, start, size) {
var source = context.source;
var source = context.sourceBuffer;
var line = 1;
var column = 0;
for (var position = 0; position < start; position++) {
var char = source[position];
if (char === '\n') {
if (char === NEWLINE) {
line++;
column = 0;
} else if (char === '\r') {
if (source[position + 1] === '\n') {
} else if (char === RETURN) {
if (source[position + 1] === NEWLINE) {
position++;
}
line++;
Expand All @@ -354,10 +364,10 @@ function getPragmaNode(context, start, size) {
}

function getLeadingSpaceNode(context, node) {
var source = context.source;
var source = context.sourceBuffer;
var end = startOf(node);
var start = end;
while (source[start - 1] === ' ' || source[start - 1] === '\t') {
while (source[start - 1] === SPACE || source[start - 1] === TAB) {
start--;
}
if (start !== end) {
Expand All @@ -370,16 +380,16 @@ function getLeadingSpaceNode(context, node) {
}

function getTrailingLineNode(context, node) {
var source = context.source;
var source = context.sourceBuffer;
var start = endOf(node);
var end = start;
while (source[end] === ' ' || source[end] === '\t') {
while (source[end] === SPACE || source[end] === TAB) {
end++;
}

// Remove all space including the line break if this token was alone on the line.
if (source[end] === '\n' || source[end] === '\r') {
if (source[end] === '\r' && source[end + 1] === '\n') {
if (source[end] === NEWLINE || source[end] === RETURN) {
if (source[end] === RETURN && source[end + 1] === NEWLINE) {
end++;
}
end++;
Expand All @@ -397,6 +407,7 @@ function getTrailingLineNode(context, node) {
// Creates a zero-width "node" with a value to splice at that position.
// WARNING: This is only safe to use when source maps are off!
function getSpliceNodeAtPos(context, pos, loc, value) {
context.bytesAdded += value.length;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is technically mixing JS string length (UTF-16 code units) and UTF-8 bytes. Not a problem yet because the only call to getSpliceNodeAtPos uses a plain ASCII string (' =>'), but maybe we should future-proof this?

return createNode({
start: pos,
end: pos,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/flow-remove-types/test/expected-pretty-inlinemap.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/flow-remove-types/test/expected-pretty.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* */
// @nolint

// multi-byte chars: Гарного дня, котики!

// Regular import
import {
Something,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading