-
Notifications
You must be signed in to change notification settings - Fork 122
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
refactor: build template.json grammar from typescript source files #581
Merged
ayazhafiz
merged 16 commits into
angular:master
from
dannymcgee:refactor-grammars-to-ts-build-system
Jan 28, 2020
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fc20b19
Added ts-node dev dependency
dannymcgee 15c8bdc
Added tsconfig, types and build script
dannymcgee a2d26f6
Added source files for Template grammar
dannymcgee 6e1ca71
npm script for building grammars
dannymcgee d334fbb
Built template.json from source
dannymcgee daebd10
Formatting
dannymcgee b7ee679
Added final newline to build script
dannymcgee d85b310
Fixed incorrect type
dannymcgee fa25c7d
Removed unnecessary type literal abstractions
dannymcgee c4b5bea
Refactored to function declarations
dannymcgee 78563d2
Refactored and added comments for clarity
dannymcgee a16f4ac
Removed ts-node dev dependency
dannymcgee 9e3c004
Bugfix: index.ts not resolved as expected when using WSL
dannymcgee ad747b9
Integrated syntax compilation into build script
dannymcgee 04546b3
Formatting
dannymcgee a10718c
Consolidated Template grammar files
dannymcgee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,6 @@ dist/ | |
.vscode/settings.json | ||
.vimrc | ||
.nvimrc | ||
syntaxes/out | ||
syntaxes/test/*.js | ||
syntaxes/test/*.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ set -ex -o pipefail | |
# Clean up from last build | ||
rm -rf client/out | ||
rm -rf server/out | ||
rm -rf syntaxes/out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import * as fs from 'fs'; | ||
|
||
import {GrammarDefinition, JsonObject} from './types'; | ||
import {template} from './template/grammar'; | ||
|
||
// Recursively transforms a TypeScript grammar definition into an object which can be processed by | ||
// JSON.stringify to generate a valid TextMate JSON grammar definition | ||
function processGrammar(grammar: GrammarDefinition): JsonObject { | ||
const processedGrammar: JsonObject = {}; | ||
for (const [key, value] of Object.entries(grammar)) { | ||
if (typeof value === 'string') { | ||
processedGrammar[key] = value; | ||
} else if (value instanceof RegExp) { | ||
// Escape backslashes/quote marks and trim the demarcating `/` characters from a regex literal | ||
processedGrammar[key] = value.toString().replace(/^\/|\/$/g, ''); | ||
} else if (value instanceof Array) { | ||
processedGrammar[key] = value.map(processGrammar); | ||
} else { | ||
processedGrammar[key] = processGrammar(value); | ||
} | ||
} | ||
return processedGrammar; | ||
} | ||
|
||
// Build a TextMate grammar JSON file from a source TypeScript object | ||
function build(grammar: GrammarDefinition, filename: string): void { | ||
const processedGrammar: JsonObject = processGrammar(grammar); | ||
const grammarContent: string = JSON.stringify(processedGrammar, null, ' ') + '\n'; | ||
|
||
fs.writeFile(`syntaxes/out/${filename}.json`, grammarContent, (error) => { | ||
if (error) throw error; | ||
}); | ||
} | ||
|
||
build(template, 'template'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {GrammarDefinition} from '../types'; | ||
|
||
import {bindingKey} from './repository/binding-key'; | ||
import {eventBinding} from './repository/event-binding'; | ||
import {interpolation} from './repository/interpolation'; | ||
import {propertyBinding} from './repository/property-binding'; | ||
import {templateBinding} from './repository/template-binding'; | ||
import {twoWayBinding} from './repository/two-way-binding'; | ||
|
||
export const template: GrammarDefinition = { | ||
scopeName: 'template.ng', | ||
injectionSelector: 'L:text.html -comment', | ||
patterns: [ | ||
{include: '#interpolation'}, | ||
{include: '#propertyBinding'}, | ||
{include: '#eventBinding'}, | ||
{include: '#twoWayBinding'}, | ||
{include: '#templateBinding'}, | ||
], | ||
repository: { | ||
interpolation, | ||
propertyBinding, | ||
eventBinding, | ||
twoWayBinding, | ||
templateBinding, | ||
bindingKey, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {GrammarDefinition} from '../../types'; | ||
|
||
export const bindingKey: GrammarDefinition = { | ||
patterns: [ | ||
{ | ||
match: /([\[\(]{1,2})(?:\s*)(@?[-_a-zA-Z0-9.$]*)(?:\s*)([\]\)]{1,2})/, | ||
captures: { | ||
1: {name: 'punctuation.definition.ng-binding-name.begin.html'}, | ||
2: { | ||
patterns: [ | ||
{ | ||
match: /\./, | ||
name: 'punctuation.accessor.html', | ||
}, | ||
], | ||
}, | ||
3: {name: 'punctuation.definition.ng-binding-name.end.html'}, | ||
}, | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {GrammarDefinition} from '../../types'; | ||
|
||
export const eventBinding: GrammarDefinition = { | ||
begin: /(\(\s*@?[-_a-zA-Z0-9.$]*\s*\))(=)(["'])/, | ||
beginCaptures: { | ||
1: { | ||
name: 'entity.other.attribute-name.html entity.other.ng-binding-name.event.html', | ||
patterns: [ | ||
{include: '#bindingKey'}, | ||
], | ||
}, | ||
2: {name: 'punctuation.separator.key-value.html'}, | ||
3: {name: 'string.quoted.html punctuation.definition.string.begin.html'}, | ||
}, | ||
end: /\3/, | ||
endCaptures: { | ||
0: {name: 'string.quoted.html punctuation.definition.string.end.html'}, | ||
}, | ||
name: 'meta.ng-binding.event.html', | ||
contentName: 'source.js', | ||
patterns: [ | ||
{include: 'source.js'}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {GrammarDefinition} from '../../types'; | ||
|
||
export const interpolation: GrammarDefinition = { | ||
begin: /{{/, | ||
beginCaptures: { | ||
0: {name: 'punctuation.definition.block.ts'}, | ||
}, | ||
end: /}}/, | ||
endCaptures: { | ||
0: {name: 'punctuation.definition.block.ts'}, | ||
}, | ||
contentName: 'source.js', | ||
patterns: [ | ||
{include: 'source.js'}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {GrammarDefinition} from '../../types'; | ||
|
||
export const propertyBinding: GrammarDefinition = { | ||
begin: /(\[\s*@?[-_a-zA-Z0-9.$]*\s*])(=)(["'])/, | ||
beginCaptures: { | ||
1: { | ||
name: 'entity.other.attribute-name.html entity.other.ng-binding-name.property.html', | ||
patterns: [ | ||
{include: '#bindingKey'}, | ||
], | ||
}, | ||
2: {name: 'punctuation.separator.key-value.html'}, | ||
3: {name: 'string.quoted.html punctuation.definition.string.begin.html'}, | ||
}, | ||
end: /\3/, | ||
endCaptures: { | ||
0: {name: 'string.quoted.html punctuation.definition.string.end.html'}, | ||
}, | ||
name: 'meta.ng-binding.property.html', | ||
contentName: 'source.js', | ||
patterns: [ | ||
{include: 'source.js'}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {GrammarDefinition} from '../../types'; | ||
|
||
export const templateBinding: GrammarDefinition = { | ||
begin: /(\*[-_a-zA-Z0-9.$]*)(=)(["'])/, | ||
beginCaptures: { | ||
1: { | ||
name: 'entity.other.attribute-name.html entity.other.ng-binding-name.template.html', | ||
patterns: [ | ||
{include: '#bindingKey'}, | ||
], | ||
}, | ||
2: {name: 'punctuation.separator.key-value.html'}, | ||
3: {name: 'string.quoted.html punctuation.definition.string.begin.html'}, | ||
}, | ||
end: /\3/, | ||
endCaptures: { | ||
0: {name: 'string.quoted.html punctuation.definition.string.end.html'}, | ||
}, | ||
name: 'meta.ng-binding.template.html', | ||
contentName: 'source.js', | ||
patterns: [ | ||
{include: 'source.js'}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {GrammarDefinition} from '../../types'; | ||
|
||
export const twoWayBinding: GrammarDefinition = { | ||
begin: /(\[\s*\(\s*@?[-_a-zA-Z0-9.$]*\s*\)\s*\])(=)(["'])/, | ||
beginCaptures: { | ||
1: { | ||
name: 'entity.other.attribute-name.html entity.other.ng-binding-name.two-way.html', | ||
patterns: [ | ||
{include: '#bindingKey'}, | ||
], | ||
}, | ||
2: {name: 'punctuation.separator.key-value.html'}, | ||
3: {name: 'string.quoted.html punctuation.definition.string.begin.html'}, | ||
}, | ||
end: /\3/, | ||
endCaptures: { | ||
0: {name: 'string.quoted.html punctuation.definition.string.end.html'}, | ||
}, | ||
name: 'meta.ng-binding.two-way.html', | ||
contentName: 'source.js', | ||
patterns: [ | ||
{include: 'source.js'}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
export interface GrammarDefinition { | ||
[key: string]: string|RegExp|GrammarDefinition|GrammarDefinition[]; | ||
} | ||
|
||
export interface JsonObject { | ||
[key: string]: string|JsonObject|JsonObject[]; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, i see. nice!