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

[JavaScript] Use meta.mapping instead of meta.object-literal #2020

Merged
merged 4 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 19 additions & 15 deletions JavaScript/JavaScript.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,7 @@ contexts:
set: object-literal-contents

object-literal-contents:
- meta_scope: meta.object-literal.js
- meta_scope: meta.mapping.js

- match: '\}'
scope: punctuation.section.block.end.js
Expand All @@ -1516,17 +1516,27 @@ contexts:

- match: '{{identifier}}(?=\s*(?:[},]|$|//|/\*))'
scope: variable.other.readwrite.js

- match: (?=\[)
push: computed-property-name
push:
- object-literal-meta-key
- computed-property-name

- match: (?=\"|')
push:
- object-literal-meta-key
- literal-string
- include: bare-property-name

- match: (?=[-+]?(?:\.[0-9]|0[bxo]|\d))
push:
- meta_scope: meta.object-literal.key.js
- include: literal-number
- object-literal-meta-key
- literal-number

# - include: bare-property-name
- match: (?={{identifier}})
push:
- object-literal-meta-key
- bare-property-name

- include: comma-separator
- match: ':'
Expand All @@ -1540,14 +1550,8 @@ contexts:
push: expression-no-comma

bare-property-name:
- match: '{{dollar_only_identifier}}'
scope: meta.object-literal.key.dollar.only.js punctuation.dollar.js
- match: '{{dollar_identifier}}'
scope: meta.object-literal.key.dollar.js
captures:
1: punctuation.dollar.js
- match: '{{identifier}}'
scope: meta.object-literal.key.js
pop: true

computed-property-name:
- match: \[
Expand All @@ -1560,7 +1564,7 @@ contexts:
push: expression

object-literal-meta-key:
- meta_scope: meta.object-literal.key.js
- meta_scope: meta.mapping.key.js
- include: else-pop

object-literal-expect-colon:
Expand All @@ -1570,7 +1574,7 @@ contexts:

method-name:
- match: '{{dollar_identifier}}'
scope: meta.object-literal.key.dollar.js entity.name.function.js
scope: meta.mapping.key.dollar.js entity.name.function.js
captures:
1: punctuation.dollar.js
pop: true
Expand Down Expand Up @@ -1611,7 +1615,7 @@ contexts:

field-name:
- match: '{{dollar_identifier}}'
scope: meta.object-literal.key.dollar.js variable.other.readwrite.js
scope: meta.mapping.key.dollar.js variable.other.readwrite.js
captures:
1: punctuation.dollar.js
pop: true
Expand Down
93 changes: 44 additions & 49 deletions JavaScript/tests/syntax_test_js.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ let x = import.meta;
// method body, but we include it here to ensure that highlighting is not
// broken as the user is typing
let a = { otherIdentifier, foo(), baz: 1 }
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.object-literal
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.mapping
Copy link
Collaborator

Choose a reason for hiding this comment

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

This wouldn't also satisfy meta.mapping - meta.mapping meta.mapping, would it? Because I'm not seeing a clear_scopes in this diff, especially not for object-literal-meta-key.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point. One issue is that there's an extra meta scope around the key and value when the value is a function:

+{
    funcKey: function() {
//  ^^^^^^^^^^^^^^^^^^^ meta.mapping meta.function.declaration
//  ^^^^^^^ meta.mapping meta.function.declaration meta.mapping.key entity.name.function
    },
}

Wrapping the whole thing like that in meta.function is admittedly dubious, but it's used for the symbol list. Do you have any ideas on how to address this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@FichteFoll Any thoughts on this?

Copy link
Member

Choose a reason for hiding this comment

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

I think this is reasonable for now. The pattern of naming functions via object key is so pervasive that we definitely wouldn't want to lose that functionality.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, you could pop two scopes for the mapping key and re-push meta.mapping.key meta.function entity.name (and do the same for the value), but unfortunately scope mangling like this can get extremly annoying when a set is involved.

There is a solution to this, but having meta.mapping is already an improvement over meta.object-literal.

// ^^^^^^^^^^^^^^^ variable.other.readwrite
// ^^^ entity.name.function
// ^^^ meta.object-literal.key
// ^^^ meta.mapping.key

someFunction({
prop1, prop2, prop3
Expand Down Expand Up @@ -445,27 +445,22 @@ a = test ? a + b : c;
// ^ meta.block meta.block variable.other.readwrite

var obj = {
// ^ meta.object-literal punctuation.section.block.begin - meta.block
// ^ meta.mapping punctuation.section.block.begin - meta.block
key: bar,
// <- meta.object-literal.key
// <- meta.mapping.key
$key2: "string value",
// ^ meta.object-literal.key
// ^ meta.mapping.key
// ^ - constant.other
// ^^^^^^^^^^^^^^ meta.string string.quoted.double
$key3: 0,
// <- meta.object-literal.key.dollar punctuation.dollar
// <- meta.object-literal.key.dollar - punctuation.dollar

$: 0,
// ^ meta.object-literal.key.dollar.only punctuation.dollar

$keyFunc: function() {
// ^^^^^^^^^^^^^^^^^^^^ meta.function.declaration
// <- meta.object-literal.key.dollar entity.name.function punctuation.dollar
// <- meta.object-literal.key.dollar entity.name.function - punctuation.dollar
// <- meta.mapping.key.dollar entity.name.function punctuation.dollar
// <- meta.mapping.key.dollar entity.name.function - punctuation.dollar
},

[true==false ? 'one' : 'two']: false,
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.mapping.key
// ^ punctuation.section.brackets.begin
// ^^^^ constant.language
// ^^^^ constant.language
Expand All @@ -475,13 +470,13 @@ var obj = {
// ^ punctuation.separator.key-value

"": true,
// <- meta.object-literal.key
// <- meta.mapping.key

"key4": true,
// ^^^^^^ meta.object-literal.key meta.string string.quoted.double
// ^^^^^^ meta.mapping.key meta.string string.quoted.double
// ^ punctuation.separator.key-value - string
'key5': false,
// ^^^^^^meta.object-literal.key meta.string string.quoted.single
// ^^^^^^meta.mapping.key meta.string string.quoted.single
// ^ punctuation.separator.key-value - string
// ^^^^^ constant.language.boolean.false

Expand All @@ -502,40 +497,40 @@ var obj = {

funcKey: function() {
// ^^^^^^^^^^^^^^^^^^^ meta.function.declaration
// ^^^^^^^ meta.object-literal.key entity.name.function
// ^^^^^^^ meta.mapping.key entity.name.function
},

func2Key: function func2Key() {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.declaration
// ^^^^^^^^ meta.object-literal.key entity.name.function
// ^^^^^^^^ meta.mapping.key entity.name.function
},

funcKeyArrow: () => {
// ^^^^^^^^^^^^^^^^^^^ meta.function.declaration
// ^^^^^^^^^^^^ meta.object-literal.key entity.name.function
// ^^^^^^^^^^^^ meta.mapping.key entity.name.function
},

"funcStringKey": function funcStringKey()
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.declaration
// ^^^^^^^^^^^^^^^ meta.object-literal.key meta.string string.quoted.double
// ^^^^^^^^^^^^^^^ meta.mapping.key meta.string string.quoted.double
// ^^^^^^^^^^^^^ entity.name.function
{ },

'funcStringKey': function() {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.declaration
// ^^^^^^^^^^^^^^^ meta.object-literal.key meta.string string.quoted.single
// ^^^^^^^^^^^^^^^ meta.mapping.key meta.string string.quoted.single
// ^^^^^^^^^^^^^ entity.name.function
},

'funcStringKeyArrow': () => {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.declaration
// ^^^^^^^^^^^^^^^^^^^^ meta.object-literal.key meta.string string.quoted.single
// ^^^^^^^^^^^^^^^^^^^^ meta.mapping.key meta.string string.quoted.single
// ^^^^^^^^^^^^^^^^^^ entity.name.function
},

"func\\String2KeyArrow": (foo) => {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.declaration
// ^^^^^^^^^^^^^^^^^^^^^^^ meta.object-literal.key meta.string string.quoted.double
// ^^^^^^^^^^^^^^^^^^^^^^^ meta.mapping.key meta.string string.quoted.double
// ^^^^^^^^^^^^^^^^^^^^^ entity.name.function
// ^^ constant.character.escape
},
Expand All @@ -558,13 +553,13 @@ var obj = {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ entity.name.function

"key (": true,
// ^^^^^^^ meta.object-literal.key
// ^^^^^^^ meta.mapping.key

"key \"(": true,
// ^^^^^^^^^ meta.object-literal.key
// ^^^^^^^^^ meta.mapping.key

"key '(": true,
// ^^^^^^^^ meta.object-literal.key
// ^^^^^^^^ meta.mapping.key

static,
// ^^^^^^ variable.other.readwrite
Expand Down Expand Up @@ -596,9 +591,9 @@ var obj = {
// ^^^ entity.name.function

get: 42,
// ^^^ meta.object-literal.key
// ^^^ meta.mapping.key
}
// <- meta.object-literal - meta.block
// <- meta.mapping - meta.block

+{
// <- keyword.operator
Expand All @@ -610,14 +605,14 @@ var obj = {
// ^ keyword.operator
// ^ constant.language
};
// <- meta.object-literal punctuation.section.block.end
// <- meta.mapping punctuation.section.block.end

({
// <- meta.object-literal
// <- meta.mapping
0.: {0.e+0: 0}
//^^ meta.object-literal.key constant.numeric
//^^ meta.mapping.key constant.numeric
// ^ punctuation.separator.key-value
// ^^^^^ meta.object-literal.key constant.numeric
// ^^^^^ meta.mapping.key constant.numeric
// ^ constant.numeric
});

Expand Down Expand Up @@ -1269,7 +1264,7 @@ const test = ({a, b, c=()=>({active:false}) }) => {};
()
=> { return; }
// ^^ storage.type.function.arrow
// ^^^^^^^^^^^ meta.block - meta.object-literal
// ^^^^^^^^^^^ meta.block - meta.mapping
// ^^^^^^ keyword.control.flow
);

Expand Down Expand Up @@ -1368,7 +1363,7 @@ var foo = ~{a:function(){}.a()}
// ^ keyword.operator.assignment
// ^ keyword.operator.bitwise
// ^ punctuation.section.block.begin
// ^^^^^^^^^^^^^^^^^^^^ meta.object-literal
// ^^^^^^^^^^^^^^^^^^^^ meta.mapping
// ^^^^^^^^^^^^ meta.function.declaration
// ^ entity.name.function
// ^ punctuation.separator.key-value
Expand All @@ -1377,7 +1372,7 @@ var foo = ~{a:function(){}.a()}
// ^ punctuation.section.group.end
// ^ meta.block punctuation.section.block.begin
// ^ meta.block punctuation.section.block.end
// ^ meta.object-literal
// ^ meta.mapping
// ^^^ meta.function.declaration
// ^ entity.name.function
// ^ punctuation.section.group.begin
Expand Down Expand Up @@ -1485,17 +1480,17 @@ void {
// ^ meta.brackets
'test3': "asdf"
}
// <- meta.object-literal punctuation.section.block.end
// <- meta.mapping punctuation.section.block.end

// This tests parsing semi-broken object literals, which should help while a
// user is in the middle of typing code
void {
key1: true
// ^^^^ meta.object-literal.key.js
// ^^^^ meta.mapping.key.js
key2: 0
// ^^^^ meta.object-literal.key.js
// ^^^^ meta.mapping.key.js
key3: function()
// ^^^^ meta.object-literal.key.js entity.name.function
// ^^^^ meta.mapping.key.js entity.name.function
{

}
Expand Down Expand Up @@ -1679,7 +1674,7 @@ new FooBar(function(){

var test =
{a: 1}
// <- meta.object-literal punctuation.section.block.begin
// <- meta.mapping punctuation.section.block.begin

var arrowFuncBraceNextLine = () => /* comments! */
// ^ entity.name.function
Expand Down Expand Up @@ -1710,14 +1705,14 @@ var o = { a: i => i * 2, b: i => i * 3 }

function test() {
return {a: 1};
// ^^^^^^ meta.object-literal
// ^ meta.object-literal.key
// ^^^^^^ meta.mapping
// ^ meta.mapping.key
// ^ constant.numeric
}

$.each({})
// <- variable.other.dollar.only punctuation.dollar
// ^ meta.object-literal
// ^ meta.mapping

$varname.method()
// <- variable.other.dollar punctuation.dollar - variable.object.dollar.only
Expand Down Expand Up @@ -1773,18 +1768,18 @@ var CONST;

return;
{a: 1};
// ^ meta.block - meta.object-literal
// ^ meta.block - meta.mapping

return/**/{a: 1}
// ^^^^^^ meta.object-literal - meta.block
// ^^^^^^ meta.mapping - meta.block

return
{a: 1};
// ^ meta.block - meta.object-literal
// ^ meta.block - meta.mapping

return/*
*/{a: 1}
//^^^^^^ meta.block - meta.object-literal
//^^^^^^ meta.block - meta.mapping

const abc = new Set
if (true) {};
Expand All @@ -1800,13 +1795,13 @@ var o = {

var query = {
type: type==undefined ? null : {$in: type.split(',')}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.object-literal
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.mapping
// ^^^^^^^^^ constant.language.undefined
// ^ keyword.operator.ternary
// ^^^^ constant.language.null
// ^ keyword.operator.ternary
// ^ punctuation.section.block.begin
// ^^ meta.object-literal.key.dollar.js
// ^^ meta.mapping.key.js
// ^ punctuation.separator.key-value.js
// ^ punctuation.section.block.end
};
Expand Down
Loading