diff --git a/types/syntax-error/index.d.ts b/types/syntax-error/index.d.ts new file mode 100644 index 0000000000..68bbba8346 --- /dev/null +++ b/types/syntax-error/index.d.ts @@ -0,0 +1,18 @@ +// Type definitions for syntax-error 1.4 +// Project: https://github.com/browserify/syntax-error +// Definitions by: TeamworkGuy2 +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import * as acorn from "acorn"; + +/** + * Check the source code string 'src' for syntax errors. Optionally you can specify a filename + * file that will show up in the output. + * If 'src' has a syntax error, return an error object err that can be printed or stringified. + * If there are no syntax errors in 'src', return undefined. + * Options will be passed through to acorn-node. acorn-node defaults to options + * that match the most recent Node versions. + */ +declare function syntaxError(src: any, file?: string, opts?: acorn.Options): (SyntaxError & { line: number; column: number; annotated: string; inspect(): string }) | undefined; + +export = syntaxError; diff --git a/types/syntax-error/syntax-error-tests.ts b/types/syntax-error/syntax-error-tests.ts new file mode 100644 index 0000000000..39a35b345f --- /dev/null +++ b/types/syntax-error/syntax-error-tests.ts @@ -0,0 +1,12 @@ +import syntaxError = require("syntax-error"); + +function test(): SyntaxError | undefined { + const res1 = syntaxError("(function(){ console.log('do something!'); }());", "motivation.js"); + const res2 = syntaxError("(function(){ console.log('do something!'); }());", "motivation.js", { ecmaVersion: 2015, sourceType: "script" }); + if (res2 != null) { + if (res2.line === 1) { + const msg = `${res2.name} (${res2.line}:${Math.abs(res2.column)}): ${res2.message}\n${res2.stack}`; + } + } + return res1 || res2; +} diff --git a/types/syntax-error/tsconfig.json b/types/syntax-error/tsconfig.json new file mode 100644 index 0000000000..4f9ec03802 --- /dev/null +++ b/types/syntax-error/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "syntax-error-tests.ts" + ] +} \ No newline at end of file diff --git a/types/syntax-error/tslint.json b/types/syntax-error/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/syntax-error/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }