本文整理匯總了TypeScript中eslint.CLIEngine類的典型用法代碼示例。如果您正苦於以下問題:TypeScript CLIEngine類的具體用法?TypeScript CLIEngine怎麽用?TypeScript CLIEngine使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了CLIEngine類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: function
export default function () {
const eslintCli = new CLIEngine({
cwd: process.cwd(),
useEslintrc: false,
configFile: ESLINT_CONFIG_PATH
})
const sourceFiles = path.join(process.cwd(), projectConf.sourceRoot, '**/*.{js,ts,jsx,tsx}')
const report = eslintCli.executeOnFiles([sourceFiles])
const formatter = eslintCli.getFormatter()
return {
desc: '檢查 ESLint (以下為 ESLint 的輸出)',
raw: formatter(report.results)
}
}
示例2: doValidation
export function doValidation(document: TextDocument, engine: CLIEngine): Diagnostic[] {
const rawText = document.getText();
// skip checking on empty template
if (rawText.replace(/\s/g, '') === '') {
return [];
}
const text = rawText.replace(/ {10}/, '<template>') + '</template>';
const report = engine.executeOnText(text, document.uri);
return report.results[0] ? report.results[0].messages.map(toDiagnostic) : [];
}
示例3: require
import minimatch = require('minimatch')
/* eslint no-undef: off */
/* REASON: not compatible with import = require() syntax. */
// No PR is too small to include a description of why you made a change
if (danger.github) {
if (danger.github.pr.body.length < 10) {
warn('Please include a description of your PR changes.')
}
}
const filesToCheck = danger.git.created_files.concat(danger.git.modified_files)
// ESLint
const cli = new CLIEngine({})
const eslintPattern = '*.{js,jsx,ts,tsx}'
const filesToLint = filesToCheck
.filter(path => minimatch(path, eslintPattern, { matchBase: true }))
.filter(path => !cli.isPathIgnored(path))
const report = cli.executeOnFiles(filesToLint)
report.results.forEach(result => {
const { filePath } = result
result.messages.forEach(msg => {
const { line, message, ruleId } = msg
const rule = ruleId || 'N/A'
const messageText = `${filePath} line ${line} – ${message} (${rule})`
if (msg.severity === 1) {
warn(messageText)
} else if (msg.severity === 2) {
fail(messageText)
示例4: require
#!/usr/bin/env ts-node
import * as Path from 'path'
import chalk from 'chalk'
const { CLIEngine } = require('eslint')
const shouldFix = process.argv.indexOf('--fix') > -1
const eslint = new CLIEngine({
cache: true,
cwd: Path.dirname(__dirname),
fix: shouldFix,
rulePaths: [Path.join(__dirname, '..', 'eslint-rules')],
})
const report = eslint.executeOnFiles([
'./{script,eslint-rules}/**/*.{j,t}s?(x)',
'./tslint-rules/**/*.ts',
'./app/*.js',
'./app/{src,typings,test}/**/*.{j,t}s?(x)',
])
if (shouldFix) {
CLIEngine.outputFixes(report)
}
console.log(eslint.getFormatter()(report.results))
if (report.errorCount > 0) {
process.exitCode = 1
console.error(
示例5: Program
} = () => {
return {
visitor: {
Program (_, state) {
const { file: { code } } = state
const report = cli.executeOnText(code)
if (report.errorCount > 0) {
for (const result of report.results) {
for (const msg of result.messages) {
const err = codeFrameError({
start: {
line: msg.line,
column: msg.column
},
end: {
line: msg.endLine,
column: msg.endColumn
}
}, msg.message)
// tslint:disable-next-line
console.warn('\n' + `ESLint(${msg.ruleId}) 錯誤:` + err.message.replace('Declare only one React component per file', '一個文件隻能定義一個 Taro 類或 Taro 函數式組件') + '\n')
}
}
}
}
}
}
}
示例6: Program
} = () => {
return {
visitor: {
Program (_, state) {
const { file: { code } } = state
const report = cli.executeOnText(code)
if (report.errorCount > 0) {
for (const result of report.results) {
for (const msg of result.messages) {
const err = codeFrameError({
start: {
line: msg.line,
column: msg.column
},
end: {
line: msg.endLine,
column: msg.endColumn
}
}, msg.message)
// tslint:disable-next-line
console.warn('\n' + `ESLint(${msg.ruleId}) 錯誤:` + err.message + '\n')
}
}
}
}
}
}
}
示例7: writeFileAsync
export async function eslintAsync(filePatterns = sourceFilePatterns) {
const stats: Stats = engine.executeOnFiles([filePatterns]);
const fixedFiles: string[] = [];
await Promise.all(
stats.results.map(async result => {
const wasFixed = result.output !== undefined;
if (wasFixed) {
fixedFiles.push(result.filePath);
await writeFileAsync(result.filePath, result.output);
}
})
);
const errorsCount = stats.errorCount;
const errorsOrEmpty = await Promise.all(stats.results.map(formatter));
const errors = errorsOrEmpty.filter(Boolean);
return { errors, errorsCount, fixedFiles };
}