-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(disableTypeChecks): add option 'true' to disable all type checks (…
…#3765) Co-authored-by: Nico Jansen <jansennico@gmail.com>
- Loading branch information
Showing
13 changed files
with
117 additions
and
67 deletions.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import path from 'path'; | ||
|
||
import { AstByFormat, AstFormat } from '../syntax/index.js'; | ||
|
||
import { createParser as createJSParser } from './js-parser.js'; | ||
import { parseTS, parseTsx } from './ts-parser.js'; | ||
import { parse as htmlParse } from './html-parser.js'; | ||
import { ParserOptions } from './parser-options.js'; | ||
|
||
export function createParser( | ||
parserOptions: ParserOptions | ||
): <T extends AstFormat = AstFormat>(code: string, fileName: string, formatOverride?: T | undefined) => Promise<AstByFormat[T]> { | ||
const jsParse = createJSParser(parserOptions); | ||
return async function parse<T extends AstFormat = AstFormat>(code: string, fileName: string, formatOverride?: T): Promise<AstByFormat[T]> { | ||
const format = getFormat(fileName, formatOverride); | ||
if (!format) { | ||
const ext = path.extname(fileName).toLowerCase(); | ||
throw new Error(`Unable to parse ${fileName}. No parser registered for ${ext}!`); | ||
} | ||
switch (format) { | ||
case AstFormat.JS: | ||
return jsParse(code, fileName) as Promise<AstByFormat[T]>; | ||
case AstFormat.Tsx: | ||
return parseTsx(code, fileName) as Promise<AstByFormat[T]>; | ||
case AstFormat.TS: | ||
return parseTS(code, fileName) as Promise<AstByFormat[T]>; | ||
case AstFormat.Html: | ||
return htmlParse(code, fileName, { parse }) as Promise<AstByFormat[T]>; | ||
} | ||
}; | ||
} | ||
|
||
export function getFormat(fileName: string, override?: AstFormat): AstFormat | undefined { | ||
if (override) { | ||
return override; | ||
} else { | ||
const ext = path.extname(fileName).toLowerCase(); | ||
switch (ext) { | ||
case '.js': | ||
case '.jsx': | ||
case '.mjs': | ||
case '.cjs': | ||
return AstFormat.JS; | ||
case '.ts': | ||
return AstFormat.TS; | ||
case '.tsx': | ||
return AstFormat.Tsx; | ||
case '.vue': | ||
case '.html': | ||
case '.htm': | ||
return AstFormat.Html; | ||
default: | ||
return; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,54 +1,5 @@ | ||
import path from 'path'; | ||
|
||
import { AstFormat, AstByFormat } from '../syntax/index.js'; | ||
|
||
import { createParser as createJSParser } from './js-parser.js'; | ||
import { parseTS, parseTsx } from './ts-parser.js'; | ||
import { parse as htmlParse } from './html-parser.js'; | ||
import { ParserOptions } from './parser-options.js'; | ||
import { createParser, getFormat } from './create-parser.js'; | ||
|
||
export type { ParserOptions }; | ||
|
||
export function createParser( | ||
parserOptions: ParserOptions | ||
): <T extends AstFormat = AstFormat>(code: string, fileName: string, formatOverride?: T | undefined) => Promise<AstByFormat[T]> { | ||
const jsParse = createJSParser(parserOptions); | ||
return function parse<T extends AstFormat = AstFormat>(code: string, fileName: string, formatOverride?: T): Promise<AstByFormat[T]> { | ||
const format = getFormat(fileName, formatOverride); | ||
switch (format) { | ||
case AstFormat.JS: | ||
return jsParse(code, fileName) as Promise<AstByFormat[T]>; | ||
case AstFormat.Tsx: | ||
return parseTsx(code, fileName) as Promise<AstByFormat[T]>; | ||
case AstFormat.TS: | ||
return parseTS(code, fileName) as Promise<AstByFormat[T]>; | ||
case AstFormat.Html: | ||
return htmlParse(code, fileName, { parse }) as Promise<AstByFormat[T]>; | ||
} | ||
}; | ||
} | ||
|
||
export function getFormat(fileName: string, override?: AstFormat): AstFormat { | ||
if (override) { | ||
return override; | ||
} else { | ||
const ext = path.extname(fileName).toLowerCase(); | ||
switch (ext) { | ||
case '.js': | ||
case '.jsx': | ||
case '.mjs': | ||
case '.cjs': | ||
return AstFormat.JS; | ||
case '.ts': | ||
return AstFormat.TS; | ||
case '.tsx': | ||
return AstFormat.Tsx; | ||
case '.vue': | ||
case '.html': | ||
case '.htm': | ||
return AstFormat.Html; | ||
default: | ||
throw new Error(`Unable to parse ${fileName}. No parser registered for ${ext}!`); | ||
} | ||
} | ||
} | ||
export { createParser, getFormat }; |
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
10 changes: 10 additions & 0 deletions
10
packages/instrumenter/test/unit/parsers/create-parser.spec.ts
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,10 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { createParser } from '../../../src/parsers/index.js'; | ||
|
||
describe(createParser.name, () => { | ||
it('should throw an error if the file extension is not supported', async () => { | ||
const sut = createParser({ plugins: null }); | ||
await expect(sut('# Readme', 'readme.md')).rejectedWith('Unable to parse readme.md. No parser registered for .md!'); | ||
}); | ||
}); |