refactor skipLineComment
MODIFY
packages/@romejs/js-parser/test-fixtures/comments/interpreter-directive/interpreter-directive-import/input.test.md
packages/@romejs/js-parser/test-fixtures/comments/interpreter-directive/interpreter-directive-import/input.test.md
```javascript
Program {
+ comments: Array []
corrupt: false
diagnostics: Array []
directives: Array []
@@ -28,7 +29,7 @@ Program {
}
}
interpreter: InterpreterDirective {
- value: "usr/bin/env babel-node"
+ value: "/usr/bin/env babel-node"
loc: Object {
filename: "input.js"
end: Object {
@@ -43,31 +44,11 @@ Program {
}
}
}
- comments: Array [
- CommentLine {
- id: "0"
- value: "usr/bin/env babel-node"
- loc: Object {
- filename: "input.js"
- end: Object {
- column: 25
- index: 25
- line: 1
- }
- start: Object {
- column: 1
- index: 1
- line: 1
- }
- }
- }
- ]
body: Array [
ImportDeclaration {
defaultSpecifier: undefined
importKind: undefined
namespaceSpecifier: undefined
- leadingComments: Array ["0"]
loc: Object {
filename: "input.js"
end: Object {
@@ -99,7 +80,6 @@ Program {
}
namedSpecifiers: Array [
ImportSpecifier {
- leadingComments: undefined
loc: Object {
filename: "input.js"
end: Object {
@@ -115,7 +95,6 @@ Program {
}
imported: Identifier {
name: "spawn"
- leadingComments: undefined
loc: Object {
filename: "input.js"
identifierName: "spawn"
@@ -134,9 +113,6 @@ Program {
local: ImportSpecifierLocal {
name: BindingIdentifier {
name: "spawn"
- innerComments: undefined
- leadingComments: undefined
- trailingComments: undefined
loc: Object {
filename: "input.js"
identifierName: "spawn"
@@ -153,7 +129,6 @@ Program {
}
}
importKind: undefined
- leadingComments: undefined
loc: Object {
filename: "input.js"
end: Object {

MODIFY
packages/@romejs/js-parser/test-fixtures/comments/interpreter-directive/interpreter-directive-object/input.test.md
packages/@romejs/js-parser/test-fixtures/comments/interpreter-directive/interpreter-directive-object/input.test.md
```javascript
Program {
+ comments: Array []
corrupt: false
diagnostics: Array []
directives: Array []
@@ -28,7 +29,7 @@ Program {
}
}
interpreter: InterpreterDirective {
- value: "usr/bin/env babel-node"
+ value: "/usr/bin/env babel-node"
loc: Object {
filename: "input.js"
end: Object {
@@ -43,28 +44,8 @@ Program {
}
}
}
- comments: Array [
- CommentLine {
- id: "0"
- value: "usr/bin/env babel-node"
- loc: Object {
- filename: "input.js"
- end: Object {
- column: 25
- index: 25
- line: 1
- }
- start: Object {
- column: 1
- index: 1
- line: 1
- }
- }
- }
- ]
body: Array [
VariableDeclarationStatement {
- leadingComments: Array ["0"]
loc: Object {
filename: "input.js"
end: Object {
@@ -80,7 +61,6 @@ Program {
}
declaration: VariableDeclaration {
kind: "var"
- leadingComments: undefined
loc: Object {
filename: "input.js"
end: Object {
@@ -97,7 +77,6 @@ Program {
declarations: Array [
VariableDeclarator {
id: BindingObjectPattern {
- leadingComments: undefined
rest: undefined
loc: Object {
filename: "input.js"
@@ -117,7 +96,6 @@ Program {
key: StaticPropertyKey {
value: Identifier {
name: "spawn"
- leadingComments: undefined
loc: Object {
filename: "input.js"
identifierName: "spawn"
@@ -133,7 +111,6 @@ Program {
}
}
}
- leadingComments: undefined
loc: Object {
filename: "input.js"
end: Object {
@@ -150,7 +127,6 @@ Program {
}
value: BindingIdentifier {
name: "spawn"
- leadingComments: undefined
loc: Object {
filename: "input.js"
identifierName: "spawn"
@@ -166,7 +142,6 @@ Program {
}
}
}
- leadingComments: undefined
loc: Object {
filename: "input.js"
end: Object {
@@ -183,7 +158,6 @@ Program {
}
]
}
- leadingComments: undefined
loc: Object {
filename: "input.js"
end: Object {

MODIFY
packages/@romejs/js-parser/tokenizer/index.ts
packages/@romejs/js-parser/tokenizer/index.ts
@@ -379,7 +379,7 @@ function skipBlockComment(parser: JSParser): void {
);
}
-export function skipLineComment(parser: JSParser, startSkip: number): AnyComment {
+function parseLineComment(parser: JSParser, startSkip: number): { startPos: Position, endPos: Position, text: string } {
const startIndex = parser.state.index;
const startPos = parser.getPositionFromState();
parser.state.index = ob1Add(parser.state.index, startSkip);
@@ -395,17 +395,25 @@ export function skipLineComment(parser: JSParser, startSkip: number): AnyComment
ch = parser.input.charCodeAt(getIndex(parser));
}
}
+ const endPos = parser.getPositionFromState();
+ const text = parser.getRawInput(ob1Add(startIndex, startSkip), parser.state.index);
+ return {
+ startPos,
+ endPos,
+ text,
+ };
+}
+
+export function skipLineComment(parser: JSParser, startSkip: number): AnyComment {
+ const commentLine = parseLineComment(parser, startSkip);
return pushComment(
parser,
{
block: false,
- text: parser.getRawInput(
- ob1Add(startIndex, startSkip),
- parser.state.index,
- ),
- startPos,
- endPos: parser.getPositionFromState(),
+ text: commentLine.text,
+ startPos: commentLine.startPos,
+ endPos: commentLine.endPos,
},
);
}
@@ -414,26 +422,11 @@ export function skipInterpreterDirective(
parser: JSParser,
startSkip: number,
): InterpreterDirective {
- const startIndex = parser.state.index;
- const startPos = parser.getPositionFromState();
- parser.state.index = ob1Add(parser.state.index, startSkip);
- let ch = parser.input.charCodeAt(getIndex(parser));
- if (parser.state.index < parser.length) {
- while (
- ch !== charCodes.lineFeed &&
- ch !== charCodes.carriageReturn &&
- ch !== charCodes.lineSeparator &&
- ch !== charCodes.paragraphSeparator &&
- bumpIndex(parser) < parser.length
- ) {
- ch = parser.input.charCodeAt(getIndex(parser));
- }
- }
- const loc = parser.finishLocAt(startPos, parser.getPositionFromState());
-
+ const commentLine = parseLineComment(parser, startSkip);
+ const loc = parser.finishLocAt(commentLine.startPos, commentLine.endPos);
return {
type: "InterpreterDirective",
- value: parser.getRawInput(ob1Add(startIndex, startSkip), parser.state.index),
+ value: commentLine.text,
loc,
};
}

refactor skipLineComment