Skip to content
Permalink
Browse files
fix: Use .range instead of .start & .end on node & token
  • Loading branch information
MichaelDeBoey committed May 11, 2020
1 parent 9adfc6f commit 93374cdd36c24b447180093383dd3e5222b80087
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 30 deletions.
@@ -10,7 +10,7 @@ const create = (context) => {

return {
BooleanTypeAnnotation (node) {
const diff = node.end - node.start;
const diff = node.range[1] - node.range[0];

if (longForm && diff === 4) {
context.report({
@@ -26,12 +26,12 @@ const create = (context) => {
const [opener, firstInnerToken] = sourceCode.getFirstTokens(types, 2);
const [lastInnerToken, closer] = sourceCode.getLastTokens(types, 2);

const spacesBefore = firstInnerToken.start - opener.end;
const spacesAfter = closer.start - lastInnerToken.end;
const spacesBefore = firstInnerToken.range[0] - opener.range[1];
const spacesAfter = closer.range[0] - lastInnerToken.range[1];

if (never) {
if (spacesBefore) {
if (sourceCode.text[opener.end] !== '\n') {
if (sourceCode.text[opener.range[1]] !== '\n') {
context.report({
data: {name: node.id.name},
fix: spacingFixers.stripSpacesAfter(opener, spacesBefore),
@@ -42,7 +42,7 @@ const create = (context) => {
}

if (spacesAfter) {
if (sourceCode.text[closer.start - 1] !== '\n') {
if (sourceCode.text[closer.range[0] - 1] !== '\n') {
context.report({
data: {name: node.id.name},
fix: spacingFixers.stripSpacesAfter(lastInnerToken, spacesAfter),
@@ -48,12 +48,12 @@ const create = (context) => {
if (never && nextLineIsEmpty) {
context.report({
fix: (fixer) => {
const lineBreak = sourceCode.text[potentialFlowFileAnnotation.end];
const lineBreak = sourceCode.text[potentialFlowFileAnnotation.range[1]];

return fixer.replaceTextRange(
[
potentialFlowFileAnnotation.end,
potentialFlowFileAnnotation.end + (
potentialFlowFileAnnotation.range[1],
potentialFlowFileAnnotation.range[1] + (
lineBreak === '\r' ? 2 : 1
),
],
@@ -71,15 +71,15 @@ const create = (context) => {
annotation = ['line', 'none'].includes(style) ? '// @flow\n' : '/* @flow */\n';
}

return fixer.replaceTextRange([node.start, node.start], annotation);
return fixer.replaceTextRange([node.range[0], node.range[0]], annotation);
};
};

const addStrictAnnotation = () => {
return (fixer) => {
const annotation = ['line', 'none'].includes(style) ? '// @flow strict\n' : '/* @flow strict */\n';

return fixer.replaceTextRange([node.start, node.range[0]], annotation);
return fixer.replaceTextRange([node.range[0], node.range[0]], annotation);
};
};

@@ -88,7 +88,7 @@ const create = (context) => {
});

if (potentialFlowFileAnnotation) {
if (firstToken && firstToken.start < potentialFlowFileAnnotation.start) {
if (firstToken && firstToken.range[0] < potentialFlowFileAnnotation.range[0]) {
context.report(potentialFlowFileAnnotation, 'Flow file annotation not at the top of the file.');
}
const annotationValue = potentialFlowFileAnnotation.value.trim();
@@ -99,7 +99,7 @@ const create = (context) => {
context.report({
fix: (fixer) => {
return fixer.replaceTextRange(
[potentialFlowFileAnnotation.start, potentialFlowFileAnnotation.end],
[potentialFlowFileAnnotation.range[0], potentialFlowFileAnnotation.range[1]],
annotation
);
},
@@ -73,8 +73,8 @@ const generateOrderedList = (context, sort, properties) => {

const commentsBefore = source.getCommentsBefore(property);
const startIndex = commentsBefore.length > 0 ?
commentsBefore[0].start :
property.start;
commentsBefore[0].range[0] :
property.range[0];

if (property.type === 'ObjectTypeSpreadProperty' || !property.value) {
// NOTE: It could but currently does not fix recursive generic type arguments in GenericTypeAnnotation within ObjectTypeSpreadProperty.
@@ -88,7 +88,7 @@ const generateOrderedList = (context, sort, properties) => {
const beforePunctuator = source.getTokenBefore(nextPunctuator, {
includeComments: true,
});
const text = source.getText().slice(startIndex, beforePunctuator.end);
const text = source.getText().slice(startIndex, beforePunctuator.range[1]);

return [property, text];
}
@@ -100,7 +100,7 @@ const generateOrderedList = (context, sort, properties) => {
});

// Preserve all code until the colon verbatim:
const key = source.getText().slice(startIndex, colonToken.start);
const key = source.getText().slice(startIndex, colonToken.range[0]);
let value;

if (property.value.type === 'ObjectTypeAnnotation') {
@@ -118,7 +118,7 @@ const generateOrderedList = (context, sort, properties) => {
const beforePunctuator = source.getTokenBefore(nextPunctuator, {
includeComments: true,
});
const text = source.getText().slice(colonToken.end, beforePunctuator.end);
const text = source.getText().slice(colonToken.range[1], beforePunctuator.range[1]);

value = text;
}
@@ -182,11 +182,11 @@ const generateFix = (node, context, sort) => {
});
const commentsBefore = source.getCommentsBefore(property);
const startIndex = commentsBefore.length > 0 ?
commentsBefore[0].start :
property.start;
commentsBefore[0].range[0] :
property.range[0];
const subString = source.getText().slice(
startIndex,
beforePunctuator.end
beforePunctuator.range[1]
);

nodeText = nodeText.replace(subString, '$' + index);
@@ -21,7 +21,7 @@ const create = (context) => {
return;
}

const spaceBefore = types.start - node.id.end;
const spaceBefore = types.range[0] - node.id.range[1];

if (never && spaceBefore) {
context.report({
@@ -24,7 +24,7 @@ const getColon = (context, objectTypeProperty) => {
// the above have identical ASTs (save for their ranges)
// case 1 doesn't have a type annotation colon and should be ignored
const isShortPropertyFunction = (objectTypeProperty) => {
return objectTypeProperty.value.type === 'FunctionTypeAnnotation' && objectTypeProperty.start === objectTypeProperty.value.start;
return objectTypeProperty.value.type === 'FunctionTypeAnnotation' && objectTypeProperty.range[0] === objectTypeProperty.value.range[0];
};

export default (context, report) => {
@@ -14,9 +14,9 @@ const getSpaces = (direction, colon, context) => {
const sourceCode = context.getSourceCode();

if (direction === 'before') {
return colon.start - sourceCode.getTokenBefore(colon).end;
return colon.range[0] - sourceCode.getTokenBefore(colon).range[1];
} else {
return sourceCode.getTokenAfter(colon).start - colon.end;
return sourceCode.getTokenAfter(colon).range[0] - colon.range[1];
}
};

@@ -22,8 +22,8 @@ const create = (context) => {
const endOfType = sourceCode.getTokenBefore(separator);
const nextType = sourceCode.getTokenAfter(separator);

const spaceBefore = separator.start - endOfType.end;
const spaceAfter = nextType.start - separator.end;
const spaceBefore = separator.range[0] - endOfType.range[1];
const spaceAfter = nextType.range[0] - separator.range[1];

const data = {type: node.type === 'UnionTypeAnnotation' ? 'union' : 'intersection'};

@@ -1,12 +1,12 @@
export const stripSpacesBefore = (node, spaces) => {
return (fixer) => {
return fixer.removeRange([node.start - spaces, node.start]);
return fixer.removeRange([node.range[0] - spaces, node.range[0]]);
};
};

export const stripSpacesAfter = (node, spaces) => {
return (fixer) => {
return fixer.removeRange([node.end, node.end + spaces]);
return fixer.removeRange([node.range[1], node.range[1] + spaces]);
};
};

@@ -24,13 +24,13 @@ export const addSpaceAfter = (node) => {

export const replaceWithSpaceBefore = (node, spaces) => {
return (fixer) => {
return fixer.replaceTextRange([node.start - spaces, node.start], ' ');
return fixer.replaceTextRange([node.range[0] - spaces, node.range[0]], ' ');
};
};

export const replaceWithSpaceAfter = (node, spaces) => {
return (fixer) => {
return fixer.replaceTextRange([node.end, node.end + spaces], ' ');
return fixer.replaceTextRange([node.range[1], node.range[1] + spaces], ' ');
};
};

0 comments on commit 93374cd

Please sign in to comment.