Skip to content

Commit

Permalink
[JavaScript] Clean up imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Thom1729 committed Sep 30, 2023
1 parent 4ac1898 commit 78249eb
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 54 deletions.
58 changes: 18 additions & 40 deletions JavaScript/JavaScript.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ contexts:
set:
- import-meta
- expect-semicolon
- import-assert
- import-export-assert
- import-string-or-items
- import-check-branch

Expand Down Expand Up @@ -290,45 +290,43 @@ contexts:
set:
- import-export-from
- import-list
- import-export-alias
- import-item
- import-export-item

import-list:
- match: ','
scope: punctuation.separator.comma.js
push:
- import-export-alias
- import-item
push: import-export-item
- include: else-pop

import-item:
import-export-item:
- match: '\{'
scope: punctuation.section.block.begin.js
set: import-brace
set: import-export-brace
- match: '{{non_reserved_identifier}}'
scope: variable.other.readwrite.js
pop: 1
- match: '\*'
scope: constant.other.js
pop: 1
scope: constant.other.wildcard.asterisk.js
set: import-export-alias
- include: else-pop

import-brace:
import-export-brace:
- meta_scope: meta.block.js
- include: comma-separator
- match: '\}'
scope: punctuation.section.block.end.js
pop: 1
- match: '{{identifier_name}}'
scope: variable.other.readwrite.js
push: import-export-alias
- match: (?={{identifier_start}}|'|")
push:
- import-export-alias
- import-export-name
- match: '\*'
scope: constant.other.js
scope: constant.other.wildcard.asterisk.js
push: import-export-alias
- include: else-pop

import-assert:
- match: assert{{identifier_break}}
import-export-assert:
- match: (?:assert|with){{identifier_break}}
scope: keyword.control.import-export.js
set:
- include: object-literal
Expand All @@ -350,7 +348,7 @@ contexts:
export-body:
- include: declaration

- match: 'default{{identifier_break}}'
- match: default{{identifier_break}}
scope: keyword.control.import-export.js
set:
- include: declaration
Expand All @@ -360,29 +358,9 @@ contexts:
- match: (?=\S)
set:
- expect-semicolon
- import-export-assert
- import-export-from
- export-item

export-item:
- match: '\{'
scope: punctuation.section.block.begin.js
set: export-brace
- match: '\*'
scope: constant.other.js
set: import-export-alias
- include: else-pop

export-brace:
- meta_scope: meta.block.js
- include: comma-separator
- match: '\}'
scope: punctuation.section.block.end.js
pop: 1
- match: (?={{identifier_start}}|'|")
push:
- import-export-alias
- import-export-name
- include: else-pop
- import-export-item

statements:
- match: '\)|\}|\]'
Expand Down
30 changes: 20 additions & 10 deletions JavaScript/TypeScript.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ contexts:
set:
- expect-semicolon
- import-export-from
- export-item
- import-export-item
- match: (?=\S)
fail: ts-export-type-from

Expand Down Expand Up @@ -180,8 +180,18 @@ contexts:
- ts-type-parameter-list

ts-import-type:
- match: type{{identifier_break}}
scope: keyword.control.import-export.js
- match: (?=type{{identifier_break}})
pop: 1
branch_point: ts-import-type
branch:
- - match: type{{identifier_break}}
scope: keyword.control.import-export.js
set:
- match: (?=,|from{{identifier_break}})
fail: ts-import-type
- include: else-pop
- immediately-pop

- include: else-pop

import-statement:
Expand All @@ -190,20 +200,20 @@ contexts:
set:
- import-meta
- expect-semicolon
- import-assert
- import-export-assert
- import-string-or-items
- ts-import-type
- import-check-branch

import-brace:
- meta_prepend: true
- match: type{{identifier_break}}
scope: keyword.control.import-export.js

export-brace:
import-export-brace:
- meta_prepend: true
- match: type{{identifier_break}}
scope: keyword.control.import-export.js
- match: (?={{identifier_start}}|'|")
push:
- import-export-alias
- import-export-name
- ts-import-type

ts-type-declaration:
- match: type{{identifier_break}}
Expand Down
19 changes: 15 additions & 4 deletions JavaScript/tests/syntax_test_js_import_export.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import thing, {identifier as otherIdentifier}, * as otherName from "otherplace";
// ^ variable.other.readwrite
// ^ keyword.control.import-export
// ^ variable.other.readwrite
// ^ constant.other.js
// ^ constant.other.wildcard.asterisk
// ^ keyword.control.import-export

import 'module';
Expand All @@ -46,6 +46,10 @@ import foo from 'bar' assert { type: "json" };
// ^ punctuation.section.mapping.end.js
// ^ punctuation.terminator.statement.js


import foo from 'bar' assert { type: "json" };
// ^^^^^^ meta.import keyword.control.import-export

// Better highlighting while typing.
import
import;
Expand Down Expand Up @@ -160,7 +164,7 @@ export { name1 as default };
export * from "./othermod";
//^^^^^^^^^^^^^^^^^^^^^^^^^ meta.export
//^ keyword.control.import-export
// ^ constant.other
// ^ constant.other.wildcard.asterisk
// ^ keyword.control.import-export

export { name1, name2 } from "./othermod";
Expand Down Expand Up @@ -216,8 +220,15 @@ import from from "./othermod";
import { from } from "./othermod";
// ^^^^ variable.other.readwrite.js

export { from } from "./othermod";
// ^^^^ variable.other.readwrite.js
export {} from "./othermod" with {};
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.export
//^^^^ keyword.control.import-export
// ^^ meta.block
// ^^^^ keyword.control.import-export
// ^^^^^^^^^^^^ meta.string string.quoted.double
// ^^^^ keyword.control.import-export
// ^^ meta.mapping
// ^ punctuation.terminator.statement

export default$
// ^^^^^^^^ - keyword
Expand Down
8 changes: 8 additions & 0 deletions JavaScript/tests/syntax_test_typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ import foo;
// ^^^^ keyword.control.import-export
// ^^^^^^^^^^^ meta.string string.quoted.single

import type from 'somewhere';
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.import
// ^^^^^^ keyword.control.import-export
// ^^^^ variable.other.readwrite
// ^^^^ keyword.control.import-export
// ^^^^^^^^^^^ meta.string string.quoted.single
// ^ punctuation.terminator.statement

import type { U, V } from 'somewhere';
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.import
// ^^^^^^ keyword.control.import-export
Expand Down

0 comments on commit 78249eb

Please sign in to comment.