-
Notifications
You must be signed in to change notification settings - Fork 261
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
Updated TypeScript definitions #102
Closed
pineapplemachine
wants to merge
18
commits into
greggman:master
from
pineapplemachine:sophie/gen-ts-2
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ae831a7
auto generate ts file?
greggman cc5a390
TextureOptions.color is optional but documented as mandatory
pineapplemachine 06ec309
Fix type name capitalization: vertexArrayInfo => VertexArrayInfo
pineapplemachine f0f0bab
Replace unclear "Matrix" and "Matrix4" types with "module:twgl/m4.Mat4"
pineapplemachine 31dcc3e
Change helper function comment type as to not confuse grunt
pineapplemachine 2085445
incorporate Sophie script
greggman a03b167
setup ts example to test
greggman f17f7c3
Fix "mungets" regex patterns & clean up the task just a little
pineapplemachine c1c2b7b
Fix typo in "WebGLContextCreationAttributes" type name
pineapplemachine b19607e
Fix inaccurate documentation for Defaults object
pineapplemachine a9678a9
Remove param in createCresentBufferInfo & createCresentBuffers docs
pineapplemachine 994309f
More fixes & refinement to "tsmunge" grunt task
pineapplemachine 58eed76
Create a TypeScript test ("npm run testts")
pineapplemachine 0e66391
Commit new dist/4.x/*.d.ts files
pineapplemachine 3c331d1
Remove unneeded "tsc" devDependency
pineapplemachine 9f307b9
Fix files list in package.json
pineapplemachine 0ff70f0
Put Vec3 and Mat4 type declarations in module scope
pineapplemachine 42b7cf5
Tiny docs typo fixes
pineapplemachine 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 |
---|---|---|
|
@@ -3,4 +3,4 @@ | |
.esm-cache | ||
node_modules | ||
package-lock.json | ||
|
||
test/src/ts/dist/* |
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 |
---|---|---|
|
@@ -154,6 +154,15 @@ module.exports = function(grunt) { | |
outputSourceFiles: false, | ||
}, | ||
}, | ||
ts: { | ||
src: docsFiles, | ||
options: { | ||
destination: 'dist/4.x', | ||
configure: 'build/jsdoc.conf.json', | ||
template: './node_modules/tsd-jsdoc/dist', | ||
outputSourceFiles: false, | ||
}, | ||
}, | ||
}, | ||
webpack: { | ||
full: { | ||
|
@@ -264,6 +273,11 @@ module.exports = function(grunt) { | |
dist: [ `dist/${verDir}` ], | ||
docs: [ 'docs' ], | ||
}, | ||
ts: { | ||
example: { | ||
src: ['examples/*.ts'], | ||
}, | ||
}, | ||
}); | ||
|
||
grunt.registerTask('makeindex', function() { | ||
|
@@ -343,16 +357,101 @@ module.exports = function(grunt) { | |
fs.writeFileSync("npm/base/package.json", JSON.stringify(p, null, 2), {encoding: "utf8"}); | ||
}); | ||
|
||
grunt.registerTask('tsmunge', function() { | ||
// Fix up syntax and content issues with the auto-generated | ||
// TypeScript definitions. | ||
let content = fs.readFileSync('dist/4.x/types.d.ts', {encoding: 'utf8'}); | ||
// These strings will be useful later | ||
const glEnumToString = ` | ||
export function glEnumToString(gl: WebGLRenderingContext, value: number): string; | ||
`; | ||
const creationAttributes = `export interface WebGLContextCreationAttributes { | ||
alpha?: boolean; | ||
antialias?: boolean; | ||
depth?: boolean; | ||
failIfMajorPerformanceCaveat?: boolean; | ||
powerPreference?: string; | ||
premultipliedAlpha?: boolean; | ||
preserveDrawingBuffer?: boolean; | ||
stencil?: boolean; | ||
}`.replace(/^ {4}/mg, ''); | ||
// Remove docstrings (Declarations do not by convention include these) | ||
content = content.replace(/\/\*\*.*?\*\/\s*/sg, ''); | ||
// Docs use "?" to represent an arbitrary type; TS uses "any" | ||
content = content.replace(/\]: \?/g, ']: any'); | ||
// Docs use "constructor"; TS expects something more like "Function" | ||
content = content.replace(/: constructor/g, ': Function'); | ||
// Docs use "ArrayBufferViewType" to describe a TypedArray constructor | ||
content = content.replace(/\bArrayBufferViewType\b/g, 'Function'); | ||
// What docs call "TypedArray", lib.d.ts calls "ArrayBufferView" | ||
content = content.replace(/\bTypedArray\b/g, 'ArrayBufferView'); | ||
// What docs call an "augmentedTypedArray" is technically an "ArrayBufferView" | ||
// albeit with a patched-in "push" method. | ||
content = content.replace(/\baugmentedTypedArray\b/g, 'ArrayBufferView'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO (though outside the scope of this PR): Describe this type more accurately. I think this would work but I'm not certain. export interface AugmentedTypedArray extends ArrayBufferView {
push: (..args: any[]): void;
} |
||
// Docs use "enum"; TS expects "GLenum" | ||
// https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Types | ||
content = content.replace(/: enum/g, ': GLenum'); | ||
// Remove every instance of "module:twgl" and "module:twgl/whatever" | ||
// Except for "module:twgl.(m4|v3|primitives)" which become just "m4.", etc. | ||
content = content.replace(/module:twgl(\/(m4|v3|primitives))\./g, '$2.'); | ||
content = content.replace(/module:twgl(\/\w+)?\./g, ''); | ||
// Replace "function", "type" declarations with "export function", "export type" | ||
content = content.replace(/^(\s*)(function|type) /mg, '$1export $2 '); | ||
// Fixup dynamically generated glEnumToString function signature | ||
content = content.replace(/var glEnumToString: any;/g, glEnumToString); | ||
// Break the file down into a list of modules | ||
const modules = content.match(/^declare module twgl(\/(\w+))? \{.*?^\}/msg); | ||
// Split into core modules and extra (only in twgl-full) modules | ||
const coreModules = modules.filter( | ||
(code) => !code.match(/^declare module twgl\/(m4|v3|primitives)/) | ||
); | ||
const extraModules = modules.filter( | ||
(code) => code.match(/^declare module twgl\/(m4|v3|primitives)/) | ||
); | ||
// Build code for the core twgl.js output | ||
let coreContent = coreModules.map((code) => { | ||
// Get rid of "declare module twgl/whatever" scope | ||
code = code.replace(/^declare module twgl(\/\w+)? \{(.*?)^\}/msg, "$2"); | ||
// De-indent the contents of that scope | ||
code = code.replace(/^ {4}/mg, ''); | ||
// All done | ||
return code; | ||
}).join("\n"); | ||
// Include type describing canvas.getContext input attributes | ||
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext | ||
coreContent = [creationAttributes, coreContent].join("\n"); | ||
// Build additional code for the extended twgl-full.js output | ||
let extraContent = extraModules.map((code) => { | ||
// Fix "declare module twgl/whatever" statements | ||
return code.replace(/^declare module twgl(\/(\w+))? \{/m, | ||
"declare module $2 {" | ||
); | ||
}).join("\n"); | ||
// Write twgl-full declarations to destination file | ||
const fullContent = [coreContent, extraContent].join("\n"); | ||
fs.writeFileSync('dist/4.x/twgl-full.d.ts', fullContent); | ||
// Write core declarations to destination file | ||
fs.writeFileSync('dist/4.x/twgl.d.ts', coreContent); | ||
// Remove the auto-generated input file | ||
fs.unlinkSync('dist/4.x/types.d.ts'); | ||
}); | ||
|
||
grunt.registerTask('docs', [ | ||
'eslint:examples', | ||
'clean:docs', | ||
'jsdoc', | ||
'jsdoc:docs', | ||
'makeindex', | ||
]); | ||
grunt.registerTask('buildts', [ | ||
'jsdoc:ts', | ||
'tsmunge', | ||
]); | ||
grunt.registerTask('build', [ | ||
'eslint:lib', | ||
'clean:dist', | ||
'webpack', | ||
'buildts', | ||
'ts', | ||
'copy', | ||
'npmpackage', | ||
]); | ||
|
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.
TODO (though outside the scope of this PR): I think this input constructor type can be described more accurately. I think this typing would work but I'm not certain.