Skip to content

Commit

Permalink
fix(estree): rename FieldDefinition -> PropertyDefinition, PrivateNam…
Browse files Browse the repository at this point in the history
…e -> PrivateIdentifier

closes #134
  • Loading branch information
3cp committed Dec 27, 2020
1 parent 84a2e2d commit 2a588e5
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 237 deletions.
2 changes: 1 addition & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ export function isStrictReservedWord(parser: ParserState, context: Context, t: T
* @param context Context masks
*/
export function isPropertyWithPrivateFieldKey(expr: any): boolean {
return !expr.property ? false : expr.property.type === 'PrivateName';
return !expr.property ? false : expr.property.type === 'PrivateIdentifier';
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const enum Errors {
Unexpected,
StrictOctalEscape,
TemplateOctalLiteral,
InvalidPrivateName,
InvalidPrivateIdentifier,
InvalidUnicodeEscapeSequence,
InvalidCodePoint,
InvalidHexEscapeSequence,
Expand Down Expand Up @@ -94,7 +94,7 @@ export const enum Errors {
InvalidForLHSBinding,
InvalidNewTarget,
InvalidEscapedKeyword,
MissingPrivateName,
MissingPrivateIdentifier,
DisallowedInContext,
AwaitOutsideAsync,
InvalidStrictLet,
Expand Down Expand Up @@ -175,7 +175,7 @@ export const errorMessages: {
[Errors.UnexpectedToken]: "Unexpected token: '%0'",
[Errors.StrictOctalEscape]: 'Octal escape sequences are not allowed in strict mode',
[Errors.TemplateOctalLiteral]: 'Octal escape sequences are not allowed in template strings',
[Errors.InvalidPrivateName]: 'Unexpected token `#`',
[Errors.InvalidPrivateIdentifier]: 'Unexpected token `#`',
[Errors.InvalidUnicodeEscapeSequence]: 'Illegal Unicode escape sequence',
[Errors.InvalidCodePoint]: 'Invalid code point %0',
[Errors.InvalidHexEscapeSequence]: 'Invalid hexadecimal escape sequence',
Expand Down Expand Up @@ -275,7 +275,7 @@ export const errorMessages: {
[Errors.InvalidForLHSBinding]: 'The left hand side of the for-header binding declaration is not destructible',
[Errors.InvalidNewTarget]: 'new.target only allowed within functions',
[Errors.InvalidEscapedKeyword]: "'Unexpected token: 'escaped keyword'",
[Errors.MissingPrivateName]: "'#' not followed by identifier",
[Errors.MissingPrivateIdentifier]: "'#' not followed by identifier",
[Errors.KeywordNotId]: 'Invalid keyword',
[Errors.InvalidLetClassName]: "Can not use 'let' as a class name",
[Errors.InvalidLetConstBinding]: "'A lexical declaration can't define a 'let' binding",
Expand Down
27 changes: 14 additions & 13 deletions src/estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export type Node =
| ExportNamedDeclaration
| ExportSpecifier
| ExpressionStatement
| FieldDefinition
| PropertyDefinition
| ForInStatement
| ForOfStatement
| ForStatement
Expand Down Expand Up @@ -109,7 +109,7 @@ export type Node =
| ObjectExpression
| ObjectPattern
| ParenthesizedExpression
| PrivateName
| PrivateIdentifier
| Program
| Property
| RegExpLiteral
Expand Down Expand Up @@ -223,7 +223,7 @@ export type PrimaryExpressionExtended =
| MetaProperty
| ObjectExpression
| ObjectPattern
| PrivateName
| PrivateIdentifier
| NewExpression
| Super
| TemplateLiteral
Expand Down Expand Up @@ -266,7 +266,7 @@ interface FunctionDeclarationBase extends _Node {
}

interface MethodDefinitionBase extends _Node {
key: Expression | PrivateName | null;
key: Expression | PrivateIdentifier | null;
value: FunctionExpression;
computed: boolean;
static: boolean;
Expand Down Expand Up @@ -356,21 +356,23 @@ export interface CatchClause extends _Node {

export interface ClassBody extends _Node {
type: 'ClassBody';
body: (ClassElement | FieldDefinition)[];
body: (ClassElement | PropertyDefinition)[];
}

export interface FieldDefinition extends _Node {
type: 'FieldDefinition';
key: PrivateName | Expression;
export interface PropertyDefinition extends _Node {
type: 'PropertyDefinition';
key: PrivateIdentifier | Expression;
value: any;
decorators?: Decorator[] | null;
computed: boolean;
static: boolean;
}
export interface PrivateName extends _Node {
type: 'PrivateName';

export interface PrivateIdentifier extends _Node {
type: 'PrivateIdentifier';
name: string;
}

export interface ClassDeclaration extends ClassDeclarationBase {
type: 'ClassDeclaration';
}
Expand Down Expand Up @@ -473,7 +475,6 @@ export interface FunctionExpression extends FunctionDeclarationBase {
export interface Identifier extends _Node {
type: 'Identifier';
name: string;
optional?: boolean;
}

export interface IfStatement extends _Node {
Expand All @@ -489,7 +490,7 @@ export interface Import extends _Node {

export interface ImportDeclaration extends _Node {
type: 'ImportDeclaration';
source: Expression;
source: Literal;
specifiers: ImportClause[];
}

Expand Down Expand Up @@ -622,7 +623,7 @@ export interface LogicalExpression extends _Node {
export interface MemberExpression extends _Node {
type: 'MemberExpression';
object: Expression | Super;
property: Expression | PrivateName;
property: Expression | PrivateIdentifier;
computed?: boolean;
optional?: boolean;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lexer/identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ export function scanIdentifierSlowCase(
*
* @param parser Parser object
*/
export function scanPrivateName(parser: ParserState): Token {
if (!isIdentifierStart(advanceChar(parser))) report(parser, Errors.MissingPrivateName);
export function scanPrivateIdentifier(parser: ParserState): Token {
if (!isIdentifierStart(advanceChar(parser))) report(parser, Errors.MissingPrivateIdentifier);
return Token.PrivateField;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lexer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export {
scanIdentifier,
scanIdentifierSlowCase,
scanUnicodeIdentifier,
scanPrivateName,
scanPrivateIdentifier,
scanUnicodeEscape
} from './identifier';
export { scanString } from './string';
Expand Down
4 changes: 2 additions & 2 deletions src/lexer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
scanIdentifier,
scanUnicodeIdentifier,
scanIdentifierSlowCase,
scanPrivateName,
scanPrivateIdentifier,
fromCodePoint,
consumeLineFeed,
scanNewLine,
Expand Down Expand Up @@ -260,7 +260,7 @@ export function scanSingleToken(parser: ParserState, context: Context, state: Le

// `#` (private name)
case Token.PrivateField:
return scanPrivateName(parser);
return scanPrivateIdentifier(parser);

case Token.WhiteSpace:
advanceChar(parser);
Expand Down
48 changes: 24 additions & 24 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4058,7 +4058,7 @@ export function parsePropertyOrPrivatePropertyName(parser: ParserState, context:
}

return context & Context.OptionsNext && parser.token === Token.PrivateField
? parsePrivateName(parser, context, parser.tokenPos, parser.linePos, parser.colPos)
? parsePrivateIdentifier(parser, context, parser.tokenPos, parser.linePos, parser.colPos)
: parseIdentifier(parser, context, 0);
}

Expand Down Expand Up @@ -4255,7 +4255,7 @@ export function parsePrimaryExpression(
case Token.BigIntLiteral:
return parseBigIntLiteral(parser, context, start, line, column);
case Token.PrivateField:
return parsePrivateName(parser, context, start, line, column);
return parsePrivateIdentifier(parser, context, start, line, column);
case Token.ImportKeyword:
return parseImportCallOrMetaExpression(parser, context, inNew, inGroup, start, line, column);
case Token.LessThan:
Expand Down Expand Up @@ -8115,8 +8115,8 @@ export function parseClassBody(
* MethodDefinition
* DecoratorList
* DecoratorList static MethodDefinition
* DecoratorList FieldDefinition
* DecoratorList static FieldDefinition
* DecoratorList PropertyDefinition
* DecoratorList static PropertyDefinition
*
* MethodDefinition :
* ClassElementName ( FormalParameterList ) { FunctionBody }
Expand All @@ -8125,9 +8125,9 @@ export function parseClassBody(
*
* ClassElementName :
* PropertyName
* PrivateName
* PrivateIdentifier
*
* PrivateName ::
* PrivateIdentifier ::
* # IdentifierName
*
* IdentifierName ::
Expand Down Expand Up @@ -8167,7 +8167,7 @@ export function parseClassBody(
context = (context | Context.DisallowIn) ^ Context.DisallowIn;
parser.flags = (parser.flags | Flags.HasConstructor) ^ Flags.HasConstructor;

const body: (ESTree.MethodDefinition | ESTree.FieldDefinition)[] = [];
const body: (ESTree.MethodDefinition | ESTree.PropertyDefinition)[] = [];
let decorators: ESTree.Decorator[];

while (parser.token !== Token.RightBrace) {
Expand Down Expand Up @@ -8235,9 +8235,9 @@ function parseClassElementList(
start: number,
line: number,
column: number
): ESTree.MethodDefinition | ESTree.FieldDefinition {
): ESTree.MethodDefinition | ESTree.PropertyDefinition {
let kind: PropertyKind = isStatic ? PropertyKind.Static : PropertyKind.None;
let key: ESTree.Expression | ESTree.PrivateName | null = null;
let key: ESTree.Expression | ESTree.PrivateIdentifier | null = null;

const { token, tokenPos, linePos, colPos } = parser;

Expand Down Expand Up @@ -8266,7 +8266,7 @@ function parseClassElementList(
case Token.AsyncKeyword:
if (parser.token !== Token.LeftParen && (parser.flags & Flags.NewLine) < 1) {
if (context & Context.OptionsNext && (parser.token & Token.IsClassField) === Token.IsClassField) {
return parseFieldDefinition(parser, context, key, kind, decorators, tokenPos, linePos, colPos);
return parsePropertyDefinition(parser, context, key, kind, decorators, tokenPos, linePos, colPos);
}

kind |= PropertyKind.Async | (optionalBit(parser, context, Token.Multiply) ? PropertyKind.Generator : 0);
Expand All @@ -8276,7 +8276,7 @@ function parseClassElementList(
case Token.GetKeyword:
if (parser.token !== Token.LeftParen) {
if (context & Context.OptionsNext && (parser.token & Token.IsClassField) === Token.IsClassField) {
return parseFieldDefinition(parser, context, key, kind, decorators, tokenPos, linePos, colPos);
return parsePropertyDefinition(parser, context, key, kind, decorators, tokenPos, linePos, colPos);
}
kind |= PropertyKind.Getter;
}
Expand All @@ -8285,7 +8285,7 @@ function parseClassElementList(
case Token.SetKeyword:
if (parser.token !== Token.LeftParen) {
if (context & Context.OptionsNext && (parser.token & Token.IsClassField) === Token.IsClassField) {
return parseFieldDefinition(parser, context, key, kind, decorators, tokenPos, linePos, colPos);
return parsePropertyDefinition(parser, context, key, kind, decorators, tokenPos, linePos, colPos);
}
kind |= PropertyKind.Setter;
}
Expand All @@ -8303,7 +8303,7 @@ function parseClassElementList(
nextToken(parser, context); // skip: '*'
} else if (context & Context.OptionsNext && parser.token === Token.PrivateField) {
kind |= PropertyKind.PrivateField;
key = parsePrivateName(parser, context, tokenPos, linePos, colPos);
key = parsePrivateIdentifier(parser, context, tokenPos, linePos, colPos);
context = context | Context.InClass;
} else if (context & Context.OptionsNext && (parser.token & Token.IsClassField) === Token.IsClassField) {
kind |= PropertyKind.ClassField;
Expand All @@ -8328,7 +8328,7 @@ function parseClassElementList(
key = parseIdentifier(parser, context, 0);
} else if (context & Context.OptionsNext && parser.token === Token.PrivateField) {
kind |= PropertyKind.PrivateField;
key = parsePrivateName(parser, context, tokenPos, linePos, colPos);
key = parsePrivateIdentifier(parser, context, tokenPos, linePos, colPos);
} else report(parser, Errors.InvalidKeyToken);
}

Expand Down Expand Up @@ -8356,7 +8356,7 @@ function parseClassElementList(
}

if (context & Context.OptionsNext && parser.token !== Token.LeftParen) {
return parseFieldDefinition(parser, context, key, kind, decorators, tokenPos, linePos, colPos);
return parsePropertyDefinition(parser, context, key, kind, decorators, tokenPos, linePos, colPos);
}

const value = parseMethodDefinition(parser, context, kind, inGroup, parser.tokenPos, parser.linePos, parser.colPos);
Expand Down Expand Up @@ -8408,22 +8408,22 @@ function parseClassElementList(
* @param parser Parser object
* @param context Context masks
*/
function parsePrivateName(
function parsePrivateIdentifier(
parser: ParserState,
context: Context,
start: number,
line: number,
column: number
): ESTree.PrivateName {
// PrivateName::
): ESTree.PrivateIdentifier {
// PrivateIdentifier::
// #IdentifierName
nextToken(parser, context); // skip: '#'
const { tokenValue } = parser;
if (tokenValue === 'constructor') report(parser, Errors.InvalidStaticClassFieldConstructor);
nextToken(parser, context);

return finishNode(parser, context, start, line, column, {
type: 'PrivateName',
type: 'PrivateIdentifier',
name: tokenValue
});
}
Expand All @@ -8438,20 +8438,20 @@ function parsePrivateName(
* @param decorators
*/

export function parseFieldDefinition(
export function parsePropertyDefinition(
parser: ParserState,
context: Context,
key: ESTree.PrivateName | ESTree.Expression | null,
key: ESTree.PrivateIdentifier | ESTree.Expression | null,
state: PropertyKind,
decorators: ESTree.Decorator[] | null,
start: number,
line: number,
column: number
): ESTree.FieldDefinition {
): ESTree.PropertyDefinition {
// ClassElement :
// MethodDefinition
// static MethodDefinition
// FieldDefinition ;
// PropertyDefinition ;
// ;
let value: ESTree.Expression | null = null;

Expand Down Expand Up @@ -8498,7 +8498,7 @@ export function parseFieldDefinition(
}

return finishNode(parser, context, start, line, column, {
type: 'FieldDefinition',
type: 'PropertyDefinition',
key,
value,
static: (state & PropertyKind.Static) > 0,
Expand Down
2 changes: 1 addition & 1 deletion src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export const enum Token {
AnyIdentifier = 120 | IsIdentifier,

// Stage #3 proposals
PrivateName = 121,
PrivateIdentifier = 121,
BigIntLiteral = 122 | IsExpressionStart | IsStringOrNumber,
Coalesce = 123 | IsBinaryOp | IsCoalesc | 1 << PrecStart, // ??
QuestionMarkPeriod = 124 | IsMemberOrCallExpression, // ?.
Expand Down
2 changes: 1 addition & 1 deletion test/parser/miscellaneous/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4193,7 +4193,7 @@ describe('Miscellaneous - JSX', () => {
type: 'ClassBody',
body: [
{
type: 'FieldDefinition',
type: 'PropertyDefinition',
decorators: [],
key: {
type: 'Identifier',
Expand Down

0 comments on commit 2a588e5

Please sign in to comment.