Skip to content

Commit

Permalink
Type-only imports and exports (#792)
Browse files Browse the repository at this point in the history
* Support type-only imports and exports

* Fix copy/paste error

* Update baseline

* Handle import export type at local matching rules

Co-authored-by: Sheetal Nandi <shkamat@microsoft.com>
  • Loading branch information
andrewbranch and sheetalkamat committed Jan 7, 2020
1 parent b7ef094 commit 1632051
Show file tree
Hide file tree
Showing 8 changed files with 354 additions and 39 deletions.
33 changes: 19 additions & 14 deletions TypeScript.YAML-tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -1066,29 +1066,31 @@ repository:
import-equals-declaration:
patterns:
- name: meta.import-equals.external.ts
begin: '{{startOfDeclaration}}\b(import)\s+({{identifier}})\s*(=)\s*(require)\s*(\()'
begin: '{{startOfDeclaration}}\b(import)(?:\s+(type))?\s+({{identifier}})\s*(=)\s*(require)\s*(\()'
beginCaptures:
'1': { name: keyword.control.export.ts }
'2': { name: storage.modifier.ts }
'3': { name: keyword.control.import.ts }
'4': { name: variable.other.readwrite.alias.ts }
'5': { name: keyword.operator.assignment.ts }
'6': { name: keyword.control.require.ts }
'7': { name: meta.brace.round.ts }
'4': { name: keyword.control.type.ts }
'5': { name: variable.other.readwrite.alias.ts }
'6': { name: keyword.operator.assignment.ts }
'7': { name: keyword.control.require.ts }
'8': { name: meta.brace.round.ts }
end: \)
endCaptures:
'0': { name: meta.brace.round.ts }
patterns:
- include: '#comment'
- include: '#string'
- name: meta.import-equals.internal.ts
begin: '{{startOfDeclaration}}\b(import)\s+({{identifier}})\s*(=)\s*(?!require\b)'
begin: '{{startOfDeclaration}}\b(import)(?:\s+(type))?\s+({{identifier}})\s*(=)\s*(?!require\b)'
beginCaptures:
'1': { name: keyword.control.export.ts }
'2': { name: storage.modifier.ts }
'3': { name: keyword.control.import.ts }
'4': { name: variable.other.readwrite.alias.ts }
'5': { name: keyword.operator.assignment.ts }
'4': { name: keyword.control.type.ts }
'5': { name: variable.other.readwrite.alias.ts }
'6': { name: keyword.operator.assignment.ts }
end: (?=;|$|^)
patterns:
- include: '#single-line-comment-consuming-line-ending'
Expand All @@ -1103,11 +1105,12 @@ repository:

import-declaration:
name: meta.import.ts
begin: '{{startOfDeclaration}}\b(import)(?!\s*[:\(]){{endOfIdentifier}}'
begin: '{{startOfDeclaration}}\b(import)(?:\s+(type)(?!\s+from))?(?!\s*[:\(]){{endOfIdentifier}}'
beginCaptures:
'1': { name: keyword.control.export.ts }
'2': { name: storage.modifier.ts }
'3': { name: keyword.control.import.ts }
'4': { name: keyword.control.type.ts }
end: (?<!{{lookBehindImport}})(?=;|$|^)
patterns:
- include: '#single-line-comment-consuming-line-ending'
Expand All @@ -1130,19 +1133,21 @@ repository:
'3': { name: storage.type.namespace.ts }
'4': { name: entity.name.type.module.ts }
- name: meta.export.default.ts
begin: '{{startOfIdentifier}}(export)(?:(?:\s*(=))|(?:\s+(default)(?=\s+)))'
begin: '{{startOfIdentifier}}(export)(?:\s+(type))?(?:(?:\s*(=))|(?:\s+(default)(?=\s+)))'
beginCaptures:
'1': { name: keyword.control.export.ts }
'2': { name: keyword.operator.assignment.ts }
'3': { name: keyword.control.default.ts }
'2': { name: keyword.control.type.ts }
'3': { name: keyword.operator.assignment.ts }
'4': { name: keyword.control.default.ts }
end: (?=$|{{endOfStatement}})
patterns:
- include: '#interface-declaration'
- include: '#expression'
- name: meta.export.ts
begin: '{{startOfIdentifier}}(export)\b(?!(\$)|(\s*:))((?=\s*[\{*])|((?=\s*{{identifier}}(\s|,))(?!\s*{{constructsAndModifiers}})))'
begin: '{{startOfIdentifier}}(export)(?:\s+(type))?\b(?!(\$)|(\s*:))((?=\s*[\{*])|((?=\s*{{identifier}}(\s|,))(?!\s*{{constructsAndModifiers}})))'
beginCaptures:
'0': { name: keyword.control.export.ts }
'1': { name: keyword.control.export.ts }
'2': { name: keyword.control.type.ts }
end: (?=$|{{endOfStatement}})
patterns:
- include: '#import-export-declaration'
Expand Down
47 changes: 36 additions & 11 deletions TypeScript.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -3382,7 +3382,7 @@
<key>name</key>
<string>meta.import-equals.external.ts</string>
<key>begin</key>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(?:(\bexport)\s+)?(?:(\bdeclare)\s+)?\b(import)\s+([_$[:alpha:]][_$[:alnum:]]*)\s*(=)\s*(require)\s*(\()</string>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(?:(\bexport)\s+)?(?:(\bdeclare)\s+)?\b(import)(?:\s+(type))?\s+([_$[:alpha:]][_$[:alnum:]]*)\s*(=)\s*(require)\s*(\()</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
Expand All @@ -3403,19 +3403,24 @@
<key>4</key>
<dict>
<key>name</key>
<string>variable.other.readwrite.alias.ts</string>
<string>keyword.control.type.ts</string>
</dict>
<key>5</key>
<dict>
<key>name</key>
<string>keyword.operator.assignment.ts</string>
<string>variable.other.readwrite.alias.ts</string>
</dict>
<key>6</key>
<dict>
<key>name</key>
<string>keyword.control.require.ts</string>
<string>keyword.operator.assignment.ts</string>
</dict>
<key>7</key>
<dict>
<key>name</key>
<string>keyword.control.require.ts</string>
</dict>
<key>8</key>
<dict>
<key>name</key>
<string>meta.brace.round.ts</string>
Expand Down Expand Up @@ -3447,7 +3452,7 @@
<key>name</key>
<string>meta.import-equals.internal.ts</string>
<key>begin</key>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(?:(\bexport)\s+)?(?:(\bdeclare)\s+)?\b(import)\s+([_$[:alpha:]][_$[:alnum:]]*)\s*(=)\s*(?!require\b)</string>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(?:(\bexport)\s+)?(?:(\bdeclare)\s+)?\b(import)(?:\s+(type))?\s+([_$[:alpha:]][_$[:alnum:]]*)\s*(=)\s*(?!require\b)</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
Expand All @@ -3468,9 +3473,14 @@
<key>4</key>
<dict>
<key>name</key>
<string>variable.other.readwrite.alias.ts</string>
<string>keyword.control.type.ts</string>
</dict>
<key>5</key>
<dict>
<key>name</key>
<string>variable.other.readwrite.alias.ts</string>
</dict>
<key>6</key>
<dict>
<key>name</key>
<string>keyword.operator.assignment.ts</string>
Expand Down Expand Up @@ -3525,7 +3535,7 @@
<key>name</key>
<string>meta.import.ts</string>
<key>begin</key>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(?:(\bexport)\s+)?(?:(\bdeclare)\s+)?\b(import)(?!\s*[:\(])(?![_$[:alnum:]])(?:(?=\.\.\.)|(?!\.))</string>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(?:(\bexport)\s+)?(?:(\bdeclare)\s+)?\b(import)(?:\s+(type)(?!\s+from))?(?!\s*[:\(])(?![_$[:alnum:]])(?:(?=\.\.\.)|(?!\.))</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
Expand All @@ -3543,6 +3553,11 @@
<key>name</key>
<string>keyword.control.import.ts</string>
</dict>
<key>4</key>
<dict>
<key>name</key>
<string>keyword.control.type.ts</string>
</dict>
</dict>
<key>end</key>
<string>(?&lt;!^import|[^\._$[:alnum:]]import)(?=;|$|^)</string>
Expand Down Expand Up @@ -3622,7 +3637,7 @@
<key>name</key>
<string>meta.export.default.ts</string>
<key>begin</key>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(export)(?:(?:\s*(=))|(?:\s+(default)(?=\s+)))</string>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(export)(?:\s+(type))?(?:(?:\s*(=))|(?:\s+(default)(?=\s+)))</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
Expand All @@ -3633,9 +3648,14 @@
<key>2</key>
<dict>
<key>name</key>
<string>keyword.operator.assignment.ts</string>
<string>keyword.control.type.ts</string>
</dict>
<key>3</key>
<dict>
<key>name</key>
<string>keyword.operator.assignment.ts</string>
</dict>
<key>4</key>
<dict>
<key>name</key>
<string>keyword.control.default.ts</string>
Expand All @@ -3659,14 +3679,19 @@
<key>name</key>
<string>meta.export.ts</string>
<key>begin</key>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(export)\b(?!(\$)|(\s*:))((?=\s*[\{*])|((?=\s*[_$[:alpha:]][_$[:alnum:]]*(\s|,))(?!\s*(?:abstract|async|class|const|declare|enum|export|function|import|interface|let|module|namespace|return|type|var)\b)))</string>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(export)(?:\s+(type))?\b(?!(\$)|(\s*:))((?=\s*[\{*])|((?=\s*[_$[:alpha:]][_$[:alnum:]]*(\s|,))(?!\s*(?:abstract|async|class|const|declare|enum|export|function|import|interface|let|module|namespace|return|type|var)\b)))</string>
<key>beginCaptures</key>
<dict>
<key>0</key>
<key>1</key>
<dict>
<key>name</key>
<string>keyword.control.export.ts</string>
</dict>
<key>2</key>
<dict>
<key>name</key>
<string>keyword.control.type.ts</string>
</dict>
</dict>
<key>end</key>
<string>(?=$|;|(?:^\s*(?:abstract|async|class|const|declare|enum|export|function|import|interface|let|module|namespace|return|type|var)\b))</string>
Expand Down
47 changes: 36 additions & 11 deletions TypeScriptReact.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -3404,7 +3404,7 @@
<key>name</key>
<string>meta.import-equals.external.tsx</string>
<key>begin</key>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(?:(\bexport)\s+)?(?:(\bdeclare)\s+)?\b(import)\s+([_$[:alpha:]][_$[:alnum:]]*)\s*(=)\s*(require)\s*(\()</string>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(?:(\bexport)\s+)?(?:(\bdeclare)\s+)?\b(import)(?:\s+(type))?\s+([_$[:alpha:]][_$[:alnum:]]*)\s*(=)\s*(require)\s*(\()</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
Expand All @@ -3425,19 +3425,24 @@
<key>4</key>
<dict>
<key>name</key>
<string>variable.other.readwrite.alias.tsx</string>
<string>keyword.control.type.tsx</string>
</dict>
<key>5</key>
<dict>
<key>name</key>
<string>keyword.operator.assignment.tsx</string>
<string>variable.other.readwrite.alias.tsx</string>
</dict>
<key>6</key>
<dict>
<key>name</key>
<string>keyword.control.require.tsx</string>
<string>keyword.operator.assignment.tsx</string>
</dict>
<key>7</key>
<dict>
<key>name</key>
<string>keyword.control.require.tsx</string>
</dict>
<key>8</key>
<dict>
<key>name</key>
<string>meta.brace.round.tsx</string>
Expand Down Expand Up @@ -3469,7 +3474,7 @@
<key>name</key>
<string>meta.import-equals.internal.tsx</string>
<key>begin</key>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(?:(\bexport)\s+)?(?:(\bdeclare)\s+)?\b(import)\s+([_$[:alpha:]][_$[:alnum:]]*)\s*(=)\s*(?!require\b)</string>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(?:(\bexport)\s+)?(?:(\bdeclare)\s+)?\b(import)(?:\s+(type))?\s+([_$[:alpha:]][_$[:alnum:]]*)\s*(=)\s*(?!require\b)</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
Expand All @@ -3490,9 +3495,14 @@
<key>4</key>
<dict>
<key>name</key>
<string>variable.other.readwrite.alias.tsx</string>
<string>keyword.control.type.tsx</string>
</dict>
<key>5</key>
<dict>
<key>name</key>
<string>variable.other.readwrite.alias.tsx</string>
</dict>
<key>6</key>
<dict>
<key>name</key>
<string>keyword.operator.assignment.tsx</string>
Expand Down Expand Up @@ -3547,7 +3557,7 @@
<key>name</key>
<string>meta.import.tsx</string>
<key>begin</key>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(?:(\bexport)\s+)?(?:(\bdeclare)\s+)?\b(import)(?!\s*[:\(])(?![_$[:alnum:]])(?:(?=\.\.\.)|(?!\.))</string>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(?:(\bexport)\s+)?(?:(\bdeclare)\s+)?\b(import)(?:\s+(type)(?!\s+from))?(?!\s*[:\(])(?![_$[:alnum:]])(?:(?=\.\.\.)|(?!\.))</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
Expand All @@ -3565,6 +3575,11 @@
<key>name</key>
<string>keyword.control.import.tsx</string>
</dict>
<key>4</key>
<dict>
<key>name</key>
<string>keyword.control.type.tsx</string>
</dict>
</dict>
<key>end</key>
<string>(?&lt;!^import|[^\._$[:alnum:]]import)(?=;|$|^)</string>
Expand Down Expand Up @@ -3644,7 +3659,7 @@
<key>name</key>
<string>meta.export.default.tsx</string>
<key>begin</key>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(export)(?:(?:\s*(=))|(?:\s+(default)(?=\s+)))</string>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(export)(?:\s+(type))?(?:(?:\s*(=))|(?:\s+(default)(?=\s+)))</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
Expand All @@ -3655,9 +3670,14 @@
<key>2</key>
<dict>
<key>name</key>
<string>keyword.operator.assignment.tsx</string>
<string>keyword.control.type.tsx</string>
</dict>
<key>3</key>
<dict>
<key>name</key>
<string>keyword.operator.assignment.tsx</string>
</dict>
<key>4</key>
<dict>
<key>name</key>
<string>keyword.control.default.tsx</string>
Expand All @@ -3681,14 +3701,19 @@
<key>name</key>
<string>meta.export.tsx</string>
<key>begin</key>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(export)\b(?!(\$)|(\s*:))((?=\s*[\{*])|((?=\s*[_$[:alpha:]][_$[:alnum:]]*(\s|,))(?!\s*(?:abstract|async|class|const|declare|enum|export|function|import|interface|let|module|namespace|return|type|var)\b)))</string>
<string>(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(export)(?:\s+(type))?\b(?!(\$)|(\s*:))((?=\s*[\{*])|((?=\s*[_$[:alpha:]][_$[:alnum:]]*(\s|,))(?!\s*(?:abstract|async|class|const|declare|enum|export|function|import|interface|let|module|namespace|return|type|var)\b)))</string>
<key>beginCaptures</key>
<dict>
<key>0</key>
<key>1</key>
<dict>
<key>name</key>
<string>keyword.control.export.tsx</string>
</dict>
<key>2</key>
<dict>
<key>name</key>
<string>keyword.control.type.tsx</string>
</dict>
</dict>
<key>end</key>
<string>(?=$|;|(?:^\s*(?:abstract|async|class|const|declare|enum|export|function|import|interface|let|module|namespace|return|type|var)\b))</string>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"pretest": "npm run build",
"test": "mocha --full-trace tests/test.js --reporter mocha-multi-reporters",
"diff": "cross-env-shell $DIFF tests/baselines tests/generated",
"accept": "copyfiles \"tests/generated/*\" tests/baselines"
"accept": "copyfiles -u 2 \"tests/generated/*\" tests/baselines"
},
"dependencies": {
"@types/chai": "latest",
Expand Down
Loading

0 comments on commit 1632051

Please sign in to comment.