Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Dec 6, 2022
1 parent 65c8496 commit ed42bf1
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 146 deletions.
58 changes: 11 additions & 47 deletions packages/babel-parser/src/parser/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,20 @@ import type { Undone } from "./node";

/**
* A whitespace token containing comments
* @typedef CommentWhitespace
* @type {object}
* @property {number} start - the start of the whitespace token.
* @property {number} end - the end of the whitespace token.
* @property {Array<Comment>} comments - the containing comments
* @property {Node | null} leadingNode - the immediately preceding AST node of the whitespace token
* @property {Node | null} trailingNode - the immediately following AST node of the whitespace token
* @property {Node | null} containingNode - the innermost AST node containing the whitespace
* with minimal size (|end - start|)
*/
export type CommentWhitespace = {
start: number;
end: number;
comments: Array<Comment>;
leadingNode: Node | null;
trailingNode: Node | null;
containingNode: Node | null;
start: number; // the start of the whitespace token.
end: number; // the end of the whitespace token.
comments: Array<Comment>; // the containing comments
leadingNode: Node | null; // the immediately preceding AST node of the whitespace token
trailingNode: Node | null; // the immediately following AST node of the whitespace token
containingNode: Node | null; // the innermost AST node containing the whitespace with minimal size (|end - start|)
};

/**
* Merge comments with node's trailingComments or assign comments to be
* trailingComments. New comments will be placed before old comments
* because the commentStack is enumerated reversely.
*
* @param {Undone<Node>} node
* @param {Array<Comment>} comments
*/
function setTrailingComments(node: Undone<Node>, comments: Array<Comment>) {
if (node.trailingComments === undefined) {
Expand All @@ -46,9 +34,6 @@ function setTrailingComments(node: Undone<Node>, comments: Array<Comment>) {
* Merge comments with node's leadingComments or assign comments to be
* leadingComments. New comments will be placed before old comments
* because the commentStack is enumerated reversely.
*
* @param {Undone<Node>} node
* @param {Array<Comment>} comments
*/
function setLeadingComments(node: Undone<Node>, comments: Array<Comment>) {
if (node.leadingComments === undefined) {
Expand All @@ -62,9 +47,6 @@ function setLeadingComments(node: Undone<Node>, comments: Array<Comment>) {
* Merge comments with node's innerComments or assign comments to be
* innerComments. New comments will be placed before old comments
* because the commentStack is enumerated reversely.
*
* @param {Undone<Node>} node
* @param {Array<Comment>} comments
*/
export function setInnerComments(
node: Undone<Node>,
Expand All @@ -81,10 +63,6 @@ export function setInnerComments(
* Given node and elements array, if elements has non-null element,
* merge comments to its trailingComments, otherwise merge comments
* to node's innerComments
*
* @param {Undone<Node>} node
* @param {Array<Node>} elements
* @param {Array<Comment>} comments
*/
function adjustInnerComments(
node: Undone<Node>,
Expand All @@ -103,7 +81,6 @@ function adjustInnerComments(
}
}

/** @class CommentsParser */
export default class CommentsParser extends BaseParser {
addComment(comment: Comment): void {
if (this.filename) comment.loc.filename = this.filename;
Expand All @@ -113,10 +90,6 @@ export default class CommentsParser extends BaseParser {
/**
* Given a newly created AST node _n_, attach _n_ to a comment whitespace _w_ if applicable
* {@see {@link CommentWhitespace}}
*
* @param {Node} node
* @returns {void}
* @memberof CommentsParser
*/
processComment(node: Node): void {
const { commentStack } = this.state;
Expand Down Expand Up @@ -158,8 +131,6 @@ export default class CommentsParser extends BaseParser {
/**
* Assign the comments of comment whitespaces to related AST nodes.
* Also adjust innerComments following trailing comma.
*
* @memberof CommentsParser
*/
finalizeComment(commentWS: CommentWhitespace) {
const { comments } = commentWS;
Expand Down Expand Up @@ -219,8 +190,6 @@ export default class CommentsParser extends BaseParser {
* to each comment whitespace. Used only in parseExpression
* where the top level AST node is _not_ Program
* {@see {@link CommentsParser#finalizeComment}}
*
* @memberof CommentsParser
*/
finalizeRemainingComments() {
const { commentStack } = this.state;
Expand All @@ -237,16 +206,15 @@ export default class CommentsParser extends BaseParser {
* method later. In this case the identifier is not part of the AST and we
* should sync the knowledge to commentStacks
*
* For example, when parsing */
// async /* 1 */ function f() {}
/*
* ↓↓↓ We had escaped "\/" to "\\/" for comments
* For example, when parsing *\/
* // async /* 1 *\/ function f() {}
* /*
* the comment whitespace "* 1 *" has leading node Identifier(async). When
* we see the function token, we create a Function node and mark "* 1 *" as
* inner comments. So "* 1 *" should be detached from the Identifier node.
*
* @param {N.Node} node the last finished AST node _before_ current token
* @returns
* @memberof CommentsParser
* @param node the last finished AST node _before_ current token
*/
resetPreviousNodeTrailingComments(node: Node) {
const { commentStack } = this.state;
Expand All @@ -264,10 +232,6 @@ export default class CommentsParser extends BaseParser {
*
* This is used to properly attach comments around parenthesized
* expressions as leading/trailing comments of the inner expression.
*
* @param {Node} node
* @param {number} start
* @param {number} end
*/
takeSurroundingComments(node: Node, start: number, end: number) {
const { commentStack } = this.state;
Expand Down
53 changes: 25 additions & 28 deletions packages/babel-parser/src/parser/lval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,18 @@ export default abstract class LValParser extends NodeUtils {
/**
* Convert existing expression atom to assignable pattern
* if possible. Also checks invalid destructuring targets:
- Parenthesized Destructuring patterns
- RestElement is not the last element
- Missing `=` in assignment pattern
NOTE: There is a corresponding "isAssignable" method.
When this one is updated, please check if also that one needs to be updated.
* @param {Node} node The expression atom
* @param {boolean} [isLHS=false] Whether we are parsing a LeftHandSideExpression.
* If isLHS is `true`, the following cases are allowed: `[(a)] = [0]`, `[(a.b)] = [0]`
* If isLHS is `false`, we are in an arrow function parameters list.
* @memberof LValParser
*
* - Parenthesized Destructuring patterns
* - RestElement is not the last element
* - Missing `=` in assignment pattern
*
* NOTE: There is a corresponding "isAssignable" method.
* When this one is updated, please check if also that one needs to be updated.
*
* @param node The expression atom
* @param isLHS Whether we are parsing a LeftHandSideExpression.
* If isLHS is `true`, the following cases are allowed: `[(a)] = [0]`, `[(a.b)] = [0]`
* If isLHS is `false`, we are in an arrow function parameters list.
*/
toAssignable(node: Node, isLHS: boolean = false): void {
let parenthesized = undefined;
Expand All @@ -108,7 +107,7 @@ export default abstract class LValParser extends NodeUtils {
// i.e. `([(a) = []] = []) => {}`
// see also `recordArrowParemeterBindingError` signature in packages/babel-parser/src/util/expression-scope.js
if (parenthesized.type === "Identifier") {
this.expressionScope.recordArrowParemeterBindingError(
this.expressionScope.recordArrowParameterBindingError(
Errors.InvalidParenthesizedAssignment,
{ at: node },
);
Expand Down Expand Up @@ -510,15 +509,14 @@ export default abstract class LValParser extends NodeUtils {
* The `string`-only return option is actually just a shorthand for:
* `[key: string, parenthesized: false]`.
*
* @param {NodeType} type A Node `type` string
* @param {boolean} isUnparenthesizedInAssign
* @param type A Node `type` string
* @param isUnparenthesizedInAssign
* Whether the node in question is unparenthesized and its parent
* is either an assignment pattern or an assignment expression.
* @param {BindingTypes} binding
* @param binding
* The binding operation that is being considered for this potential
* LVal.
* @returns { boolean | string | [string, boolean] }
* `true` or `false` if we can immediately determine whether the node
* @returns `true` or `false` if we can immediately determine whether the node
* type in question can be treated as an `LVal`.
* A `string` key to traverse if we must check this child.
* A `[string, boolean]` tuple if we need to check this child and
Expand Down Expand Up @@ -548,31 +546,30 @@ export default abstract class LValParser extends NodeUtils {
/**
* Verify that a target expression is an lval (something that can be assigned to).
*
* @param {Expression} expression The expression in question to check.
* @param {Object} options A set of options described below.
* @param {LValAncestor} options.in
* @param expression The expression in question to check.
* @param options A set of options described below.
* @param options.in
* The relevant ancestor to provide context information for the error
* if the check fails.
* @param {BindingTypes} [options.binding=BIND_NONE]
* @param options.binding
* The desired binding type. If the given expression is an identifier
* and `binding` is not `BIND_NONE`, `checkLVal` will register binding
* to the parser scope See also `src/util/scopeflags.js`
* @param {Set<string>|false} [options.checkClashes=false]
* @param options.checkClashes
* An optional string set to check if an identifier name is included.
* `checkLVal` will add checked identifier name to `checkClashes` It is
* used in tracking duplicates in function parameter lists. If it is
* false, `checkLVal` will skip duplicate checks
* @param {boolean} [options.allowingSloppyLetBinding]
* @param options.allowingSloppyLetBinding
* Whether an identifier named "let" should be allowed in sloppy mode.
* Defaults to `true` unless lexical scope its being used. This property
* is only relevant if the parser's state is in sloppy mode.
* @param {boolean} [options.strictModeChanged=false]
* @param options.strictModeChanged
* Whether an identifier has been parsed in a sloppy context but should
* be reinterpreted as strict-mode. e.g. `(arguments) => { "use strict "}`
* @param {boolean} [options.hasParenthesizedAncestor=false]
* @param options.hasParenthesizedAncestor
* This is only used internally during recursive calls, and you should
* not have to set it yourself.
* @memberof LValParser
*/

checkLVal(
Expand Down
22 changes: 4 additions & 18 deletions packages/babel-parser/src/parser/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ const keywordRelationalOperator = /in(?:stanceof)?/y;
* tt.templateNonTail => tt.backquote/tt.braceR + tt.template + tt.dollarBraceL
* For performance reasons this routine mutates `tokens`, it is okay
* here since we execute `parseTopLevel` once for every file.
* @param {*} tokens
* @returns
*/
function babel7CompatTokens(tokens: (Token | N.Comment)[], input: string) {
for (let i = 0; i < tokens.length; i++) {
Expand Down Expand Up @@ -237,14 +235,8 @@ export default abstract class StatementParser extends ExpressionParser {
return finishedProgram;
}

// TODO

/**
* cast a Statement to a Directive. This method mutates input statement.
*
* @param {N.Statement} stmt
* @returns {N.Directive}
* @memberof StatementParser
*/
stmtToDirective(stmt: N.Statement): N.Directive {
const directive = stmt as any;
Expand Down Expand Up @@ -286,12 +278,10 @@ export default abstract class StatementParser extends ExpressionParser {

/**
* Assuming we have seen a contextual `let`, check if it starts a variable declaration
so that `left` should be interpreted as a `let` keyword.
* so that `left` should be interpreted as a `let` keyword.
*
* @param {?string} context When `context` is non nullish, it will return early and _skip_ checking
if the next token after `let` is `{` or a keyword relational operator
* @returns {boolean}
* @memberof StatementParser
* @param context When `context` is non nullish, it will return early and _skip_ checking
if the next token after `let` is `{` or a keyword relational operator
*/
hasFollowingIdentifier(context?: string | null): boolean {
const next = this.nextTokenStart();
Expand Down Expand Up @@ -2866,9 +2856,7 @@ export default abstract class StatementParser extends ExpressionParser {
/**
* parse assert entries
*
* @see {@link https://tc39.es/proposal-import-assertions/#prod-AssertEntries |AssertEntries}
* @returns {N.ImportAttribute[]}
* @memberof StatementParser
* @see {@link https://tc39.es/proposal-import-assertions/#prod-AssertEntries AssertEntries}
*/
parseAssertEntries(): N.ImportAttribute[] {
const attrs = [];
Expand Down Expand Up @@ -2915,8 +2903,6 @@ export default abstract class StatementParser extends ExpressionParser {
/**
* parse module attributes
* @deprecated It will be removed in Babel 8
* @returns
* @memberof StatementParser
*/
maybeParseModuleAttributes() {
if (this.match(tt._with) && !this.hasPrecedingLineBreak()) {
Expand Down
12 changes: 4 additions & 8 deletions packages/babel-parser/src/parser/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export default abstract class UtilParser extends Tokenizer {
// Forward-declaration: defined in parser/index.js
abstract getScopeHandler(): { new (...args: any): ScopeHandler };

// TODO

addExtra(
node: Partial<Node>,
key: string,
Expand Down Expand Up @@ -128,8 +126,6 @@ export default abstract class UtilParser extends Tokenizer {
return skipWhiteSpaceToLineBreak.test(this.input);
}

// TODO

isLineTerminator(): boolean {
return this.eat(tt.semi) || this.canInsertSemicolon();
}
Expand Down Expand Up @@ -264,15 +260,15 @@ export default abstract class UtilParser extends Tokenizer {
return tokenIsLiteralPropertyName(this.state.type);
}

/*
/**
* Test if given node is a PrivateName
* will be overridden in ESTree plugin
*/
isPrivateName(node: Node): boolean {
return node.type === "PrivateName";
}

/*
/**
* Return the string value of a given private name
* WITHOUT `#`
* @see {@link https://tc39.es/ecma262/#sec-static-semantics-stringvalue}
Expand All @@ -281,7 +277,7 @@ export default abstract class UtilParser extends Tokenizer {
return node.id.name;
}

/*
/**
* Return whether the given node is a member/optional chain that
* contains a private name as its property
* It is overridden in ESTree plugin
Expand Down Expand Up @@ -380,7 +376,7 @@ export default abstract class UtilParser extends Tokenizer {
* - **shorthandAssignLoc**: track initializer `=` position
* - **doubleProtoLoc**: track the duplicate `__proto__` key position
* - **privateKey**: track private key `#p` position
* - **optionalParametersLoc**: track the optional paramter (`?`).
* - **optionalParametersLoc**: track the optional parameter (`?`).
* It's only used by typescript and flow plugins
*/
export class ExpressionErrors {
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-parser/src/plugins/placeholders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { ExpressionErrors } from "../parser/util";
import type { BindingTypes } from "../util/scopeflags";
import type { Position } from "../util/location";

type PossiblePlaceholedrs = {
type PossiblePlaceholders = {
Identifier: N.Identifier;
StringLiteral: N.StringLiteral;
Expression: N.Expression;
Expand All @@ -19,9 +19,9 @@ type PossiblePlaceholedrs = {
ClassBody: N.ClassBody;
Pattern: N.Pattern;
};
export type PlaceholderTypes = keyof PossiblePlaceholedrs;
export type PlaceholderTypes = keyof PossiblePlaceholders;

type NodeOf<T extends keyof PossiblePlaceholedrs> = PossiblePlaceholedrs[T];
type NodeOf<T extends keyof PossiblePlaceholders> = PossiblePlaceholders[T];
// todo: when there is proper union type for Node
// type NodeOf<T extends PlaceholderTypes> = Extract<N.Node, { type: T }>;

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/plugins/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3544,7 +3544,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
case "TSNonNullExpression":
case "TSTypeAssertion":
if (isLHS) {
this.expressionScope.recordArrowParemeterBindingError(
this.expressionScope.recordArrowParameterBindingError(
TSErrors.UnexpectedTypeCastInParameter,
{ at: node },
);
Expand Down
Loading

0 comments on commit ed42bf1

Please sign in to comment.