Skip to content

Commit 189465a

Browse files
committed
Return warnings about recoverable parsing problems
1 parent f6668ca commit 189465a

14 files changed

Lines changed: 439 additions & 254 deletions

File tree

.eslintrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
extends: standard
22
rules:
33
no-labels: off
4+
no-multi-spaces: off

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Changelog
22

3+
## 0.1.0
4+
5+
Include a new property `warnings` in the parser output or error, which will contain an array of warnings. A warning is a mistake in the source code, which is not fatal (and will be compiled and executed properly), but which is against the modern language specification. Warn about:
6+
7+
* A line break in a single-line string
8+
* A backslash not followed by a whitespace
9+
* A `ifdef` or `ifndef` preprocessor directive without a name identifier following it
10+
* An object declared with other modifier than `public`
11+
* A preprocessor directive followed by non-whitespace characters
12+
* A semicolon following feature, function or script declaration
13+
* A missing line break or semicolon after an empty c-like for statement before the end keyword
14+
315
## 0.0.1
416

517
Initial release.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ Options:
167167
-S|--source <type> source type is object, script (default) or dump
168168
-O|--old-version expect the old version of OScript. defaults to false
169169
-e|--errors-only print only files that failed the check
170+
-w|--warnings consider warnings as failures too
170171
-s|--silent suppress output
171172
-v|--verbose print error stacktrace
172173
-p|--performance print parsing timing

dist/index.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ export type SourceType = 'object' | 'script' | 'dump'
3434
// ============================================================
3535
// Error Handling
3636

37-
export interface ParseError extends Error {
37+
export interface ParseWarning extends Error {
3838
line?: number
3939
column?: number
4040
offset?: number
4141
source?: string
42+
}
43+
44+
export interface ParseError extends ParseWarning {
4245
tokens?: Token[]
46+
warnings: ParseWarning[]
4347
}
4448

4549
// ============================================================
@@ -104,6 +108,7 @@ export interface TokenTypes {
104108
ObjRef: 16384
105109
LegacyAlias: 32768
106110
KeywordOrIdentifier: 64 | 128
111+
PunctuatorOrKeyword: 32 | 64
107112
Literal: 256 | 512 | 1024 | 2048 | 4096 | 8182 | 16384
108113
NoCode: 2 | 4 | 8 | 16
109114
}
@@ -115,6 +120,7 @@ export interface Program extends Node {
115120
type: 'Program'
116121
body: PackageDeclaration | ScriptSource | DumpSource
117122
tokens?: Token[] // if tokens are enabled during parsing
123+
warnings: ParseWarning[]
118124
}
119125

120126
// ---------- Package

lib/bin/oslint.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { parseText } from 'oscript-parser'
77
const { version } = JSON.parse(readFileSync(join(__dirname, '../../package.json'), 'utf-8'))
88
const args = process.argv
99
let errorsOnly = false
10+
let considerWarnings = false
1011
let silent = false
1112
let verbose = false
1213
let measure = false
@@ -25,6 +26,7 @@ Options:
2526
-S|--source <type> source type is object, script (default) or dump
2627
-O|--old-version expect the old version of OScript. defaults to false
2728
-e|--errors-only print only files that failed the check
29+
-w|--warnings consider warnings as failures too
2830
-s|--silent suppress output
2931
-v|--verbose print error stacktrace
3032
-p|--performance print parsing timing
@@ -53,6 +55,9 @@ for (let i = 2, l = args.length; i < l; ++i) {
5355
case 'e': case 'errors-only':
5456
errorsOnly = true
5557
continue
58+
case 'w': case 'warnings':
59+
considerWarnings = true
60+
continue
5661
case 's': case 'silent':
5762
silent = true
5863
continue
@@ -113,14 +118,22 @@ function run () {
113118
try {
114119
options.sourceFile = source.name
115120
const start = measure && performance.now()
116-
parseText(source.code, options)
121+
const { warnings } = parseText(source.code, options)
117122
let time
118123
if (start) {
119124
const end = performance.now()
120125
time = ` in ${Math.round(end - start)}ms`
121126
} else {
122127
time = ''
123128
}
129+
if (considerWarnings && warnings.length) {
130+
if (!silent) {
131+
for (const { message, line, column } of warnings) {
132+
console.warn(`${source.name}:${line}:${column}: ${message}`)
133+
}
134+
}
135+
process.exitCode = 1
136+
}
124137
if (!silent && !errorsOnly) console.log(`${source.name} succeeded${time}`)
125138
} catch (error) {
126139
if (!silent) {

lib/errors.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/format-message.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const slice = Array.prototype.slice // optimize the often access
2+
3+
// A sprintf-like implementation using `%index` placeholders (index based on 1)
4+
// to insert arguments to the message format.
5+
//
6+
// Example:
7+
// // Unexpected function in token
8+
// formatMessage('Unexpected %2 in %1.', 'token', 'function')
9+
10+
export default function formatMessage (format) {
11+
const args = slice.call(arguments, 1)
12+
return format.replace(/%(\d)/g, (match, index) => '' + args[index - 1])
13+
}

0 commit comments

Comments
 (0)